Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

JetWebLogReader.java

Go to the documentation of this file.
00001 package cedar.jetweb.xml;
00002 
00003 import org.jdom.input.*;
00004 import org.jdom.Element;
00005 import org.jdom.Document;
00006 import org.jdom.Namespace;
00007 import org.jdom.JDOMException;
00008 
00009 import java.util.List;
00010 import java.util.Iterator;
00011 import java.util.StringTokenizer;
00012 import java.util.HashMap;
00013 import java.util.Vector;
00014 
00015 import cedar.jetweb.*;
00016 import cedar.jetweb.model.*;
00017 import cedar.jetweb.job.LogFile;
00018 import cedar.jetweb.generator.Generator;
00019 
00020 
00021 
00029 public class JetWebLogReader extends JetWebXMLReader {
00030 
00031     private LogFile logfile  = null;
00032     private Element hepml = null;
00033     //    private Element reaction = null;
00034     private Namespace ns = null;
00035     protected RunSeries runSeries_ = null;
00036 
00038     private static boolean zipped = true;
00040     private static String HEPDATA = "hepdata";
00042     private static String HEPML = "hepml";
00044     private static String SETTINGS = "generatorSettings";
00046     private static String GENERATOR = "generator";
00048     private static String RUN = "run";
00050     private static String RUNSERIES = "runSeries";
00052     private static String DARRAY = "doubleArray";
00054     private static String IARRAY = "intArray";
00056     private static String IMATRIX = "intMatrix";
00058     private static String DPARAM = "doubleParameter";
00060     private static String IPARAM = "intParameter";
00062     private static String PDF = "pdf";
00064     private static String ARRAYE = "arrayElement";
00065     private static String MATRIXE= "matrixElement";
00067     private static String NORM = "normalisation";
00069     private static String LUMI = "luminosity";
00071     private static String NEVENTS = "numberOfEvents";
00073     private static String CX = "crossSection";
00075     private static String PARTICLE = "particle";
00077     private static String REACTION = "reaction";
00079     private static String COLLISION = "collision";
00081     private static String MOMENTUM = "momentum";
00083     private static String COMMENT = "comment";
00085     private static String ID = "id";
00086 
00088     private  static String DEFAULT_NS = "hep";
00090     //private  static String DEFAULT_NS_URI = "http://www.cedar.ac.uk/xml/hepdata";
00091     
00092     private  static String DEFAULT_NS_URI = 
00093     "http://www.cedar.ac.uk/xml/hepml";
00094     private  static String PHOTON="gamma";
00095     private  static String PROTON="p";
00096     private  static String ANTIPROTON="pbar";
00097     private  static String GAMMALEPTON = "gamma/e";
00098     private  static String GAMMAANTILEPTON = "gamma/e+";
00099 
00100     private static boolean particleNameMapInit_ = true;
00101     private static HashMap<String, String> particleNameMap_ = 
00102     new HashMap<String, String>();
00103 
00104     //    private HashMap<String,HashMap<Integer,Integer>> procParameters = 
00105     //new HashMap<String,HashMap<Integer,Integer>>();
00106 
00111     public static void main(String args[]){
00112     if (args[0]==null){
00113         System.out.println("Please give xml doc URI");
00114         return;
00115     }
00116 
00117     
00118     try {
00119         System.out.println("Converting to JDOM..");
00120         JetWebLogReader reader = new JetWebLogReader(args[0]);
00121         LogFile log = new LogFile(args[0]);
00122         reader.fillLog(log);
00123         System.out.println(log.toString());
00124 
00125 
00126     } catch (Throwable t) {
00127         System.err.println("Error reading XML");
00128         System.err.println(t.toString());
00129     } 
00130     }
00131 
00132     public JetWebLogReader(String filename) throws JetWebXMLException {
00133     //be careful, this should be zipped
00134     super(filename,zipped);
00135     buildNamespace();
00136    }
00137 
00138     private void buildNamespace(){
00139     String nsPrefix = System.getProperty("hepdataNS");
00140     String nsUri = System.getProperty("hepdataNsURI");
00141     if (nsPrefix==null){
00142         nsPrefix=DEFAULT_NS;
00143     }
00144     if (nsUri==null){
00145         nsUri=DEFAULT_NS_URI;
00146     }
00147     ns = Namespace.getNamespace(nsPrefix,nsUri);
00148 
00149     }
00155     public void fillLog(LogFile log) throws JetWebException {
00156     
00157     try { 
00158         logfile = log;  
00159         logfile.setValid(false);
00160         runSeries_ = logfile.getRunSeries();
00161         if (runSeries_==null) {
00162         System.out.println
00163            ("JetWebLogReader.fillLog: LogFile contains null runSeries");
00164         runSeries_ = new RunSeries();
00165         logfile.setRunSeries(runSeries_);
00166         }else{
00167         System.out.println
00168           ("JetWebLogReader.fillLog: LogFile already contains a runSeries");
00169         }
00170         
00171         Element root = getDocument().getRootElement();
00172 
00173         if(root.getName().equals(HEPML)){
00174         hepml = root;
00175         } else {
00176         //hepdata = root.getChild(HEPDATA,ns);
00177         hepml = root.getChild(HEPML, ns);
00178         if (hepml==null){
00179             throw new JetWebXMLException
00180             ("XML LogReader cannot find "
00181              +HEPML+" in "+root.getName(),getFileName());
00182             // +HEPDATA+" in "+root.getName(),getFileName());
00183         }
00184         }
00185         
00186         List<Element> runs = null;//new List<Element>();
00187         Element runSeries = null;
00188         Element reaction = null;
00189         
00190         try{
00191         reaction = hepml.getChild(REACTION, ns);
00192         //reaction  = (Element)hepml.getChildren().get(0);
00193         }catch (Exception e){
00194         throw new JetWebXMLException
00195             ("XML LogReader cannot find "+REACTION
00196              +" in "+root.getName(),getFileName());
00197         }
00198 
00199         try{
00200         runSeries = hepml.getChild(RUNSERIES, ns);
00201         }catch (Exception e){
00202         throw new JetWebXMLException
00203             ("XML LogReader cannot find "+RUNSERIES
00204              +" in "+root.getName(),getFileName());
00205         }
00206 
00207         try{
00208         runs = runSeries.getChildren(RUN, ns);
00209         System.out.println
00210             ("JetWebLogReader: found " + runs.size() +" runs");
00211         }catch (Exception e){
00212 
00213         throw new JetWebXMLException
00214             ("XML LogReader cannot find "+RUN
00215              +" in "+root.getName(),getFileName());
00216         }
00217 
00218         for(int ii=0; ii!= runs.size(); ++ii){
00219         runSeries_.addRun(fillRun(reaction, runs.get(ii)));
00220         }
00221         runSeries_.setSoftDefaults();
00222         // at this stage only the current log lumi is here.
00223         logfile.setLumi(runSeries_.getLumi());
00224         logfile.setCrossSection(runSeries_.getCrossSection());
00225 
00226         logfile.doChecks();
00227 
00228         //make sure LogReader xml file and Logfile xml file are same
00229         if (!this.getFileName().equals(logfile.getXmlFileName())){
00230         System.out.println
00231             ("LogReader Warning: xmlfile "+this.getFileName()
00232              + " different from logfile xmlfile " + 
00233              logfile.getXmlFileName() +
00234              " changing logfile version");
00235         logfile.setXmlFile(this.getFileName());
00236         }
00237 
00238     } catch (JetWebException jwe) {
00239         System.out.println
00240         ("JetWebLogReader: Problem reading "+log.getFile());
00241         System.out.println(jwe.getMessage());
00242         jwe.printStackTrace(System.out);
00243         logfile.setValid(false);
00244         logfile.setStatus(LogFile.ERROR);
00245     }   
00246     }
00247 
00255     private Run fillRun(Element reaction, Element run)
00256     throws JetWebException{
00257 
00258     Run runObj = new Run();
00259 
00260     Element settings = run.getChild(SETTINGS, ns);
00261     
00262     if(settings==null){
00263         throw new JetWebXMLException
00264         ("JetWebLogReader: No Generator settings", this.getFileName());
00265     }
00266 
00267     Generator generator = makeGenerator(settings);
00268     
00269     runObj.setGenerator(generator);
00270     Iterator<Element> pdfIt = settings.getChildren(PDF, ns).iterator();
00271 
00272     if(pdfIt==null){
00273         throw new JetWebXMLException
00274         ("JetWebLogReader: No PDFs", this.getFileName());
00275     }
00276 
00277     while(pdfIt.hasNext()){
00278         Element pdf = (Element)pdfIt.next();
00279         String particleName = 
00280         getParticleName(pdf.getChild(PARTICLE,ns).
00281                 getAttributeValue("name"));
00282         String pdfSet = pdf.getAttributeValue("set");
00283         String pdfM = pdf.getAttributeValue("member");
00284         String pdfScheme = pdf.getAttributeValue("scheme");
00285         if(particleName==null||
00286            pdfSet==null||
00287            pdfM==null||
00288            pdfScheme==null){
00289         throw new JetWebXMLException
00290             ("JetWebLogReader: Badly defined PDFs", 
00291              this.getFileName());
00292         }
00293 
00294         Integer pdfMember = null;
00295         if (pdfM!=null){
00296         pdfMember= new Integer(pdfM);
00297         }
00298         
00299         PDF pdfObj = new PDF(pdfMember);
00300 
00301         if(particleName.equals(PHOTON)){
00302         pdfObj.setIsPhoton();
00303         }else if(particleName.equals(PROTON) ||
00304              particleName.equals(ANTIPROTON)){
00305 
00306         pdfObj.setIsProton();
00307         }
00308 
00309         runObj.setPDF(pdfObj);
00310     }
00311 
00312     Element norm = run.getChild(NORM, ns);
00313 
00314     //runObj.setLumi(lumi(run));
00315     runObj.setLumi(lumi(norm));
00316     runObj.setCrossSection(crossSection(norm));
00317     runObj.setProcParams(procParams(generator, settings));
00318     try{
00319 
00320         runObj.setMCProcessType(fillCollision(reaction));
00321 
00322     }catch(JetWebException err){
00323         System.out.println(err.getMessage());
00324     }
00325 
00326     return(runObj);
00327     
00328     }
00329 
00330     private double crossSection(Element norm){
00331 
00332     double cs = -1.0;
00333     boolean gotCS = false;
00334 
00335     if(norm!=null){
00336         String cross = norm.getChildTextTrim(CX, ns);
00337         if(cross!=null && !cross.equals("")){
00338         cs = Double.parseDouble(cross);
00339         gotCS = true;
00340         }else{
00341         String nevents = norm.getChildTextTrim(NEVENTS,ns);
00342         String lum = norm.getChildTextTrim(LUMI,ns);
00343 
00344         if(nevents!=null &&
00345            !nevents.equals("") &&
00346            lum!=null &&
00347            !lum.equals("")){
00348             Double l = (Double)Double.parseDouble(lum);
00349             double n = Double.parseDouble(nevents);
00350             
00351             if(!l.equals(0)){
00352             cs = n/l;
00353             gotCS = true;
00354             }
00355         }
00356         }
00357     }
00358 
00359     if(!gotCS){
00360         System.out.println
00361         ("Warning: cross section not specified for run!");
00362     }
00363     
00364     return cs;
00365     }
00366 
00367 
00368     private double lumi(Element norm){
00369     double luminosity = 0.0;
00370     boolean gotLumi = false;
00371 
00372     //Element norm = settings.getChild(NORM,ns);
00373     
00374     if(norm!=null){
00375         
00376         String lumi = norm.getChildTextTrim(LUMI,ns);
00377         if (lumi!=null && !lumi.equals("")){
00378         
00379         luminosity += Double.parseDouble(lumi);
00380         gotLumi = true;
00381 
00382         }else{
00383         String nevents = norm.getChildTextTrim(NEVENTS,ns);
00384         String cx = norm.getChildTextTrim(CX,ns);
00385         
00386         if(nevents != null &&
00387            !nevents.equals("") &&
00388            !cx.equals("")){
00389             Double crossSection = (Double)Double.parseDouble(cx);
00390             int n = Integer.parseInt(nevents);
00391 
00392             if(!crossSection.equals(0)){
00393             luminosity+= n / crossSection;
00394             gotLumi = true;
00395             }
00396         }
00397         }
00398     }
00399 
00400     if(!gotLumi){
00401         System.out.println("Warning: zero luminosity for run!");
00402     }
00403     return(luminosity);
00404     }
00405 
00406     private Generator makeGenerator(Element settings)
00407     throws JetWebException{
00408 
00409     Generator generator = null;
00410 
00411         String genName = 
00412         settings.getChild(GENERATOR, ns).getAttributeValue("name");
00413         
00414         String genVersion = 
00415         settings.getChild(GENERATOR,ns).getAttributeValue("version");
00416         
00417         if(genName==null||
00418            genVersion==null){
00419         throw new JetWebXMLException
00420             ("JetWebLogReader: Badly defined generator settings", 
00421              this.getFileName());
00422         }
00423 
00424         int ii = genVersion.indexOf(".");
00425         
00426         while(ii>-1){
00427         genVersion = 
00428             genVersion.substring(0,ii)+genVersion.substring(ii+1);
00429         ii = genVersion.indexOf(".");
00430         }
00431         try{
00432         generator = Generator.Maker(genName, genVersion);
00433         }catch(JetWebException err){
00434         System.out.println(err.getMessage());
00435         throw new JetWebXMLException
00436             ("JetWebLogReader: Badly defined generator settings", 
00437              this.getFileName());
00438         }
00439         List<Element> arg = settings.getChildren(DARRAY,ns);
00440         if(arg!=null){
00441         fillDoubleArrays(arg, generator);
00442         }else{
00443         throw new JetWebXMLException
00444             ("JetWebLogReader: Badly defined generator settings", 
00445              this.getFileName());
00446         }
00447 
00448         arg = settings.getChildren(IARRAY,ns);
00449 
00450         if(arg!=null){
00451         fillIntArrays(arg, generator);
00452         }else{
00453         throw new JetWebXMLException
00454             ("JetWebLogReader: Badly defined generator settings", 
00455              this.getFileName());
00456         }
00457 
00458         arg = settings.getChildren(DPARAM,ns);
00459 
00460         if(arg!=null){
00461         fillDoubleParams(arg, generator);
00462         }else{
00463         throw new JetWebXMLException
00464             ("JetWebLogReader: Badly defined generator settings", 
00465              this.getFileName());
00466         }
00467 
00468         arg = settings.getChildren(IPARAM,ns);
00469 
00470         if(arg!=null){
00471         fillIntParams(arg, generator);
00472         }else{
00473         throw new JetWebXMLException
00474             ("JetWebLogReader: Badly defined generator settings", 
00475              this.getFileName());
00476         }
00477 
00478     return(generator);
00479     }
00480 
00481     private Collision fillCollision(Element collE) throws JetWebXMLException {
00482 
00483         Collision coll = new Collision();
00484 
00485     List<Element> pList = collE.getChild(COLLISION, ns).getChildren(PARTICLE,ns);
00486 
00487     if (pList==null||pList.size()==0) {
00488         throw new JetWebXMLException
00489         ("XMLLogReader; Collision has no particles",
00490          logfile.toString());
00491     }
00492 
00493     Iterator<Element> it = pList.iterator();
00494 
00495     while (it.hasNext()){
00496         Element eParticle = (Element)it.next();
00497         String name = getParticleName(eParticle.getAttributeValue("name"));
00498 
00499         Double energy = null;
00500         Particle particle = null;
00501         Integer pNumber = null;
00502         try {
00503         pNumber = new Integer(eParticle.getAttributeValue(ID));
00504         energy = new Double(eParticle.getChildTextTrim(MOMENTUM,ns));
00505             particle = new Particle(name,energy.doubleValue());
00506 
00507         } catch (NumberFormatException e){
00508 
00509         throw new JetWebXMLException
00510             (e+" LogFileReader: getParticle ",
00511              name+" energy <"+
00512              eParticle.getChildTextTrim(MOMENTUM,ns)+">");
00513         } catch (NullPointerException e){
00514         throw new JetWebXMLException
00515             (e+" LogFileReader: getParticle ",
00516              name+" energy <"+
00517              eParticle.getChildTextTrim(MOMENTUM,ns)+">");
00518         }
00519         try{
00520         coll.addParticle(particle, pNumber);
00521         }catch(JetWebException err){
00522         System.out.println(err.getMessage());
00523         throw new JetWebXMLException
00524             ("XMLLogReader: Badly defined particle in collision", 
00525              this.getFileName());
00526         }
00527     }
00528 
00529     if(coll.getNumberOfParticles()!=2){
00530         throw new JetWebXMLException
00531             ("XMLLogReader: Collision does not contain two particles", 
00532              this.getFileName());
00533     }
00534 
00535     if(coll.getId()==-1){
00536         throw new JetWebXMLException
00537             ("XMLLogReader: Collision is not present in Database", 
00538              this.getFileName());
00539     };
00540 
00541     return coll;
00542 
00543     }//fillCollision
00544 
00545 
00546     private void fillDoubleArrays(List<Element> aList, Generator generator)
00547     throws JetWebException{
00548 
00549     if (aList==null) return;
00550     Iterator<Element> it = aList.iterator();
00551     while (it.hasNext()){
00552         Element eArray = it.next();
00553         String aName = eArray.getAttributeValue("name");
00554         Iterator<Element> eleIt = 
00555         eArray.getChildren(ARRAYE,ns).iterator();       
00556         if (generator.inModel(aName)||
00557         generator.inCuts(aName)){
00558         if (!generator.getRealParameterArrays().containsKey(aName)){
00559             generator.getRealParameterArrays().put
00560             (aName,new HashMap<Integer,Double>());
00561         }
00562         HashMap<Integer,Double> array = 
00563             generator.getRealParameterArrays().get(aName);
00564         while (eleIt.hasNext()){
00565             Element e = (Element)eleIt.next();
00566             Integer index = new Integer(e.getAttributeValue("index"));
00567             Double eVal = new Double(e.getTextTrim());
00568             if(generator.inModel(aName, index)||generator.inCuts(aName)){
00569             array.put(index,eVal);
00570             }
00571         }
00572         }
00573     }
00574 
00575     };
00576     
00577     private void fillIntArrays(List<Element> aList, Generator generator)
00578     throws JetWebException{
00579     if (aList==null) return;
00580     Iterator<Element> it = aList.iterator();
00581     while (it.hasNext()){
00582         Element eArray = it.next();
00583         String aName = eArray.getAttributeValue("name");        
00584         Iterator<Element> eleIt = eArray.getChildren(ARRAYE,ns).iterator();     
00585         if (generator.inModel(aName)){
00586         if (!generator.getIntParameterArrays().containsKey(aName)){
00587             generator.getIntParameterArrays().put(aName,new HashMap<Integer,Integer>());
00588         }
00589         HashMap<Integer,Integer> array = generator.getIntParameterArrays().get(aName);
00590         while (eleIt.hasNext()){
00591             Element e = eleIt.next();
00592             Integer index = new Integer(e.getAttributeValue("index"));
00593             Integer eVal = new Integer(e.getTextTrim());
00594             array.put(index,eVal);         
00595         }
00596         }       
00597     }
00598     }
00599 
00600     private void fillIntParams
00601     (List<Element> ipList, Generator generator) 
00602     throws JetWebException{
00603 
00604     if (ipList==null) return;
00605     Iterator<Element> it = ipList.iterator();
00606     while (it.hasNext()){
00607         Element iParam = (Element)it.next();
00608         String pName = iParam.getAttributeValue("name");
00609         Integer pValue = new Integer(iParam.getTextTrim());
00610 
00611         HashMap<String,Integer> iParams = generator.getIntParameters();
00612         if (iParams.containsKey(pName)){
00613         // maybe be more severe about this?
00614         System.out.println("Warning. Multiply defined "+pName);
00615         iParams.remove(pName);
00616         }
00617 
00618         // Read in process params for now because some model params 
00619         // are only relevant to some processes
00620 
00621         //if (generator.inModel(pName)){
00622         iParams.put(pName,pValue);
00623         //}
00624     }
00625     }
00626     private void fillDoubleParams(List<Element> dpList, Generator generator) 
00627     throws JetWebException{
00628 
00629     if (dpList==null) return;
00630     Iterator<Element> it = dpList.iterator();
00631     while (it.hasNext()){
00632         Element dParam = it.next();
00633         String pName = dParam.getAttributeValue("name");
00634         Double pValue = new Double(dParam.getTextTrim());
00635         HashMap<String,Double> rParams = generator.getRealParameters();
00636 
00637         if (rParams.containsKey(pName)){
00638         // maybe be more severe about this?
00639         System.out.println("Warning. Multiply defined "+pName);
00640         rParams.remove(pName);
00641         }
00642 
00643         if (generator.inModel(pName)||generator.inCuts(pName)){
00644         rParams.put(pName,pValue);      
00645         }
00646     }
00647     }
00648     private HashMap<String, Object> procParams
00649     //    private HashMap<String, HashMap<Integer, Integer> > procParams
00650     (Generator generator, Element settings)
00651     throws JetWebXMLException{
00652 
00653     Iterator<Element> aIt = settings.getChildren(IARRAY, ns).iterator();
00654     Iterator<Element> iIt = settings.getChildren(IPARAM, ns).iterator();
00655     Iterator<Element> mIt = settings.getChildren(IMATRIX, ns).iterator();
00656 
00657     HashMap<String, Object > Params = 
00658         new HashMap<String, Object >();
00659 
00660     while(aIt.hasNext()){
00661         Element eArray = aIt.next();
00662         String aName = eArray.getAttributeValue("name");        
00663         Iterator<Element> eleIt = eArray.getChildren(ARRAYE,ns).iterator();
00664         
00665         if(generator.inProcessType(aName)){
00666         HashMap<Integer,Integer> array = new HashMap<Integer,Integer>();
00667         while(eleIt.hasNext()){
00668             Element e = eleIt.next();
00669             Integer index = new Integer(e.getAttributeValue("index"));
00670             Integer eVal = new Integer(e.getTextTrim());
00671             array.put(index, eVal);
00672         }
00673         Params.put(aName, array);
00674         }
00675     }
00676     
00677     while(iIt.hasNext()){
00678         Element iParam = (Element)iIt.next();
00679         String pName = iParam.getAttributeValue("name");
00680         Integer pValue = new Integer(iParam.getTextTrim());
00681         
00682         if (generator.inProcessType(pName)){
00683         //HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
00684         //map.put(new Integer(1),pValue);
00685         //Params.put(pName, map);
00686         Params.put(pName, pValue);
00687         }
00688     }
00689 
00690     while(mIt.hasNext()){
00691         Element eMatrix = (Element)mIt.next();
00692         String mName = eMatrix.getAttributeValue("name");
00693         Iterator<Element> matIt = 
00694         eMatrix.getChildren(MATRIXE,ns).iterator();
00695         if(generator.inProcessType(mName)){
00696         HashMap<Integer, HashMap<Integer, Integer>> matrix = 
00697             new HashMap<Integer, HashMap<Integer, Integer>>();
00698         while(matIt.hasNext()){
00699             Element e = matIt.next();
00700             Integer iindex = new Integer(e.getAttributeValue("iindex"));
00701             Integer jindex = new Integer(e.getAttributeValue("jindex"));
00702             Integer mValue = new Integer(e.getTextTrim());
00703             HashMap<Integer, Integer> row = matrix.get(iindex);
00704             if(row==null){
00705             row = new HashMap<Integer, Integer>();
00706             matrix.put(iindex, row);
00707             }
00708             row.put(jindex, mValue);
00709         }
00710         Params.put(mName, matrix);
00711         }
00712     }
00713 
00714     return(Params);
00715     };
00716 
00717     private String getParticleName(String pName){
00718     
00719     if(particleNameMapInit_) initParticleNames();
00720 
00721     String name = 
00722         particleNameMap_.get(pName);
00723 
00724     if(name==null) return pName;
00725 
00726     return name;
00727     }
00728 
00729     private void initParticleNames(){
00730     particleNameMapInit_ = false;
00731     particleNameMap_.put("p+", PROTON);
00732     particleNameMap_.put("pbar-", ANTIPROTON);
00733     particleNameMap_.put(GAMMALEPTON, "e-");
00734     particleNameMap_.put(GAMMAANTILEPTON, "e+");
00735     
00736     return;
00737     }
00738 
00739 }
00740 
00741 
00742 
00743 
00744 

Generated Wed Jan 17 09:14:27 GMT 2007