00001 package cedar.jetweb.generator; 00002 00003 import java.util.HashMap; 00004 import java.util.Vector; 00005 import java.util.HashSet; 00006 import java.util.Iterator; 00007 import java.util.Set; 00008 import java.util.Map; 00009 import java.util.Map.Entry; 00010 import java.util.Random; 00011 import java.util.Date; 00012 import java.lang.Math; 00013 00014 import java.io.BufferedWriter; 00015 import java.io.IOException; 00016 00017 import java.text.NumberFormat; 00018 import java.text.SimpleDateFormat; 00019 00020 import cedar.jetweb.model.MCProcessType; 00021 import cedar.jetweb.model.Model; 00022 import cedar.jetweb.model.PDF; 00023 import cedar.jetweb.model.Collision; 00024 import cedar.jetweb.model.plots.DataPlot; 00025 import cedar.jetweb.model.paper.*; 00026 import cedar.jetweb.db.DBGeneratorManager; 00027 import cedar.jetweb.JetWebException; 00028 import cedar.jetweb.JetWebConfig; 00029 import cedar.jetweb.util.Match; 00030 import cedar.jetweb.job.CutCollection; 00031 import cedar.jetweb.job.Cut; 00032 00037 public abstract class Generator { 00038 00039 private static Set<String> nameList; 00040 00041 protected HashMap<String,Integer> intParameters = 00042 new HashMap<String,Integer>(); 00043 protected HashMap<String,Double> realParameters = 00044 new HashMap<String,Double>(); 00045 protected HashMap<String,HashMap<Integer,Integer>> intParameterArrays = 00046 new HashMap<String,HashMap<Integer,Integer>>(); 00047 protected HashMap<String,HashMap<Integer,Double>> realParameterArrays = 00048 new HashMap<String,HashMap<Integer,Double>>(); 00049 00050 protected Set<String> realArrayNames; 00051 protected Set<String> realNames; 00052 protected Set<String> intArrayNames; 00053 protected Set<String> intNames; 00054 00055 protected String generatorVersion; 00056 protected String generatorName; 00057 protected int generatorId; 00058 00059 private static HashMap<String, Boolean> relevantPhoton_ = 00060 new HashMap<String, Boolean>(); 00061 00062 private static HashMap<String, Boolean> relevantProton_ = 00063 new HashMap<String, Boolean>(); 00064 00065 private static HashMap<String, HashMap<Integer, Boolean>> 00066 relevantPhotonArrays_ = 00067 new HashMap<String, HashMap<Integer, Boolean>>(); 00068 00069 private static HashMap<String, HashMap<Integer, Boolean>> 00070 relevantProtonArrays_ = 00071 new HashMap<String, HashMap<Integer, Boolean>>(); 00072 00073 //Collection of cuts on phase space 00074 protected CutCollection cuts_; 00075 protected boolean dirtyCuts_ = true; 00076 00080 public static Generator Maker(int id) 00081 throws JetWebException { 00082 00083 String[] nameAndVersion = DBGeneratorManager.getNameAndVersion(id); 00084 Generator gen = Maker(nameAndVersion[0],nameAndVersion[1]); 00085 gen.setGeneratorId(id); 00086 return gen; 00087 00088 } 00092 public static Generator Maker(String name,String version) 00093 throws JetWebException { 00094 00095 if (name==null) { 00096 name = defaultName(); 00097 } 00098 if (version==null){ 00099 version = defaultVersion(name); 00100 } 00101 00102 Generator gen = null; 00103 if (name.equalsIgnoreCase("herwig")){ 00104 return new Herwig(version); 00105 } else if (name.equalsIgnoreCase("pythia")){ 00106 return new Pythia(version); 00107 } else { 00108 throw new JetWebException("Unknown generator",name); 00109 } 00110 00111 } 00115 public static Generator Maker() 00116 throws JetWebException { 00117 String name = defaultName(); 00118 String version = defaultVersion(name); 00119 return Maker(name,version); 00120 } 00124 public HashMap<String,Integer> getIntParameters(){ 00125 return intParameters; 00126 } 00130 public HashMap<String,Double> getRealParameters(){ 00131 dirtyCuts_ = true; 00132 return realParameters; 00133 } 00137 public HashMap<String,HashMap<Integer,Integer>> getIntParameterArrays(){ 00138 return intParameterArrays; 00139 } 00143 public HashMap<String,HashMap<Integer,Double>> getRealParameterArrays(){ 00144 dirtyCuts_ = true; 00145 return realParameterArrays; 00146 } 00147 00148 /* 00149 public Generator setCollision(Collision collision){ 00150 dirtyProcessType_ = true; 00151 collision_ = collision; 00152 return(this); 00153 } 00154 */ 00155 00156 00161 public Generator setIntParameters(HashMap<String, Integer> parm){ 00162 00163 intParameters = parm; 00164 00165 return this; 00166 } 00167 00168 00173 public Generator setRealParameters(HashMap<String, Double> parm){ 00174 00175 realParameters = parm; 00176 dirtyCuts_ = true; 00177 return this; 00178 } 00179 00180 00185 public Generator setIntParameterArrays 00186 (HashMap<String, HashMap<Integer, Integer>> parm){ 00187 intParameterArrays = parm; 00188 return this; 00189 } 00190 00195 public Generator setRealParameterArrays 00196 (HashMap<String, HashMap<Integer, Double>> parm){ 00197 realParameterArrays = parm; 00198 dirtyCuts_ = true; 00199 return this; 00200 } 00201 00208 public void setParameter(String name, int value) throws JetWebException { 00209 if (getIntNames().contains(name)){ 00210 if (intParameters.containsKey(name)){ 00211 intParameters.remove(name); 00212 } 00213 intParameters.put(name,value); 00214 00215 //in principle, changing a parameter could change the process type 00216 //dirtyProcessType_ = true; 00217 00218 } else { 00219 throw new JetWebException("Attempt to set illegal parameter"+name, 00220 generatorName+generatorVersion); 00221 00222 } 00223 } 00224 00231 public void setParameter(String name, double value) throws JetWebException { 00232 dirtyCuts_ = true; 00233 if (getRealNames().contains(name)){ 00234 if (realParameters.containsKey(name)){ 00235 realParameters.remove(name); 00236 } 00237 realParameters.put(name,value); 00238 00239 //in principle, changing a parameter could change the process type 00240 //dirtyProcessType_ = true; 00241 00242 } else { 00243 throw new JetWebException("Attempt to set illegal parameter"+name, 00244 generatorName+generatorVersion); 00245 } 00246 } 00247 00248 00255 public void setRealElement(final String name, final int index, final Double value) 00256 throws JetWebException { 00257 dirtyCuts_ = true; 00258 HashMap<Integer,Double> array = realParameterArrays.get(name); 00259 if (array==null) { 00260 if (getRealArrayNames().contains(name)){ 00261 array = new HashMap<Integer,Double>(); 00262 realParameterArrays.put(name,array); 00263 00264 } else { 00265 throw new ParameterNotFoundException("Could not find array", 00266 generatorName+generatorVersion,name); 00267 } 00268 } 00269 if (index>getMaxIndex(name) || index<getMinIndex(name)) { 00270 throw new ParameterNotFoundException("Index out of range", 00271 generatorName+generatorVersion,name,index); 00272 } 00273 if (array.containsKey(index)) { 00274 array.remove(index); 00275 } 00276 array.put(index,value); 00277 //dirtyProcessType_ = true; 00278 } 00285 public void setIntElement(final String name, final int index, final int value) 00286 throws JetWebException { 00287 00288 HashMap<Integer,Integer> array = intParameterArrays.get(name); 00289 if (array==null) { 00290 if (getIntArrayNames().contains(name)){ 00291 array = new HashMap<Integer,Integer>(); 00292 intParameterArrays.put(name,array); 00293 } else { 00294 throw new ParameterNotFoundException("Could not find array", 00295 generatorName+generatorVersion,name); 00296 } 00297 } 00298 if (index>getMaxIndex(name) || index<getMinIndex(name)) { 00299 throw new ParameterNotFoundException("Index out of range", 00300 generatorName+generatorVersion,name); 00301 } 00302 if (array.containsKey(index)) { 00303 array.remove(index); 00304 } 00305 array.put(index,value); 00306 //dirtyProcessType_ = true; 00307 } 00308 00312 // public abstract String getMCProcessName(HashMap<String,HashMap<Integer,Integer>> parameters) 00313 public abstract String getMCProcessName(HashMap<String, Object> parameters) 00314 throws JetWebException; 00322 public abstract String getProcessSelector(MCProcessType proc); 00323 00334 public boolean processTrumps(Generator gen){ 00335 return false; 00336 } 00337 00341 public int getMaxIndex(String name) throws JetWebException { 00342 return DBGeneratorManager.getMaxIndex(generatorName,generatorVersion,name); 00343 } 00347 public int getMinIndex(String name) throws JetWebException { 00348 return DBGeneratorManager.getMinIndex(generatorName,generatorVersion,name); 00349 } 00350 00354 public String toString(){ 00355 return getName()+"-"+getVersion(); 00356 } 00357 00361 public String getName(){ 00362 return generatorName; 00363 } 00367 public String getVersion(){ 00368 return generatorVersion; 00369 } 00373 public void setSoftDefaults() throws JetWebException { 00374 DBGeneratorManager.selectDefaultParameters(this,false); 00375 //dirtyProcessType_ = true; 00376 } 00380 public void setDefaults() throws JetWebException { 00381 DBGeneratorManager.selectDefaultParameters(this,true); 00382 //dirtyProcessType_ = true; 00383 } 00384 00389 public abstract Generator addCut(Cut cut); 00390 00394 public Generator addCutCollection(CutCollection cutCollection){ 00395 for(Cut cut: cutCollection.getCuts()){ 00396 addCut(cut); 00397 } 00398 return this; 00399 } 00400 00406 public Generator setCutCollection(CutCollection cutCollection){ 00407 cuts_ = new CutCollection(); 00408 addCutCollection(cutCollection); 00409 return this; 00410 } 00411 00412 public abstract CutCollection getCutCollection(); 00413 00417 public abstract Double getPthat(); 00418 00424 public abstract boolean checkParameters(); 00425 00429 public Set<String> getRealArrayNames() throws JetWebException { 00430 if (realArrayNames==null){ 00431 realArrayNames = DBGeneratorManager.selectRealArrayNames(generatorName,generatorVersion); 00432 } 00433 return realArrayNames; 00434 } 00435 00439 public Set<String> getRealNames() throws JetWebException { 00440 if (realNames==null){ 00441 realNames = DBGeneratorManager.selectRealNames(generatorName,generatorVersion); 00442 } 00443 return realNames; 00444 } 00445 00449 public Set<String> getIntArrayNames() throws JetWebException { 00450 if (intArrayNames==null){ 00451 intArrayNames = DBGeneratorManager.selectIntArrayNames(generatorName,generatorVersion); 00452 } 00453 return intArrayNames; 00454 } 00455 00459 public Set<String> getIntNames() throws JetWebException { 00460 if (intNames==null){ 00461 intNames = DBGeneratorManager.selectIntNames(generatorName,generatorVersion); 00462 } 00463 return intNames; 00464 } 00465 00466 00467 // Static methods. 00471 public static String defaultName() throws JetWebException { 00472 return allNames().iterator().next(); 00473 } 00474 00478 public static Set<String> allNames() throws JetWebException { 00479 if (nameList==null){ 00480 nameList = DBGeneratorManager.allNames(); 00481 } 00482 return nameList; 00483 } 00484 00488 public static Set<String> allVersions(String name) throws JetWebException { 00489 return DBGeneratorManager.getVersions(name); 00490 } 00491 00495 public static String defaultVersion(String name) throws JetWebException { 00496 return DBGeneratorManager.getDefaultVersion(name); 00497 } 00498 00503 protected abstract String getParticleName(String pName); 00504 00508 public abstract void writeSteering(BufferedWriter newCards, 00509 Model model, MCProcessType proc) 00510 throws JetWebException; 00511 00515 protected void writeCommonFFKey(BufferedWriter newCards, 00516 MCProcessType proc) 00517 throws JetWebException, IOException { 00518 00519 newCards.write("C HZSteer control cards for "+this); 00520 newCards.newLine(); 00521 newCards.write("C\n"); 00522 00523 SimpleDateFormat df = 00524 new SimpleDateFormat("HH:mm:ss dd/MM/yyyy"); 00525 Date now = new Date(); 00526 newCards.write("C Created by JetWeb at "+df.format(now)); 00527 newCards.newLine(); 00528 00529 newCards.write("C These cards will generate "+proc.getMCProcessName()); 00530 newCards.newLine(); 00531 00532 newCards.write("C\n"); 00533 00534 newCards.write("C Start of generator independent FFKEY style steering. "); 00535 newCards.newLine(); 00536 00537 newCards.write("C\n"); 00538 00539 newCards.write("C Tell FFREAD to print out steering cards"); 00540 newCards.newLine(); 00541 newCards.write("LIST"); 00542 newCards.newLine(); 00543 00544 // Turn on XML output 00545 newCards.write("C Turn on XML output.\n"); 00546 newCards.write("XML 1 "); 00547 newCards.newLine(); 00548 00549 // Turn on AIDA output 00550 newCards.write("C Turn on AIDA output.\n"); 00551 newCards.write("AIDA 1 "); 00552 newCards.newLine(); 00553 00554 // Specify the luminosity and maximum number of events. 00555 newCards.write("C Absolute maximum number of events\n"); 00556 newCards.write("C Enforced if the lumi is too high.\n"); 00557 newCards.write("NEV 2000000"); 00558 newCards.newLine(); 00559 newCards.write("C "); 00560 newCards.write("C Requested lumi in inv.pb\n"); 00561 newCards.write("LUMI "+proc.getDefaultLumi()); 00562 newCards.newLine(); 00563 00564 // Choose beams. 00565 newCards.write("C Specify the beams. BEAM 1 has positive momentum."); 00566 newCards.newLine(); 00567 // newCards.write("BEAM1 '"+proc.getCollision().getParticle1().getName()+"'"); 00568 00569 newCards.write("BEAM1 '" + 00570 getParticleName 00571 (proc.getCollision().getParticle1().getName()) + 00572 "'"); 00573 00574 newCards.newLine(); 00575 00576 //newCards.write("BEAM2 '"+proc.getCollision().getParticle2().getName()+"'"); 00577 newCards.write("BEAM2 '" + 00578 getParticleName 00579 (proc.getCollision().getParticle2().getName()) + 00580 "'"); 00581 00582 newCards.newLine(); 00583 00584 newCards.write("PBEAM1 "+proc.getCollision().getParticle1().getEnergy()); 00585 newCards.newLine(); 00586 newCards.write("PBEAM2 "+proc.getCollision().getParticle2().getEnergy()); 00587 newCards.newLine(); 00588 00589 newCards.write("C\n"); 00590 00591 // Random number seeds. 00592 newCards.write("C Random number seeds\n"); 00593 newCards.write("RANDNUM1 "+JetWebConfig.random.nextInt(1000000)); 00594 newCards.newLine(); 00595 newCards.write("RANDNUM2 "+JetWebConfig.random.nextInt(1000000)); 00596 newCards.newLine(); 00597 00598 newCards.write("C\n"); 00599 newCards.write("C End of generator independent FFKEY style steering. "); 00600 newCards.write("C\n"); 00601 00602 } 00603 00607 protected void writePDFFFKey(BufferedWriter newCards, Model model, 00608 MCProcessType proc) 00609 throws JetWebException, IOException { 00610 00611 newCards.write("C "); 00612 newCards.newLine(); 00613 00614 // Choose photon pdf 00615 if (proc.hasPhoton()){ 00616 newCards.write("C Photon parton density " + 00617 model.getPhotonPDF().getName()); 00618 // JetWebConfig.photonpdfName.get 00619 // (model.getPhotonPDF())); 00620 //newCards.write("C Photon parton density "+ 00621 // model.getPhotonPDF()); 00622 newCards.newLine(); 00623 newCards.write("PDFPH "+model.getPhotonPDF().getMember()); 00624 //newCards.write("PDFPH " + 00625 // JetWebConfig.photonpdfName.get 00626 // (model.getPhotonPDF())); 00627 newCards.newLine(); 00628 } 00629 // Choose proton pdf 00630 00631 if (proc.hasProton()){ 00632 newCards.write("C Proton parton density " + 00633 model.getProtonPDF().getName()); 00634 //JetWebConfig.protonpdfName.get 00635 //(model.getProtonPDF())); 00636 //newCards.write("C Proton parton density " + 00637 // model.getProtonPDF()); 00638 newCards.newLine(); 00639 newCards.write("PDFPR " + model.getProtonPDF().getMember()); 00640 //newCards.write("PDFPR " +JetWebConfig.protonpdfName.get 00641 // (model.getProtonPDF())); 00642 newCards.newLine(); 00643 } 00644 00645 newCards.write("C "); 00646 newCards.newLine(); 00647 00648 } 00652 protected void writeGenFFKey(BufferedWriter newCards, MCProcessType proc) 00653 throws JetWebException, IOException { 00654 00655 NumberFormat form = JetWebConfig.getFormat(); 00656 00657 newCards.write("C\n"); 00658 newCards.write("C Start of generator specific parameters for " 00659 +getName()+"-"+getVersion()); 00660 newCards.newLine(); 00661 00662 newCards.write("C\n"); 00663 00664 // Now choose the process type 00665 newCards.write("C Cards to select "+proc.getMCProcessName()); 00666 newCards.newLine(); 00667 //Vector<Integer> pIds = proc.getCompatibleProcesses(); 00668 //for(Integer pId: pIds){ 00669 //MCProcessType p = new MCProcessType(pId); 00670 newCards.write(getProcessSelector(proc)); 00671 newCards.newLine(); 00672 newCards.write("C\n"); 00673 //} 00674 00675 // First the integer parameters. 00676 newCards.write("C Integer parameters\n"); 00677 for (Map.Entry<String,Integer> entry : intParameters.entrySet()){ 00678 newCards.write(entry.getKey()+" "+entry.getValue()); 00679 newCards.newLine(); 00680 newCards.write("C"); 00681 newCards.newLine(); 00682 } 00683 00684 // Now the the real parameters. 00685 newCards.write("C Real parameters\n"); 00686 for (Map.Entry<String,Double> entry : realParameters.entrySet()){ 00687 newCards.write(entry.getKey()+" "+form.format(entry.getValue())); 00688 newCards.newLine(); 00689 } 00690 newCards.write("C\n"); 00691 newCards.write("C Integer parameter arrays\n"); 00692 00693 // Now the integer arrays. 00694 for (Map.Entry<String,HashMap<Integer,Integer>> entry : intParameterArrays.entrySet()){ 00695 00696 String arrayName = entry.getKey(); 00697 00698 Set<Map.Entry<Integer,Integer>> array = entry.getValue().entrySet(); 00699 for (Map.Entry<Integer,Integer> arrayEntry : array){ 00700 Integer value = arrayEntry.getValue(); 00701 Integer index = arrayEntry.getKey(); 00702 newCards.write(arrayName.toUpperCase()+" "+index+" = "+value); 00703 newCards.newLine(); 00704 } 00705 } 00706 00707 00708 00709 // Now the real arrays. 00710 newCards.write("C Real parameter arrays\n"); 00711 for (Map.Entry<String,HashMap<Integer,Double>> entry : realParameterArrays.entrySet()){ 00712 00713 String arrayName = entry.getKey(); 00714 00715 Set<Map.Entry<Integer,Double>> array = entry.getValue().entrySet(); 00716 for (Map.Entry<Integer,Double> arrayEntry : array){ 00717 Double value = arrayEntry.getValue(); 00718 Integer index = arrayEntry.getKey(); 00719 newCards.write(arrayName.toUpperCase()+" "+index+" = "+form.format(value)); 00720 newCards.newLine(); 00721 } 00722 } 00723 } 00727 protected void writePapersFFKey(BufferedWriter newCards, MCProcessType proc) 00728 throws JetWebException, IOException { 00729 00730 newCards.write("C\n"); 00731 newCards.write("C Start turning on HZTool Routines\n"); 00732 newCards.write("C\n"); 00733 00734 Vector<Integer> writtenPapers = new Vector<Integer>(); 00735 00736 for(Paper paper: PaperBank.getPapers()){ 00737 00738 if(!writtenPapers.contains(paper.getId())){ 00739 for(DataPlot plot: paper.getPlots()){ 00740 int pIdWanted = plot.getProcIdWanted(this); 00741 00742 // only add a plot if this generator has suitable 00743 // kinematic cuts. 00744 CutCollection cuts = plot.getCutCollection(); 00745 if(cuts==null || // if plot does not have any cuts 00746 // always add it 00747 cuts.isWithin(getCutCollection())){ 00748 00749 if(pIdWanted==proc.getId()&& 00750 !writtenPapers.contains(paper.getId())){ 00751 writtenPapers.add(paper.getId()); 00752 }else if(!writtenPapers.contains(paper.getId())){ 00753 00754 MCProcessType tmpProc = 00755 new MCProcessType(pIdWanted); 00756 00757 if(tmpProc.isValid()){ 00758 Vector<Integer> compatPIds = 00759 tmpProc.getCompatibleProcesses(); 00760 for(Integer pId: compatPIds){ 00761 00762 if(pId.intValue()==proc.getId()&& 00763 !writtenPapers.contains(paper.getId())){ 00764 writtenPapers.add(paper.getId()); 00765 } 00766 } 00767 } 00768 } 00769 } 00770 } 00771 } 00772 } 00773 00774 for(Integer paperId: writtenPapers){ 00775 newCards.write("ROUTINE "+paperId+"= TRUE\n"); 00776 } 00777 /* 00778 for (Paper paper : PaperBank.getPapers()){ 00779 for (DataPlot plot : paper.getPlotCollection()){ 00780 00781 int pIdWanted = plot.getProcIdWanted(this); 00782 00783 if (pIdWanted==proc.getId() && 00784 !writtenProcs.contains(pIdWanted)){ 00785 newCards.write("ROUTINE "+paper.getId()+"= TRUE\n"); 00786 writtenProcs.add(pIdWanted); 00787 break; 00788 } 00789 00790 MCProcessType tmpProc = new MCProcessType(pIdWanted); 00791 Vector<Integer> compatPIds = tmpProc.getCompatibleProcesses(); 00792 00793 for(Integer pId: compatPIds){ 00794 if(pId.intValue()==proc.getId() && 00795 !writtenProcs.contains(pId)){ 00796 newCards.write("ROUTINE "+paper.getId()+"= TRUE\n"); 00797 writtenProcs.add(pId); 00798 break; 00799 } 00800 } 00801 00802 } 00803 } 00804 */ 00805 newCards.write("C\n"); 00806 newCards.write("C End turning on HZTool Routines\n"); 00807 newCards.write("C\n"); 00808 00809 } 00810 00818 public boolean inModel(String pName){ 00819 return true; 00820 } 00821 00829 public boolean inModel(String aName, Integer index){ 00830 return true; 00831 } 00832 00840 public boolean inProcessType(String pName){ 00841 return false; 00842 } 00843 00850 public boolean inCuts(String pName){ 00851 return false; 00852 } 00853 00859 public boolean hasConsistentModel(Generator gen){ 00860 00861 System.out.println("called hasConsistentModel"); 00862 00863 return consistentParams(gen, true); 00864 } 00865 00874 private <T1, T2 extends Comparable> boolean consistentParams 00875 (HashMap<T1, T2> parms1, 00876 HashMap<T1, T2> parms2, 00877 Generator gen, 00878 String name, boolean checkModel){ 00879 00880 boolean isArray = name!=null; 00881 00882 for(Map.Entry<T1, T2> entry: parms2.entrySet()){ 00883 T2 parm = parms1.get(entry.getKey()); 00884 if(parm!=null){ 00885 boolean check = true; 00886 if(checkModel){ 00887 if(isArray){ 00888 check = 00889 (inModel(name, (Integer)entry.getKey()) && 00890 gen.inModel(name, (Integer)entry.getKey()))|| 00891 this.inCuts(name); 00892 }else{ 00893 String tmp = (String)entry.getKey(); 00894 check = (inModel((String)entry.getKey()) && 00895 gen.inModel((String)entry.getKey()))|| 00896 this.inCuts(tmp); 00897 } 00898 } 00899 00900 if(check){ 00901 //if(parm.compareTo(entry.getValue())!=0){ 00902 00903 //compareTo doesn't seem to work as it should :( 00904 00905 if(!Match.compare(entry.getValue(), parm)){ 00906 String aName; 00907 00908 if(isArray){ 00909 aName = name + "(" + entry.getKey() + ")"; 00910 }else{ 00911 aName = (String)entry.getKey(); 00912 } 00913 00914 System.out.println("Generator compare: Failed on " + 00915 aName + ": " + 00916 entry.getValue() + 00917 " != " + parm); 00918 return false; 00919 } 00920 } 00921 } 00922 } 00923 00924 return true; 00925 } 00926 00927 private boolean consistentParams(Generator gen, boolean checkModel){ 00928 00929 boolean consistent = true; 00930 consistent = consistentParams(intParameters, 00931 gen.getIntParameters(), 00932 gen, 00933 null, 00934 checkModel); 00935 if(!consistent) return consistent; 00936 00937 consistent = consistentParams(realParameters, 00938 gen.getRealParameters(), 00939 gen, 00940 null, 00941 checkModel); 00942 if(!consistent) return consistent; 00943 00944 for(Map.Entry<String, HashMap<Integer, Integer>> entry: 00945 gen.getIntParameterArrays().entrySet()){ 00946 String arrayName = entry.getKey(); 00947 HashMap<Integer, Integer> otherArray = 00948 intParameterArrays.get(arrayName); 00949 if(otherArray != null){ 00950 consistent = consistentParams(otherArray, 00951 entry.getValue(), 00952 gen, 00953 entry.getKey(), 00954 checkModel); 00955 if(!consistent)return consistent; 00956 } 00957 } 00958 00959 for(Map.Entry<String, HashMap<Integer, Double>>entry: 00960 gen.getRealParameterArrays().entrySet()){ 00961 String arrayName = entry.getKey(); 00962 HashMap<Integer, Double> otherArray = 00963 realParameterArrays.get(arrayName); 00964 if(otherArray != null){ 00965 consistent = consistentParams(otherArray, 00966 entry.getValue(), 00967 gen, 00968 entry.getKey(), 00969 checkModel); 00970 if(!consistent) return consistent; 00971 } 00972 } 00973 00974 return consistent; 00975 } 00976 00977 00984 public boolean isConsistentWith(Generator gen){ 00985 00986 return consistentParams(gen, false); 00987 /* 00988 String pName; 00989 NumberFormat form = JetWebConfig.getFormat(); 00990 00991 for (Map.Entry<String,Integer> entry : 00992 gen.getIntParameters().entrySet()){ 00993 00994 Integer iParam = intParameters.get(entry.getKey()); 00995 if (iParam!=null && !iParam.equals(entry.getValue())){ 00996 System.out.println("Generator compare: Failed on " + 00997 entry.getKey() + 00998 ": "+entry.getValue()+" != "+iParam); 00999 return false; 01000 } 01001 01002 } 01003 01004 for (Map.Entry<String,Double> entry : gen.getRealParameters().entrySet()){ 01005 01006 Double rParam = realParameters.get(entry.getKey()); 01007 01008 01009 if (rParam!=null && entry.getValue()!=null){ 01010 01011 01012 /* 01013 Double diff = rParam - entry.getValue(); 01014 boolean equal = 01015 Math.abs(diff) <= (rParam+entry.getValue()) * 0.00000005 || 01016 rParam==entry.getValue(); 01017 */ 01018 /* 01019 if(rParam!=null && !Match.compareDouble(rParam,entry.getValue())){ 01020 01021 System.out.println("Generator compare: Failed on " + 01022 entry.getKey() + 01023 ": " + 01024 form.format(entry.getValue()) + 01025 " != " + 01026 form.format(rParam)); 01027 01028 return false; 01029 } 01030 } 01031 } 01032 01033 // Now the integer arrays. 01034 for (Map.Entry<String,HashMap<Integer,Integer>> entry : gen.getIntParameterArrays().entrySet()){ 01035 01036 String arrayName = entry.getKey(); 01037 HashMap<Integer,Integer> otherArray = intParameterArrays.get(arrayName); 01038 if (otherArray != null) { 01039 01040 Set<Map.Entry<Integer,Integer>> array = entry.getValue().entrySet(); 01041 for (Map.Entry<Integer,Integer> arrayEntry : array){ 01042 Integer index = arrayEntry.getKey(); 01043 Integer iParam = otherArray.get(index); 01044 if (iParam!=null && !iParam.equals(arrayEntry.getValue())){ 01045 System.out.println 01046 ("Generator compare: Failed on " + 01047 arrayName+index + 01048 ": " + 01049 arrayEntry.getValue() + 01050 " != "+iParam); 01051 return false; 01052 } 01053 } 01054 } 01055 } 01056 01057 // Now the real arrays. 01058 for (Map.Entry<String,HashMap<Integer,Double>> entry : gen.getRealParameterArrays().entrySet()){ 01059 01060 String arrayName = entry.getKey(); 01061 HashMap<Integer,Double> otherArray = realParameterArrays.get(arrayName); 01062 if (otherArray != null) { 01063 01064 Set<Map.Entry<Integer,Double>> array = entry.getValue().entrySet(); 01065 for (Map.Entry<Integer,Double> arrayEntry : array){ 01066 Integer index = arrayEntry.getKey(); 01067 Double rParam = otherArray.get(index); 01068 01069 01070 if(rParam!=null && arrayEntry.getValue()!=null && 01071 !Match.compareDouble(rParam,arrayEntry.getValue())){ 01072 01073 System.out.println("failed on "+arrayName); 01074 System.out.println("rParam = "+rParam); 01075 System.out.println("entry = "+arrayEntry.getValue()); 01076 01077 return false; 01078 } 01079 } 01080 } 01081 } 01082 01083 return true; 01084 */ 01085 } 01086 01087 public Generator union(Generator generator) 01088 throws JetWebException{ 01089 01090 Generator gen = null; 01091 dirtyCuts_ = true; 01092 //if(!this.isConsistentWith(generator)){ 01093 if(!this.hasConsistentModel(generator)){ 01094 return gen; 01095 } 01096 01097 gen = Maker(this.generatorName, this.generatorVersion); 01098 01099 HashMap<String, Integer> commonInts = 01100 new HashMap<String, Integer> (this.intParameters); 01101 HashMap<String, Double> commonReals = 01102 new HashMap<String, Double> (this.realParameters); 01103 01104 for(Map.Entry<String, Integer> entry: 01105 generator.getIntParameters().entrySet()){ 01106 String name = entry.getKey(); 01107 Integer val = entry.getValue(); 01108 if(generator.inModel(name)){ 01109 commonInts.put(name, val); 01110 } 01111 01112 if(generator.inProcessType(name) && generator.processTrumps(this)){ 01113 commonInts.put(name, val); 01114 } 01115 01116 } 01117 01118 commonReals.putAll(generator.getRealParameters()); 01119 01120 HashMap<String, HashMap<Integer, Integer>> commonIntArrays = 01121 new HashMap<String, HashMap<Integer, Integer>> 01122 (this.intParameterArrays); 01123 01124 HashMap<String, HashMap<Integer, Double>> commonRealArrays = 01125 new HashMap<String, HashMap<Integer, Double>> 01126 (this.realParameterArrays); 01127 01128 for(Map.Entry entry: generator.getIntParameterArrays().entrySet()){ 01129 if(commonIntArrays.containsKey(entry.getKey())){ 01130 ((HashMap<Integer, Integer>) 01131 commonIntArrays.get(entry.getKey())).putAll 01132 ((HashMap<Integer, Integer>)entry.getValue()); 01133 01134 }else{ 01135 commonIntArrays.put((String)entry.getKey(), 01136 (HashMap<Integer, Integer>) 01137 entry.getValue()); 01138 } 01139 } 01140 01141 for(Map.Entry entry: generator.getRealParameterArrays().entrySet()){ 01142 if(commonRealArrays.containsKey(entry.getKey())){ 01143 ((HashMap<Integer, Double>) 01144 commonRealArrays.get(entry.getKey())).putAll 01145 ((HashMap<Integer, Double>)entry.getValue()); 01146 }else{ 01147 commonRealArrays.put((String)entry.getKey(), 01148 (HashMap<Integer, Double>) 01149 entry.getValue()); 01150 } 01151 } 01152 01153 gen.setIntParameters(commonInts). 01154 setRealParameters(commonReals). 01155 setIntParameterArrays(commonIntArrays). 01156 setRealParameterArrays(commonRealArrays); 01157 01158 return gen; 01159 } 01160 01162 01167 public Boolean getIsRelevantToPhoton(String pName) 01168 throws JetWebException{ 01169 01170 Boolean relevant = relevantPhoton_.get(pName); 01171 01172 if(relevant==null){ 01173 relevant = 01174 DBGeneratorManager.checkPhotonRelevance(this,pName,null); 01175 relevantPhoton_.put(pName, relevant); 01176 } 01177 return(relevant); 01178 } 01180 01186 public Boolean getIsRelevantToPhoton(String aName, Integer aIndex) 01187 throws JetWebException{ 01188 01189 HashMap<Integer, Boolean> relevantArray = 01190 relevantPhotonArrays_.get(aName); 01191 01192 if(relevantArray==null){ 01193 relevantArray = new HashMap<Integer, Boolean>(); 01194 relevantPhotonArrays_.put(aName, relevantArray); 01195 } 01196 01197 Boolean relevant = relevantArray.get(aIndex); 01198 01199 if(relevant==null){ 01200 relevant = 01201 DBGeneratorManager.checkPhotonRelevance(this,aName,aIndex); 01202 relevantArray.put(aIndex, relevant); 01203 } 01204 01205 return(relevant); 01206 } 01208 01209 public Boolean getIsRelevantToProton(String pName) 01210 throws JetWebException{ 01211 Boolean relevant = relevantProton_.get(pName); 01212 01213 if(relevant==null){ 01214 relevant = 01215 DBGeneratorManager.checkProtonRelevance(this,pName,null); 01216 relevantProton_.put(pName, relevant); 01217 } 01218 //System.out.println(relevant); 01219 return(relevant); 01220 } 01222 public Boolean getIsRelevantToProton(String aName, Integer aIndex) 01223 throws JetWebException{ 01224 01225 HashMap<Integer, Boolean> relevantArray = 01226 relevantProtonArrays_.get(aName); 01227 01228 if(relevantArray==null){ 01229 relevantArray = new HashMap<Integer, Boolean>(); 01230 relevantProtonArrays_.put(aName, relevantArray); 01231 } 01232 01233 Boolean relevant = relevantArray.get(aIndex); 01234 01235 if(relevant==null){ 01236 relevant = 01237 DBGeneratorManager.checkProtonRelevance(this,aName,aIndex); 01238 relevantArray.put(aIndex, relevant); 01239 } 01240 01241 return(relevant); 01242 } 01244 01253 public Boolean isRelevant(MCProcessType proc, String pName) 01254 throws JetWebException { 01255 01256 if(proc==null){ 01257 return(true); 01258 } 01259 //System.out.println(proc); 01260 01261 if (proc.hasPhoton()){ 01262 //System.out.println("checking photon relevance "+pName); 01263 if(getIsRelevantToPhoton(pName)) return true; 01264 } 01265 01266 if (proc.hasProton()){ 01267 //System.out.println("checking proton relevance "+pName); 01268 if(getIsRelevantToProton(pName)) return true; 01269 } 01270 //System.out.println("returning false"); 01271 return(false); 01272 } 01274 public Boolean isRelevant(MCProcessType proc, String pName, 01275 Integer aIndex) 01276 throws JetWebException { 01277 01278 if(proc==null){ 01279 return(true); 01280 } 01281 01282 if (proc.hasPhoton()){ 01283 if(getIsRelevantToPhoton(pName, aIndex)) return true; 01284 } 01285 01286 if (proc.hasProton()){ 01287 if(getIsRelevantToProton(pName, aIndex)) return true; 01288 } 01289 01290 return(false); 01291 } 01292 01297 public int getGeneratorId() throws JetWebException { 01298 if (generatorId<=0) { 01299 generatorId = DBGeneratorManager.getGeneratorId(generatorName,generatorVersion); 01300 } 01301 return generatorId; 01302 } 01307 protected void setGeneratorId(int id){ 01308 generatorId=id; 01309 } 01310 01311 01312 01313 01314 } 01315 01316
Generated Wed Jan 17 09:14:27 GMT 2007