00001 package cedar.jetweb.job; 00002 00003 import java.io.*; 00004 00005 import java.util.*; 00006 import java.util.Map.Entry; 00007 00008 import cedar.jetweb.*; 00009 import cedar.jetweb.html.*; 00010 import cedar.jetweb.xml.*; 00011 import cedar.jetweb.model.*; 00012 import cedar.jetweb.model.plots.*; 00013 import cedar.jetweb.model.paper.*; 00014 import cedar.jetweb.model.fit.Fit; 00015 import cedar.jetweb.db.DBJobManager; 00016 import cedar.jetweb.batch.SubmitScriptHandler; 00017 import cedar.jetweb.generator.Generator; 00018 import cedar.jetweb.util.JetWebFile; 00019 00020 import cedar.jetweb.*; 00021 00022 import org.apache.commons.io.FileUtils; 00023 00028 public abstract class JobUtils { 00029 00034 public static void updateJobs() throws JetWebException { 00035 00036 // Add all new LogFiles and return a vector of them: 00037 00038 00039 System.out.println("=== checking new files ==="); 00040 00041 Vector<LogFile> logFiles = getLogFiles 00042 (new File(JetWebConfig.jobInDirName)); 00043 00044 System.out.println("=== found vector of new files ==="); 00045 00046 // for each logfile find the models that are affected. 00047 00048 for(LogFile log: logFiles){ 00049 00050 System.out.println("=== "); 00051 00052 System.out.println("=== getting runseries from file ==="); 00053 00054 RunSeries rs = log.getRunSeries(); 00055 00056 System.out.println("=== found runseries ==="); 00057 00058 //Set the Cuts applied to this runseries 00059 00060 System.out.println("=== setting cuts on runseries ==="); 00061 00062 rs.getGenerator().setCutCollection 00063 (log.getGenerator().getCutCollection()); 00064 00065 00066 00067 //set the smallest cut collection that fits all the plots 00068 //matched by this runseries 00069 00070 rs.setCuts(); 00071 00072 00073 System.out.println("=== cuts set ==="); 00074 00075 System.out.println("=== getting consistent models ==="); 00076 00077 Vector<Model> changedModels = 00078 rs.getConsistentModels(); 00079 //make a new model if there isn't one 00080 00081 System.out.println("=== got consistent models ==="); 00082 00083 if(changedModels.size()==0){ 00084 00085 System.out.println("JobUtils: Found no consistent Models"); 00086 00087 System.out.println("=== making and storing new model ==="); 00088 00089 Model model = new Model(rs); 00090 model.store(); 00091 changedModels.add(model); 00092 00093 System.out.println("=== made and stored new model ==="); 00094 00095 } 00096 00097 for(Model model: changedModels){ 00098 00099 System.out.println 00100 ("=== at start of loop over changed models ==="); 00101 00102 System.out.println("=== adding all runseries to model ==="); 00103 00104 model.addAllRunSeries(); 00105 00106 System.out.println("=== all runseries added ==="); 00107 00108 System.out.println("=== storing model ==="); 00109 00110 model.store(); 00111 00112 System.out.println("=== model stored ==="); 00113 00114 System.out.println("=== making new plotselection ==="); 00115 00116 PlotSelection sel = new PlotSelection(); 00117 00118 System.out.println("=== made new plotselection ==="); 00119 System.out.println 00120 ("=== setting default plots in selection ==="); 00121 00122 sel.setFitDefault(); 00123 sel.setSumDefault(); 00124 00125 System.out.println("=== default plots set ==="); 00126 00127 System.out.println("=== getting cuts from logfile ==="); 00128 00129 CutCollection phaseSpace = 00130 log.getGenerator().getCutCollection(); 00131 00132 System.out.println("=== got cuts ==="); 00133 00134 System.out.println("=== restricting plotselection cuts ==="); 00135 00136 sel.restrictCutCollection(rs.getMCProcessType(),phaseSpace); 00137 00138 System.out.println("=== done restricting cuts ==="); 00139 00140 Fit fit = null; 00141 boolean goodFit = false; 00142 00143 try { 00144 00145 System.out.println("=== making new fit ==="); 00146 00147 fit = new Fit(model,sel); 00148 goodFit = true; 00149 00150 System.out.println("=== made new fit ==="); 00151 00152 } catch (JetWebException jwe) { 00153 // Failed to build the default fit, so do the best we can. 00154 00155 00156 System.out.println("=== setting best plots ==="); 00157 00158 sel.setFitToBest(model); 00159 sel.setSumToBest(model); 00160 sel.restrictCutCollection(rs.getMCProcessType(),phaseSpace); 00161 00162 System.out.println("=== set best plots ==="); 00163 00164 if (sel.getFitPlots().size()==0){ 00165 00166 // None of the default fit plots have been simulated, 00167 // so just generate a fit 00168 // using all the plots which the Chi2 is used for. 00169 00170 System.out.println 00171 ("=== setting fit plots ==="); 00172 00173 sel.setFitPlots(sel.getSumPlots()); 00174 00175 System.out.println("=== fit plots set ==="); 00176 } 00177 if (sel.getFitPlots().size()>0){ 00178 try{ 00179 00180 System.out.println("=== making new fit ==="); 00181 00182 fit = new Fit(model,sel); 00183 goodFit = true; 00184 00185 System.out.println("=== new fit made ==="); 00186 00187 }catch (Throwable err){ 00188 System.out.println(err.getMessage()); 00189 } 00190 } 00191 } 00192 00193 if(goodFit){ 00194 System.out.println("Now to update the fit and save it"); 00195 00196 System.out.println("=== updating fit ==="); 00197 fit.update(false); 00198 00199 System.out.println("=== fit updated ==="); 00200 00201 System.out.println("back from update"); 00202 } 00203 } 00204 } 00205 00206 return; 00207 } 00208 00209 00213 public static void doJobRequest(ResultSearchPattern pattern, 00214 Vector processList, 00215 PrintWriter out, 00216 String servletPath, 00217 String requestingHost 00218 ) throws JetWebException { 00219 00220 JobRequest jreq = null; 00221 Model model = null; 00222 00223 if (processList.size()==0){ 00224 // Must specify at least one process. 00225 HTMLWriter op = new HTMLWriter(out,servletPath); 00226 HTMLUtils.writeJobRequestForm("Please specify a process to generate.", 00227 pattern,new PlotSelection(), 00228 out,servletPath); 00229 return; 00230 } 00231 HashMap<MCProcessType, RunSeries> submittedRunSeries = 00232 new HashMap<MCProcessType, RunSeries>(); 00233 // Build a new job request. 00234 try { 00235 00236 // Build a Model from the search pattern. 00237 model = new Model(pattern); 00238 model.addAllRunSeries(); 00239 00240 // New logfile row not written at this point 00241 00242 //LogFile log = new LogFile(); 00243 00244 //Get a list of the already existing runseries that match 00245 //the model to be submitted. 00246 //Vector<RunSeries> existingRunSeries = new Vector<RunSeries>(); 00247 00248 HashMap<MCProcessType, RunSeries> existingRunSeries = 00249 new HashMap<MCProcessType, RunSeries>(); 00250 00251 for(Enumeration proc = processList.elements(); 00252 proc.hasMoreElements();){ 00253 00254 for(RunSeries rs: 00255 model.getRunSeries((MCProcessType)proc.nextElement())){ 00256 if(pattern.matches(rs)){ 00257 00258 //need to check cut collection against 00259 //existing runseries 00260 00261 CutCollection existingCuts = 00262 rs.getCutCollection(); 00263 00264 if(existingCuts!=null && 00265 (existingCuts.getCuts().size()==0 || 00266 existingCuts.isWithin( 00267 pattern.getGenerator().getCutCollection())) 00268 ){ 00269 00270 RunSeries exists = existingRunSeries.get 00271 (rs.getMCProcessType()); 00272 if(exists==null || 00273 exists.getCutCollection(). 00274 isWithin(rs.getCutCollection()) 00275 ){ 00276 00277 existingRunSeries.put(rs.getMCProcessType(), rs); 00278 } 00279 } 00280 } 00281 } 00282 } 00283 00284 // Create a run series if necessary 00285 // for this submission 00286 00287 for(Enumeration proc = processList.elements(); 00288 proc.hasMoreElements();){ 00289 00290 RunSeries tmpRS = new RunSeries(pattern); 00291 MCProcessType tmpProc = (MCProcessType)proc.nextElement(); 00292 tmpRS.setMCProcessType(tmpProc); 00293 tmpRS.setCuts(); 00294 submittedRunSeries.put(tmpProc,tmpRS); 00295 00296 } 00297 00298 for(Map.Entry entry: existingRunSeries.entrySet()){ 00299 00300 RunSeries tmpRS = (RunSeries) entry.getValue(); 00301 MCProcessType tmpProc = (MCProcessType) entry.getKey(); 00302 00303 RunSeries submitted = submittedRunSeries.get(tmpProc); 00304 00305 if(submitted==null || 00306 submitted.getCutCollection().isWithin 00307 (tmpRS.getCutCollection()) || 00308 submitted.getCutCollection().equals 00309 (tmpRS.getCutCollection())){ 00310 submittedRunSeries.put 00311 (tmpProc, tmpRS); 00312 00313 } 00314 } 00315 00316 //for(RunSeries rs: existingRunSeries){ 00317 //submittedRunSeries.put(rs.getMCProcessType(), rs); 00318 //} 00319 00320 } catch (JetWebException e) { 00321 // Keep bouncing back if there is not enough information to 00322 // specify the model. 00323 HTMLWriter op = new HTMLWriter(out,servletPath); 00324 HTMLUtils.writeJobRequestForm("Please specify all fields.", 00325 pattern,new PlotSelection(), 00326 out,servletPath); 00327 return; 00328 } 00329 00330 //No logfile written to DB by this point 00331 StringBuffer jobsDone = new StringBuffer(); 00332 Enumeration list = processList.elements(); 00333 int status = -1; 00334 while (list.hasMoreElements()){ 00335 MCProcessType proc = (MCProcessType)list.nextElement(); 00336 00337 jreq = new JobRequest(model,proc,requestingHost); 00338 00339 if (jreq==null) { 00340 throw new JetWebException("JobUtils:","No process types were selected!"); 00341 } 00342 00343 // Set the job name here. 00344 jreq.setJobName(); 00345 00346 // Make the files for this job. 00347 makeCards(jreq); 00348 String newFilePath = JetWebConfig.jobOutDirName+"/"+jreq.getJobName(); 00349 makeScript(newFilePath,jreq.getModel().getGenerator()); 00350 00351 // Submit it. 00352 status = submit(jreq); 00353 00354 if (status == 0) { 00355 jobsDone.append("Successfully submitted: " 00356 +HTMLJobWriter.getDescription(jreq)); 00357 RunSeries rs = submittedRunSeries.get(proc); 00358 00359 LogFile log = new LogFile(rs); 00360 00361 log.setFile(new JetWebFile(JetWebConfig.jobOutDirName + 00362 "/" + 00363 jreq.getJobName())); 00364 //retrieve the already reserved logfile name 00365 //doing this sets the log status to reserved 00366 log.retrieve(); 00367 log.getGenerator().setCutCollection 00368 (pattern.getGenerator().getCutCollection()); 00369 log.setStatus(LogFile.SUBMITTED); 00370 00371 log.store(); 00372 //model.store(); 00373 } else { 00374 jobsDone.append("Failed job submission code "+status+"<br/>"); 00375 } 00376 } 00377 00378 if(status == 0 ){ 00379 model.addAllRunSeries(); 00380 model.store(); 00381 } 00382 00383 HTMLUtils.writeQueryScreen(jobsDone.toString(), 00384 pattern,new PlotSelection(), 00385 out,servletPath); 00386 return; 00387 } 00388 00396 public static void makeCards(JobRequest jreq) 00397 throws JetWebException { 00398 00399 Model model = jreq.getModel(); 00400 MCProcessType proc = jreq.getMCProcessType(); 00401 Collision collision = proc.getCollision(); 00402 Generator gen = model.getGenerator(); 00403 00404 System.out.println("JobUtils.makecards:"+jreq.getCardName()+" , "+gen); 00405 00406 File test = new File(JetWebConfig.rootDirectory+jreq.getCardName()); 00407 00408 if (test.exists()) { 00409 throw new JobRequestException("cannot create new card because filename already exists"); 00410 } 00411 00412 try { 00413 BufferedWriter newCards = new BufferedWriter(new FileWriter(jreq.getCardName()) ); 00414 00415 gen.writeSteering(newCards,model,proc); 00416 newCards.newLine(); 00417 newCards.close(); 00418 00419 } catch ( IOException e ) { 00420 System.out.println("problem creating new card file:"+e); 00421 throw new JetWebException(e); 00422 } catch ( NullPointerException n ) { 00423 throw new JetWebException(n); 00424 } 00425 00426 } 00427 00435 public static void makeScript(String scriptname, Generator gen) 00436 throws JetWebException { 00437 00438 String line; 00439 File test = new File(scriptname); 00440 00441 if (test.exists()) { 00442 throw new JobRequestException("cannot create new script because filename already exists"); 00443 } 00444 00445 try { 00446 BufferedReader templateScript = 00447 new BufferedReader( new InputStreamReader( new FileInputStream(SubmitScriptHandler.getTemplate()))); 00448 BufferedWriter newScript = new BufferedWriter(new FileWriter(scriptname) ); 00449 while((line=templateScript.readLine())!=null){ 00450 // Choose executable 00451 if (line.startsWith("PROGRAM=")) { 00452 line = "PROGRAM=" + SubmitScriptHandler.getExecutable(gen); 00453 } 00454 newScript.write(line); 00455 newScript.newLine(); 00456 } 00457 00458 newScript.close(); 00459 templateScript.close(); 00460 00461 } catch ( IOException e ) { 00462 System.out.println("problem creating new script file:"+e); 00463 return; 00464 } 00465 00466 } 00467 00472 public static void moveLogs(Enumeration logs, String newDir){ 00473 00474 String editDir = JetWebConfig.rootDirectory+"/"+newDir+"/"; 00475 00476 while (logs.hasMoreElements()){ 00477 00478 LogFile log = (LogFile)logs.nextElement(); 00479 00480 String xmlName = log.getStem()+".xml"; 00481 File xml = new File(xmlName); 00482 00483 String aidaName = log.getStem()+".aida"; 00484 File aida = new File(aidaName); 00485 00486 File file = log.getFile(); 00487 00488 File newFile = new File(editDir+file.getName()); 00489 file.renameTo(newFile); 00490 00491 newFile = new File(editDir+xml.getName()); 00492 xml.renameTo(newFile); 00493 00494 newFile = new File(editDir+aida.getName()); 00495 aida.renameTo(newFile); 00496 00497 // Remove this logfile from the logfile table. 00498 DBJobManager.removeLogFile(log); 00499 } 00500 } 00501 00502 00507 public static boolean isValidLogName(String name){ 00508 00509 if (name.equals("CVS")){ 00510 return false; 00511 } 00512 00513 if ( ((name.indexOf(".o")>0) || (name.indexOf(".log")>0) && 00514 !(name.indexOf("~")>0) && 00515 !(name.indexOf("#")>0)) && 00516 !name.contains(".gz")){ 00517 return true; 00518 } 00519 return false; 00520 00521 } 00522 00528 public static String zipIt(String name) throws JetWebException { 00529 00530 JetWebFile file = new JetWebFile(name); 00531 00532 file = file.Zip(); 00533 00534 return file.directory() + file + file.extension() + ".gz"; 00535 } 00536 00540 public static void removeBadPoints(PredictedPlot plot, LogFile log) 00541 throws JetWebException { 00542 00543 String logName = log.getFile().getName(); 00544 String runLabel = logName.substring(0,1); 00545 int runNumber; 00546 int csnId = plot.getId(); 00547 try { 00548 runNumber = Integer.parseInt(logName.substring(1,12)); 00549 } catch (Exception e) { 00550 runNumber = 0; 00551 runLabel = "old"; 00552 } 00553 00554 //System.out.println(runSeries+","+runNumber); 00555 00556 return; 00557 } 00558 00562 public static int countJobs(String batchNum) throws JetWebException { 00563 return DBJobManager.countJobs(batchNum); 00564 } 00568 public static int countCompletedJobs(String batchNum) throws JetWebException { 00569 return DBJobManager.countCompletedJobs(batchNum); 00570 } 00571 00575 public static Enumeration selectPendingJobs() throws JetWebException { 00576 return DBJobManager.selectPendingJobs(); 00577 } 00578 00579 00580 00593 public static int submit(JobRequest jreq) { 00594 00595 String line; 00596 try { 00597 jreq.setSubmitScript(File.createTempFile("job",".ready."+jreq.getRequestingHost(), 00598 new File(JetWebConfig.spoolDirName))); 00599 } catch (IOException e){ 00600 return SubmitScriptHandler.CANTWRITESCRIPT; 00601 } 00602 00603 // pj.setScriptName(submitScript.getName()); // BMW: pj not created 00604 00605 // make the submit script. 00606 return SubmitScriptHandler.make(jreq); 00607 00608 } 00609 00616 public static boolean moveAndStore(LogFile log) throws JetWebException { 00617 00618 JetWebFile logFile = log.getFile(); 00619 00620 String stem = log.getStem(); 00621 00622 JetWebFile file = new JetWebFile(stem, JetWebFile.AIDA); 00623 file = file.Archive(); 00624 file = file.Zip(); 00625 00626 file = new JetWebFile(stem, JetWebFile.XML); 00627 file = file.Archive(); 00628 file = file.Zip(); 00629 00630 file = new JetWebFile(stem, JetWebFile.OUT); 00631 file = file.Archive(); 00632 file = file.Zip(); 00633 00634 if (log.store()) { 00635 log.setStatus(LogFile.COMPLETED); 00636 } 00637 return true; 00638 } 00639 00652 public static Vector<Model> getNewData(File root) throws JetWebException { 00653 00654 Vector<Model> modelList = new Vector<Model>(); 00655 Vector<File> fileList = new Vector<File>(); 00656 00657 if (root.isDirectory()) { 00658 00659 // Add the root directory to the list of files to be checked. 00660 fileList.add(root); 00661 00662 // Find files ending with .xml in this directory 00663 // and any subdirectories. 00664 while (fileList.size()>0){ 00665 00666 Enumeration<File> list = fileList.elements(); 00667 00668 while (list.hasMoreElements()){ 00669 00670 File tmpfile = list.nextElement(); 00671 00672 // First look for directories. 00673 if (tmpfile.isDirectory()){ 00674 System.out.println 00675 ("JobUtils: Searching in "+tmpfile.getPath()); 00676 00677 File[] array = tmpfile.listFiles(); 00678 int n = array.length; 00679 for (int i=0; i<n; i++){ 00680 fileList.addElement(array[i]); 00681 } 00682 00683 } else { 00684 00685 // Now look for aida files. 00686 String tmpname = tmpfile.getPath(); 00687 00688 if ( JobUtils.isValidLogName(tmpname)) { 00689 // Now search through to see if it terminated successfully. 00690 00691 // first, gzip it if necessary. 00692 tmpname = JobUtils.zipIt(tmpname); 00693 00694 // Instantiate, but don't add to the DB yet. 00695 LogFile log = new LogFile(tmpname); 00696 System.out.println 00697 ("JobUtils: Created log: about to fill."); 00698 System.out.println("JobUtils: Using xml file name " 00699 + log.getXmlFileName()); 00700 JetWebLogReader logReader = 00701 new JetWebLogReader(log.getXmlFileName()); 00702 00703 logReader.fillLog(log); 00704 log.checkStatus(); 00705 System.out.println 00706 ("JobUtils: log status = " + log.getStatus()); 00707 if (log.getStatus()==LogFile.RUN){ 00708 System.out.println("JobUtils:"+log.toString()); 00709 // Now see if an aida file really exists. 00710 if (log.hasAidaFile()){ 00711 // Move the logfile & add it to the DB. 00712 if (moveAndStore(log)){ 00713 00714 // Get all models which can be constructed from 00715 // this runSeries in combination with others. 00716 System.out.println 00717 ("JobUtils: adding models"); 00718 RunSeries rs = log.getRunSeries(); 00719 Vector<Model> mList = 00720 rs.getConsistentModels(); 00721 00722 if (mList.size()==0) { 00723 // Make a new model with 00724 // this run series plus 00725 // default parameters for 00726 // any not set by the 00727 // runseries. 00728 System.out.println 00729 ("JobUtils: Model: Making a new one for " + 00730 log.getXmlFileName()); 00731 Model model = new Model(rs); 00732 model.store(); 00733 mList.add(model); 00734 } else { 00735 System.out.println("JobUtils: Model:Not making a new one for "+log.getXmlFileName()); 00736 } 00737 modelList.addAll(mList); 00738 System.out.println("JobUtils: added models"); 00739 00740 } 00741 } 00742 } 00743 } 00744 } 00745 // Now remove this file from the vector. 00746 fileList.remove(tmpfile); 00747 } 00748 00749 } 00750 00751 } else { 00752 System.out.println("Invalid root directory: "+root.getPath()); 00753 } 00754 System.out.println("JobUtils: "+modelList.size()+" Models affected"); 00755 return modelList; 00756 00757 } 00758 00762 public static Vector<LogFile> getLogFiles(int rsid) throws JetWebException { 00763 return DBJobManager.getLogFiles(rsid); 00764 } 00765 00766 private static Vector<LogFile> getLogFiles(File dir) 00767 throws JetWebException{ 00768 00769 Vector<File> fileList = new Vector<File>(); 00770 Vector<LogFile> logFiles = new Vector<LogFile>(); 00771 if(dir.isDirectory()){ 00772 fileList.add(dir); 00773 00774 while(fileList.size()>0){ 00775 Enumeration<File> list = fileList.elements(); 00776 while (list.hasMoreElements()){ 00777 00778 File tmpfile = list.nextElement(); 00779 00780 // First look for directories. 00781 if (tmpfile.isDirectory()){ 00782 00783 File[] array = tmpfile.listFiles(); 00784 int n = array.length; 00785 for (int i=0; i<n; i++){ 00786 fileList.addElement(array[i]); 00787 } 00788 00789 }else{ 00790 00791 // Now look for aida files. 00792 String tmpname = tmpfile.getPath(); 00793 00794 if ( JobUtils.isValidLogName(tmpname)) { 00795 // Now search through to see if it 00796 // terminated successfully. 00797 00798 // first, gzip it if necessary. 00799 tmpname = JobUtils.zipIt(tmpname); 00800 00801 // Instantiate, but don't add to the DB yet. 00802 LogFile log = new LogFile(tmpname); 00803 System.out.println 00804 ("JobUtils: Created log: about to fill."); 00805 System.out.println("JobUtils: Using xml file name " 00806 + log.getXmlFileName()); 00807 JetWebLogReader logReader = 00808 new JetWebLogReader(log.getXmlFileName()); 00809 00810 logReader.fillLog(log); 00811 log.checkStatus(); 00812 if (log.getStatus()==LogFile.RUN){ 00813 00814 // Now see if an aida file really exists. 00815 if (log.hasAidaFile()&& 00816 !log.isArchived()){ 00817 if(moveAndStore(log)){ 00818 logFiles.add(log); 00819 } 00820 } 00821 } 00822 } 00823 } 00824 00825 // Now remove this file from the vector. 00826 fileList.remove(tmpfile); 00827 } 00828 } 00829 } 00830 return logFiles; 00831 } 00832 00833 } 00834
Generated Wed Jan 17 09:14:27 GMT 2007