00001 package cedar.jetweb.model.plots; 00002 00003 import java.util.Enumeration; 00004 import java.util.Collection; 00005 import java.util.List; 00006 import java.util.ArrayList; 00007 import java.util.Iterator; 00008 import java.util.Vector; 00009 00010 import cedar.jetweb.model.Model; 00011 import cedar.jetweb.model.MCProcessType; 00012 import cedar.jetweb.model.paper.PaperBank; 00013 import cedar.jetweb.model.paper.Paper; 00014 import cedar.jetweb.JetWebConfig; 00015 import cedar.jetweb.job.CutCollection; 00016 00017 import cedar.jetweb.db.DBPlotManager; 00018 import cedar.jetweb.JetWebException; 00019 00026 public class PlotSelection implements Cloneable { 00027 00028 private ArrayList<DataPlot> chi2SumPlots = new ArrayList<DataPlot>(); 00029 private ArrayList<DataPlot> chi2FitPlots = new ArrayList<DataPlot>(); 00030 private ArrayList<DataPlot> requiredPlots = new ArrayList<DataPlot>(); 00031 protected int cssId=-1; 00032 private boolean initialized = false; 00033 00035 public PlotSelection (){ } 00036 00038 public PlotSelection (int newCssId) throws JetWebException { 00039 cssId=newCssId; 00040 } 00041 00042 private void initialize() throws JetWebException { 00043 00044 // Pick up the default sumplots but not the default fit plots. 00045 setSumDefault(); 00046 try { 00047 DBPlotManager.populateSelection(this); 00048 00049 } catch (JetWebException e){ 00050 throw e; 00051 } 00052 00053 // find the runseries with largest phase space (i.e. lowest PTMIN) 00054 // that has data for this fit and set the CutCollection of this 00055 00056 00057 initialized = true; 00058 } 00059 00065 public PlotSelection restrictCutCollection(CutCollection phaseSpace) 00066 throws JetWebException{ 00067 restrictCutCollection(null, phaseSpace); 00068 //checkCuts(chi2SumPlots, null, phaseSpace); 00069 //checkCuts(chi2FitPlots, null, phaseSpace); 00070 00071 // don't check required plots 00072 // because those are the plots that are necessary for a fit 00073 return this; 00074 } 00075 00081 public PlotSelection restrictCutCollection 00082 (MCProcessType proc, CutCollection phaseSpace) 00083 throws JetWebException{ 00084 if (!initialized && cssId>=0) initialize(); 00085 checkCuts(chi2SumPlots, proc, phaseSpace); 00086 checkCuts(chi2FitPlots, proc, phaseSpace); 00087 00088 return this; 00089 } 00090 00091 private static void checkCuts(ArrayList<DataPlot> plots, 00092 MCProcessType proc, 00093 CutCollection phaseSpace){ 00094 int ii=0; 00095 while(ii<plots.size()){ 00096 00097 boolean incr = true; 00098 try{ 00099 if(plots.get(ii).getMCProcessType().equals(proc) || 00100 proc==null){ 00101 00102 if(!plots.get(ii).getCutCollection().isWithin 00103 (phaseSpace)){ 00104 00105 plots.remove(ii); 00106 incr = false; 00107 } 00108 } 00109 }catch(JetWebException err){ 00110 System.out.println 00111 ("PlotSelection: unable to get process type for plot " + 00112 plots.get(ii).getId()); 00113 System.out.println(err.getStackTrace()); 00114 System.out.println(err.getMessage()); 00115 } 00116 if(incr) ++ii; 00117 } 00118 00119 return; 00120 } 00121 00122 private static void checkCuts(ArrayList<DataPlot> plots, 00123 CutCollection phaseSpace){ 00124 00125 int ii=0; 00126 while(ii<plots.size()){ 00127 00128 //if(phaseSpace.isWithin(plots.get(ii).getCutCollection())){ 00129 if(!plots.get(ii).getCutCollection().isWithin(phaseSpace)){ 00130 00131 plots.remove(ii); 00132 }else{ 00133 ++ii; 00134 } 00135 00136 } 00137 return; 00138 } 00139 00140 public Vector<Integer> getPlotIds() throws JetWebException{ 00141 00142 ArrayList<DataPlot> list = getRequiredPlots(); 00143 00144 Vector<Integer> ids = new Vector<Integer>(); 00145 00146 for(DataPlot plot: list){ 00147 Integer id = plot.getId(); 00148 if(!ids.contains(id)) ids.add(id); 00149 } 00150 00151 list = getSumPlots(); 00152 00153 for(DataPlot plot: list){ 00154 Integer id = plot.getId(); 00155 if(!ids.contains(id)) ids.add(id); 00156 } 00157 00158 list = getFitPlots(); 00159 00160 for(DataPlot plot: list){ 00161 Integer id = plot.getId(); 00162 if(!ids.contains(id)) ids.add(id); 00163 } 00164 00165 return ids; 00166 } 00167 00168 00173 public ArrayList<DataPlot> getRequiredPlots() throws JetWebException { 00174 00175 if (!initialized && cssId>=0) initialize(); 00176 00177 if (requiredPlots.size()==0){ 00178 // Build the required plots. 00179 for (DataPlot plot : chi2SumPlots){ 00180 requiredPlots.add(plot); 00181 } 00182 for (DataPlot plot : chi2FitPlots){ 00183 if (!requiredPlots.contains(plot)){ 00184 requiredPlots.add(plot); 00185 } 00186 } 00187 00188 } 00189 return requiredPlots; 00190 } 00191 public void setRequiredPlots(ArrayList<DataPlot> rp) throws JetWebException { 00192 if (!initialized && cssId>=0) initialize(); 00193 requiredPlots = rp; 00194 } 00195 00199 public ArrayList<DataPlot> getSumPlots() throws JetWebException { 00200 if (!initialized && cssId>=0) initialize(); 00201 return chi2SumPlots; 00202 } 00203 public void setSumPlots(ArrayList<DataPlot> sp) throws JetWebException { 00204 if (!initialized && cssId>=0) initialize(); 00205 chi2SumPlots = sp; 00206 } 00211 public ArrayList<DataPlot> getFitPlots() throws JetWebException { 00212 if (!initialized && cssId>=0) initialize(); 00213 return chi2FitPlots; 00214 } 00215 public void setFitPlots(ArrayList<DataPlot> fp) throws JetWebException { 00216 if (!initialized && cssId>=0) initialize(); 00217 chi2FitPlots = fp; 00218 } 00219 00221 public boolean isDefaultSum() throws JetWebException { 00222 if (!initialized && cssId>=0) initialize(); 00223 boolean isDefault = false; 00224 PlotSelection selection = new PlotSelection(); 00225 selection.setSumDefault(); 00226 if (sumEquals(selection)) 00227 isDefault = true; 00228 return isDefault; 00229 } 00230 00232 public boolean isDefaultFit() throws JetWebException { 00233 if (!initialized && cssId>=0) initialize(); 00234 PlotSelection selection = new PlotSelection(); 00235 selection.setFitDefault(); 00236 return (fitEquals(selection)); 00237 } 00238 00240 public boolean isDefault() throws JetWebException { 00241 if (!initialized && cssId>=0) initialize(); 00242 boolean isDefault = false; 00243 PlotSelection selection = new PlotSelection(); 00244 selection.setSumDefault(); 00245 selection.setFitDefault(); 00246 if (sumEquals(selection) && fitEquals(selection)) 00247 isDefault = true; 00248 return isDefault; 00249 } 00250 00251 00253 public void setFitDefault() throws JetWebException { 00254 if (!initialized && cssId>=0) initialize(); 00255 chi2FitPlots.clear(); 00256 00257 // now add the default papers to this selection. 00258 Collection<? extends DataPlot> list = PaperBank.getAllPlots(); 00259 for (DataPlot plot : list){ 00260 if (plot.isDefaultFit()) {chi2FitPlots.add(plot);} 00261 } 00262 } 00264 public void setSumDefault() throws JetWebException { 00265 00266 chi2SumPlots.clear(); 00267 00268 // now add the default papers to this selection. 00269 Collection<? extends DataPlot> list = PaperBank.getAllPlots(); 00270 for (DataPlot plot : list){ 00271 if (plot.isDefaultSum()) {chi2SumPlots.add(plot);} 00272 } 00273 } 00274 00275 00277 public boolean fitEquals(PlotSelection selection) throws JetWebException { 00278 if (!initialized && cssId>=0) initialize(); 00279 00280 //System.out.println(chi2FitPlots.size()+" , "+selection.chi2FitPlots.size()); 00281 //System.out.println(chi2FitPlots); 00282 //System.out.println(selection.getFitPlots()); 00283 00284 return (((chi2FitPlots.size()==selection.getFitPlots().size())) 00285 && (chi2FitPlots.containsAll(selection.getFitPlots()))); 00286 00287 } 00289 public boolean sumEquals(PlotSelection selection) throws JetWebException { 00290 if (!initialized && cssId>=0) initialize(); 00291 00292 boolean equals = false; 00293 00294 if (((chi2SumPlots.size()==selection.getSumPlots().size())) 00295 && (chi2SumPlots.containsAll(selection.getSumPlots()))) 00296 equals = true; 00297 00298 return equals; 00299 } 00300 00301 private List<Integer> makeFitPlotList(){ 00302 00303 List<Integer> plotList= new ArrayList<Integer>(); 00304 for (DataPlot plot : chi2FitPlots){ 00305 plotList.add(plot.getId()); 00306 } 00307 00308 return plotList; 00309 } 00310 private List<Integer> makeSumPlotList(){ 00311 List<Integer> plotList= new ArrayList<Integer>(); 00312 for (DataPlot plot : chi2SumPlots){ 00313 plotList.add(plot.getId()); 00314 } 00315 return plotList; 00316 } 00317 00326 public int getId() throws JetWebException { 00327 00328 if (cssId<0) { 00329 00330 // get list of all csnIds for this plotSelection 00331 List<Integer> fitPlotList = makeFitPlotList(); 00332 Iterator<Integer> cssIds = null; 00333 try { 00336 cssIds = DBPlotManager.getCSSIds((fitPlotList.get(0)).intValue(),false).iterator(); 00337 } catch (JetWebException e){ 00338 throw e; 00339 } catch (Exception e){ 00340 throw new JetWebException(e,"Attempt to read from Empty Plot Selection"); 00341 } 00342 00343 boolean found = false; 00344 while (cssIds.hasNext() && !found){ 00345 00346 int localCssId = ((Integer)cssIds.next()).intValue(); 00347 List<Integer> csnIdList = DBPlotManager.getCsnIds(localCssId,false); 00348 00349 //test whether set of csnIds for this Plot Selection 00350 // is same as set of csnIds for localCssId 00351 00352 if ((fitPlotList.size() == csnIdList.size()) && 00353 (csnIdList.containsAll(fitPlotList))){ 00354 00355 cssId = localCssId; 00356 found = true; 00357 } 00358 } 00359 00360 if (!found){ 00361 System.out.println("No matching css found"); 00362 // No matching set found. Make a new one. 00363 cssId = DBPlotManager.getMaxCssId(); 00364 //System.out.println("Max cssId is "+cssId); 00365 DBPlotManager.insertCrossSectionSet(fitPlotList.iterator(),++cssId); 00366 System.out.println("PlotSelection: Allocated cssId is "+cssId); 00367 } 00368 } 00369 00370 return cssId; 00371 } 00372 00381 public void setSumToBest(Model model) throws JetWebException { 00382 if (!initialized && cssId>=0) initialize(); 00383 00384 chi2SumPlots.clear(); 00385 00386 for (Paper dataPaper : PaperBank.getPapers()){ 00387 for (DataPlot plot : dataPaper.getPlots()){ 00388 if (plot.isDefaultSum() && model.predicts(plot)){ 00389 chi2SumPlots.add(plot); 00390 } 00391 } 00392 } 00393 00394 } 00395 00396 00405 public void setFitToBest(Model model) throws JetWebException { 00406 if (!initialized && cssId>=0) initialize(); 00407 00408 chi2FitPlots.clear(); 00409 00410 for (Paper dataPaper : PaperBank.getPapers()){ 00411 for (DataPlot plot : dataPaper.getPlots()){ 00412 try { 00413 if (plot.isDefaultFit() && model.predicts((RealPlot)plot)){ 00414 chi2FitPlots.add(plot); 00415 } 00416 } catch (ClassCastException e){ 00417 System.out.println("Attempt to cast RealPlot, for paper id " 00418 + dataPaper.getId() ); 00419 } 00420 } 00421 } 00422 00423 } 00424 00431 public boolean isAvailable(Model model) throws JetWebException { 00432 if (!initialized && cssId>=0) initialize(); 00433 00434 for (DataPlot plot : getRequiredPlots()){ 00435 if ( !model.predicts((RealPlot)plot) ){ return false; } 00436 } 00437 return true; 00438 } 00439 00443 public Object clone() { 00444 00445 PlotSelection ps = new PlotSelection(); 00446 00447 try { 00448 if (!initialized && cssId>=0) initialize(); 00449 00450 00451 ArrayList<DataPlot> rp = (ArrayList<DataPlot>)chi2FitPlots.clone(); 00452 ps.setFitPlots(rp); 00453 ArrayList<DataPlot> sp = (ArrayList<DataPlot>)chi2SumPlots.clone(); 00454 ps.setSumPlots(sp); 00455 ArrayList<DataPlot> qp = (ArrayList<DataPlot>)requiredPlots.clone(); 00456 ps.setRequiredPlots(qp); 00457 00458 ps.cssId=cssId; 00459 } catch (JetWebException jwe) { 00460 jwe.printStackTrace(System.out); 00461 } 00462 00463 return ps; 00464 } 00465 00466 00470 public void requireLatest() throws JetWebException { 00471 if (!initialized && cssId>=0) initialize(); 00472 // now add the default papers to this selection. 00473 for (DataPlot plot: PaperBank.getAllPlots()){ 00474 chi2FitPlots.add(plot); 00475 } 00476 } 00477 00481 public void requireLatest(MCProcessType proc) throws JetWebException { 00482 if (!initialized && cssId>=0) initialize(); 00483 System.out.println("PlotSelection.requireLatest(proc) not yet implemented."); 00484 } 00485 00489 public void requirePaper(String identifier) throws JetWebException { 00490 if (!initialized && cssId>=0) initialize(); 00491 00492 // now add the default papers to this selection. 00493 for (Paper paper : PaperBank.getPapers()){ 00494 if (paper.getDirName().equals(identifier)){ 00495 chi2FitPlots.addAll( paper.getPlots() ); 00496 } 00497 } 00498 } 00499 00500 public boolean fitIsEmpty() throws JetWebException { 00501 if (!initialized && cssId>=0) initialize(); 00502 return (chi2FitPlots.size()==0); 00503 00504 } 00505 public boolean sumIsEmpty() throws JetWebException { 00506 if (!initialized && cssId>=0) initialize(); 00507 return (chi2SumPlots.size()==0); 00508 00509 } 00510 00514 public String toString() { 00515 StringBuffer strbuff = new StringBuffer(); 00516 try { 00517 if (!initialized && cssId>=0) initialize(); 00518 for (DataPlot plot : getRequiredPlots()){ 00519 strbuff.append("Plot "+plot.getId()+": "+plot.getTitle()); 00520 strbuff.append("\n"); 00521 } 00522 } catch (JetWebException jwe){ 00523 jwe.printStackTrace(System.out); 00524 } 00525 return strbuff.toString(); 00526 } 00527 00528 00529 public void addFitPlot(DataPlot plot){ 00530 chi2FitPlots.add(plot); 00531 } 00532 00533 public void clearFitPlots(){ 00534 chi2FitPlots.clear(); 00535 } 00536 00537 00538 }// PlotSelection 00539 00540 00541 00542 00543 00544 00545 00546 00547 00548
Generated Wed Jan 17 09:14:27 GMT 2007