00001 package cedar.jetweb.job; 00002 00003 import java.io.*; 00004 import java.util.Date; 00005 import java.util.Vector; 00006 import java.util.Enumeration; 00007 //import java.util.zip.GZIPInputStream; 00008 00009 import java.text.ParseException; 00010 00011 import org.apache.commons.io.FileUtils; 00012 00013 import cedar.jetweb.*; 00014 import cedar.jetweb.model.*; 00015 import cedar.jetweb.model.plots.*; 00016 import cedar.jetweb.model.paper.*; 00017 import cedar.jetweb.job.PendingJob; 00018 import cedar.jetweb.generator.Generator; 00019 00020 import cedar.jetweb.xml.JetWebPlotReader; 00021 import cedar.jetweb.xml.JetWebPlotReaderFactory; 00022 00023 import cedar.jetweb.db.DBManager; 00024 import cedar.jetweb.db.DBJobManager; 00025 00026 import cedar.jetweb.util.JetWebFile; 00027 00035 public class LogFile implements Storeable { 00036 00038 public static final int RESERVED = 5; 00040 public static final int UNDEFINED = -1; 00042 public static final int SUBMITTED = 0; 00044 public static final int RUN = 1; 00046 public static final int COMPLETED = 2; 00048 public static final int ERROR = 3; 00049 00050 // Private data 00051 private JetWebFile logfile; 00052 00053 private String xmlFile = ""; 00054 00055 // Unique ID of this logfile. 00056 private int logId=-1; 00057 00058 // A runSeries object to contain all the parameters. 00059 private RunSeries runSeries; 00060 //private Vector<RunSeries> runSeries_; 00061 00062 private double lumi=0.0; 00063 private Double crossSection_ = -1.0; 00064 private boolean weighted=false; 00065 00066 private boolean valid = false; 00067 private Vector<PredictedPlot> mcPlots; 00068 00069 private int status; 00070 private int rsId = -1; 00071 00072 private static String SCHEME_PDFLIB="PDFLIB"; 00073 00079 public LogFile(RunSeries lp){ 00080 runSeries = lp; 00081 rsId = runSeries.getId(); 00082 00083 } 00084 00085 00089 public LogFile(int id){ 00090 logId = id; 00091 } 00092 00100 public LogFile(String name) throws JetWebException { 00101 00102 try { 00103 // Initialise File. 00104 00105 this.logfile = new JetWebFile(name); 00106 00107 status = LogFile.UNDEFINED; 00108 00109 // Fill the runSeriesId, the status and logId. 00110 retrieve(); 00111 00112 if (rsId>=0 && status>0) { 00113 // This is a finished logfile which has already been 00114 // added to the DB. 00115 valid = true; 00116 status = LogFile.COMPLETED; 00117 System.out.println("LogFile: Name already used. "+name); 00118 return; 00119 } 00120 00121 } catch (NullPointerException e){ 00122 System.out.println("Caused by "+e.getCause()); 00123 System.out.println(e.getMessage()); 00124 System.out.println("WARNING: LogFile not properly initialised; JetWebConfig probably not initialised"); 00125 } 00126 } 00127 00128 public boolean retrieve() throws JetWebException { 00129 if (logId>=0) { 00130 DBJobManager.selectFromDB(this); 00131 } else { 00132 DBJobManager.selectFromDBbyName(this); 00133 } 00134 return true; 00135 } 00136 00151 public boolean store() throws JetWebException { 00152 00153 // First check this logfile isn't already stored. 00154 //checkStatus(); 00155 DBJobManager.checkStatus(this); 00156 00157 if (status==COMPLETED) { 00158 System.out.println 00159 ("LogFile: attempt to add a LogFile for which an entry exists." 00160 ); 00161 System.out.println("DB not changed, LogFile rejected."); 00162 return false; 00163 00164 } else if (status==RUN) { 00165 00166 //System.out.println("LogFile: Making plots"); 00167 00168 00169 //add the log file if it isn't already in the DB. 00170 00171 addLogFile(); 00172 00173 // Make MC papers associated with this logfile by building 00174 // & reading XML. 00175 00176 makePlots(); 00177 00178 if (!valid) { 00179 System.out.println("LogFile: Invalid plots. Nothing added to DB."); 00180 return false; 00181 } 00182 00183 00184 // Write the logfile, the parameters (or a link to them) and the 00185 // updated predicted_point to the DB. 00186 return toDB(); 00187 00188 }else if(status==SUBMITTED){ 00189 addLogFile(); 00190 return true; 00191 // }else if(status==RESERVED){ 00192 00193 }else{ 00194 00195 System.out.println("LogFile: attempt to add a LogFile with undefined/error status"); 00196 System.out.println("DB not changed, LogFile rejected."); 00197 00198 System.out.println("Logfile: status = " + status); 00199 return false; 00200 00201 } 00202 } 00203 00209 public JetWebFile getFile(){ return logfile; } 00210 00216 public void setFile(JetWebFile f){ logfile = f; } 00217 00223 public int getId(){ return logId;} 00224 00225 00238 public int getStatus() { 00239 return status; 00240 } 00241 00247 public RunSeries getRunSeries() throws JetWebException { 00248 00249 if (rsId<0 && runSeries !=null) { 00250 00251 rsId = runSeries.getId(); 00252 } else if ( (runSeries==null && rsId>0) || 00253 (runSeries!=null) && (runSeries.getId()!=rsId) && 00254 rsId >= 0) { 00255 00256 //runSeries = new RunSeries(rsId); 00257 runSeries = RunSeries.Maker(rsId); 00258 } 00259 return runSeries; 00260 } 00261 00262 00264 public Generator getGenerator() throws JetWebException { 00265 try { 00266 return getRunSeries().getGenerator(); 00267 } catch (JetWebException jwe){ 00268 return null; 00269 } 00270 } 00271 00278 public void setRunSeriesId(int inew){ 00279 rsId = inew; 00280 if (runSeries!=null){ 00281 runSeries.setId(inew); 00282 } 00283 } 00284 00291 public void setRunSeries(RunSeries newrs){ 00292 runSeries = newrs; 00293 rsId = runSeries.getId(); 00294 } 00295 00301 public void setId(int i){ logId = i;} 00317 public void setStatus(int i) throws JetWebException { 00318 00319 if (checkStatus(i)){ 00320 status = i; 00321 DBJobManager.updateStatus(this); 00322 } else { 00323 throw new JetWebException("Attempt to set invalid Status on logfile:"+i,""); 00324 } 00325 } 00326 00327 public LogFile setStatusAsSubmitted(){ 00328 try{ 00329 setStatus(SUBMITTED); 00330 }catch(JetWebException err){ 00331 System.out.println(err.getMessage()); 00332 } 00333 return this; 00334 } 00335 00336 private boolean checkStatus(int i){ 00337 boolean ok = false; 00338 if (i==RESERVED || 00339 i==UNDEFINED || 00340 i==SUBMITTED || 00341 i==RUN || 00342 i==COMPLETED || 00343 i==ERROR ){ 00344 ok = true; 00345 } 00346 return ok; 00347 } 00348 00350 public void setGenerator(Generator gen) throws JetWebException { 00351 getRunSeries().getGeneratorList().clear(); 00352 getRunSeries().getGeneratorList().add(gen);} 00353 00359 public LogFile setCrossSection(Double cs){ 00360 crossSection_ = cs; 00361 return this; 00362 } 00363 00368 public Double getCrossSection(){ 00369 return crossSection_; 00370 } 00371 00376 public void setLumi(double l){ lumi = l;} 00380 public double getLumi(){ return lumi;} 00381 00382 // Methods 00383 00384 //adds a new logfile row in the submitted state 00385 00386 private void addLogFile() 00387 throws JetWebException{ 00388 00389 try{ 00390 00391 if(runSeries.getId()<0){ 00392 rsId = DBManager.matchRunSeriesId(runSeries); 00393 runSeries.setId(rsId); 00394 } 00395 00396 if(runSeries.getId()>0){ 00397 //DBManager.addLumi(lumi, runSeries.getId()); 00398 DBManager.addLumi(this); 00399 rsId = runSeries.getId(); 00400 }else{ 00401 runSeries.setLumi(lumi); 00402 DBManager.addRunSeries(runSeries); 00403 rsId = runSeries.getId(); 00404 00405 } 00406 00407 DBJobManager.addLogfile(this); 00408 }catch(JetWebException err){ 00409 System.out.println 00410 ("Logfile: This file not added - failed updating runSeries"); 00411 valid = false; 00412 throw err; 00413 } 00414 return; 00415 } 00416 00426 private synchronized boolean toDB() throws JetWebException { 00427 00428 if (status!=LogFile.RUN) {return false;} 00429 00430 setRunSeriesId(DBManager.matchRunSeriesId(getRunSeries())); 00431 00432 int update=0; 00433 00434 if (runSeries.getId()>0){ 00435 // Add the plots from this logfile to those already in the DB. 00436 addPlots(); 00437 // Add the lumi and update the timestamp. 00438 //DBManager.addLumi(lumi,runSeries.getId()); 00439 00440 //no need to add the lumi here because it was already done 00441 //by the addLogFile method 00442 00443 //DBManager.addLumi(this); 00444 } else { 00445 00446 //No existing db record so add a row to the runSeries table. 00447 try { 00448 00449 runSeries.setLumi(lumi); 00450 DBManager.addRunSeries(runSeries); 00451 } catch (JetWebException e){ 00452 System.out.println("LogFile: This file not added."); 00453 valid = false; 00454 throw e; 00455 } 00456 update = 1; 00457 } 00458 00459 // Now insert the points into the predicted_point table. 00460 //Enumeration<PredictedPlot> plots = mcPlots.elements(); 00461 for (PredictedPlot plot : mcPlots){ 00462 //while (plots.hasMoreElements()){ 00463 //PredictedPlot plot = (PredictedPlot)plots.nextElement(); 00464 //plot.setRunSeriesId(runSeries.getId()); 00465 plot.setRunSeries(runSeries); 00466 // Only update plots which have data for this logfile. 00467 if (plot.hasData()){ 00468 plot.toDB(update); 00469 } 00470 } 00471 00472 // Finally add or update a row to the logfile table. 00473 setStatus(COMPLETED); 00474 DBJobManager.addLogfile(this); 00475 return true; 00476 } 00477 00478 00482 public void doChecks() throws JetWebException { 00483 valid=true; 00484 00485 RunSeries runSeries = getRunSeries(); 00486 Generator gen = runSeries.getGenerator(); 00487 00488 // Check we have RunSeries and Generator. 00489 if ( (runSeries==null) || (getGenerator()==null) ) { 00490 System.out.println("LogFile: Null runseries or generator"); 00491 valid=false; 00492 return; 00493 } 00494 00495 // Check the consistency etc of the process name. 00496 //runSeries.checkMCProcessType(); 00497 00498 MCProcessType proc = runSeries.getMCProcessType(); 00499 00500 // Check the MCProcessType is one we know about. 00501 00502 00503 // Check we have RunSeries and Generator. 00504 if ( (proc==null) ) { 00505 System.out.println("LogFile: Null MCProcessType"); 00506 valid=false; 00507 return; 00508 } 00509 00510 00511 if (proc.getId()<=0) { 00512 System.out.println("LogFile: Unknown ProcessType"); 00513 valid=false; 00514 return; 00515 } 00516 00517 00518 // Remove any irrelevant parameters. 00519 runSeries.removeIrrelevantParameters(); 00520 00521 // Perform any generator specific consistency checks. 00522 valid = gen.checkParameters(); 00523 return; 00524 } 00525 00532 public String getStem(){ 00533 00534 return logfile.directory() + logfile.toString(); 00535 } 00541 public String getXmlFileName() throws JetWebException { 00542 00543 String path; 00544 00545 if (!xmlFile.equals("")){ 00546 if(xmlFile.endsWith(JetWebFile.XML)|| 00547 xmlFile.endsWith(JetWebFile.XML + ".gz")){ 00548 path = xmlFile; 00549 }else{ 00550 path = xmlFile + JetWebFile.XML; 00551 } 00552 } else { 00553 path = logfile + logfile.XML; 00554 } 00555 //either .xml or .xml.gz SHOULD exist 00556 //JetWebFile test = new JetWebFile(xmlFile); 00557 JetWebFile test = new JetWebFile(path); 00558 00559 if(!test.isXML() || !test.exists()){ 00560 throw new JetWebException 00561 ("Logfile Error: Attempt to open XML file which does not exist" 00562 ,test + " in directory " + test.directory()); 00563 } 00564 00565 test.Zip(); 00566 00567 setXmlFile(path); 00568 00569 return test.fullPath(); 00570 } 00571 public void setXmlFile(String path){ 00572 xmlFile= path; 00573 logfile = new JetWebFile(path); 00574 } 00580 public boolean hasAidaFile(){ 00581 boolean exists = false; 00582 String path = getStem(); 00583 //.aida SHOULD exist 00584 JetWebFile test = new JetWebFile(path, JetWebFile.AIDA); 00585 if (!test.exists()){ 00586 System.out.println 00587 ("Logfile: AIDA File does not exist :"+test.fullPath()); 00588 } else { 00589 System.out.println 00590 ("LogFile: Found matching AIDA file"); 00591 exists = true; 00592 } 00593 return exists; 00594 } 00595 00601 public boolean isArchived(){ 00602 boolean archived = false; 00603 String path = getStem(); 00604 00605 JetWebFile test = new JetWebFile(path, JetWebFile.AIDA); 00606 00607 if(test.exists()){ 00608 if(test.isArchived()){ 00609 archived = true; 00610 } 00611 } 00612 return archived; 00613 } 00614 00615 00616 00623 public boolean isValid(){return valid;} 00624 public void setValid(boolean b){ valid = b;} 00625 00626 /* 00627 * Instantiate MC plot objects from xml file. 00628 */ 00629 private void makePlots() throws JetWebException { 00630 00631 if (!valid) { 00632 System.out.println("Warning: attempt to make paper for invalid log "+logfile.getPath()); 00633 return; 00634 } 00635 00636 // mcPlots will be a Vector containing all plots which 00637 // are predicted by this LogFile. 00638 mcPlots = new Vector<PredictedPlot>(); 00639 00640 // first make a plot reader. 00641 String xfname = getStem()+".aida"; 00642 00643 try { 00644 if ( (new JetWebFile(xfname)).exists() ){ 00645 System.out.println("LogFile: found file "+xfname); 00646 JetWebPlotReader reader = 00647 JetWebPlotReaderFactory.getNewPlotReader(xfname); 00648 00649 for (Paper paper : PaperBank.getPapers()){ 00650 00651 reader.setDirectory(paper.getDirName()); 00652 00653 for (DataPlot dplot : paper.getPlots()){ 00654 00655 RealPlot rplot = (RealPlot)dplot; 00656 00657 PredictedPlot plot = new PredictedPlot(rplot,runSeries); 00658 00659 //Only fill and write the plot if this 00660 //logfile generated the correct process in the 00661 //correct region of phase space 00662 00663 if (runSeries.getMCProcessType().getId()== 00664 plot.getProcIdWanted() && 00665 plot.getCutCollection().isWithin 00666 (getGenerator().getCutCollection())){ 00667 00668 // Read the xml file for the plot. 00669 try { 00670 reader.fillPlot(plot); 00671 00672 // Now, various fixes to plots to account for bugs 00673 // and features in HZTOOL. 00674 if (weighted) { 00675 // No more weighted runs. 00676 plot.zero(); 00677 } else { 00678 // Set errors on zero points to upper limits. 00679 if (plot.getYLabel().indexOf("pb")>0){ 00680 plot.fixZeroErrors(lumi/1000.); 00681 } else { 00682 plot.fixZeroErrors(lumi); 00683 } 00684 } 00685 // Remove points with known bug (run list 00686 // lookup table). 00687 //JobUtils.removeBadPoints(plot,this); 00688 00689 // Add this plot to the MC plots Vector. 00690 mcPlots.add(plot); 00691 00692 } catch (JetWebException e) { 00693 // not found this histogram. try the next. 00694 System.out.println("Not found"); 00695 } 00696 00697 } 00698 } 00699 } 00700 00701 } 00702 } catch (JetWebException je) { 00703 System.out.println("LogFile: no data for "+xfname); 00704 System.out.println(je); 00705 je.printStackTrace(System.out); 00706 } 00707 00708 if (mcPlots.size()==0) { 00709 System.out.println("LogFile: no valid plots found"); 00710 valid=false; 00711 } 00712 } 00713 00714 00715 00716 00717 // Add these plots to any matching ones in the DB. 00718 private void addPlots() throws JetWebException { 00719 00720 // Add the plots for this logfile to those already associated with 00721 // this row of runSeries. 00722 00723 for (PredictedPlot mcPlot : mcPlots){ 00724 00725 // Get the plots associated with this runSeries table. 00726 // Create a DataPlot object from this record and the DataPlot. 00727 PredictedPlot plot2 = null; 00728 plot2 = new PredictedPlot(mcPlot.getCompPlot(),runSeries); 00729 System.out.println(mcPlot.getNumber()+","+plot2.getNumber()); 00730 if (plot2.hasData()){ 00731 mcPlot.add(plot2); 00732 } 00733 } 00734 } 00735 00736 00795 private void dumpStream(InputStream in) throws IOException { 00796 BufferedReader br = new BufferedReader(new InputStreamReader(in)); 00797 String line; 00798 while ((line = br.readLine()) != null) { 00799 } 00800 } 00801 00802 public String toString() { 00803 00804 try { 00805 StringBuffer b = new StringBuffer("Logfile: "+logfile.getPath()+"\n"); 00806 b.append("Generator:");b.append(getGenerator());b.append("\n"); 00807 b.append("Luminosity:");b.append(getLumi());b.append("\n"); 00808 00809 b.append("Process:"); 00810 b.append(getRunSeries().getMCProcessType().getMCProcessName()); 00811 b.append("\n"); 00812 00813 if (isValid()){ 00814 b.append("Logfile is valid, "); 00815 } else { 00816 b.append("Logfile is invalid, "); 00817 } 00818 String statusString = "UNDEFINED"; 00819 if (status==RUN) { 00820 statusString="RUN"; 00821 } else if (status==COMPLETED){ 00822 statusString="COMPLETED"; 00823 } else if (status==SUBMITTED){ 00824 statusString="SUBMITTED"; 00825 } 00826 b.append("Status is "+statusString); 00827 00828 00829 return b.toString(); 00830 00831 } catch (Exception e){ 00832 return e.getMessage(); 00833 00834 } 00835 00836 00837 } 00838 00842 public void checkStatus() throws JetWebException { 00843 if (isValid() && status!=ERROR){ 00844 //setStatus(RUN); 00845 status=RUN; 00846 DBJobManager.checkStatus(this); 00847 } else { 00848 setStatus(UNDEFINED); 00849 } 00850 } 00851 00852 }
Generated Wed Jan 17 09:14:27 GMT 2007