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

Paper.java

Go to the documentation of this file.
00001 package cedar.jetweb.model.paper;
00002 
00003 import java.util.Vector;
00004 import java.util.Date;
00005 import java.util.Map;
00006 import java.util.HashMap;
00007 import java.util.Iterator;
00008 import java.util.Collection;
00009 
00010 import java.text.DateFormat;
00011 import java.text.SimpleDateFormat;
00012 
00013 import java.io.BufferedReader;
00014 import java.io.File;
00015 import java.io.IOException;
00016 import java.io.Serializable;
00017 import java.io.BufferedWriter;
00018 
00019 import java.text.NumberFormat;
00020 import java.text.ParseException;
00021 import java.text.DecimalFormat;
00022 
00023 import cedar.jetweb.Storeable;
00024 import cedar.jetweb.JetWebException;
00025 import cedar.jetweb.JetWebConfig;
00026 import cedar.jetweb.model.*;
00027 import cedar.jetweb.model.plots.*;
00028 import cedar.jetweb.model.fit.*;
00029 import cedar.jetweb.db.DBPlotManager;
00030 import cedar.jetweb.xml.JetWebPlotReader;
00031 import cedar.jetweb.xml.JetWebPlotReaderFactory;
00032 
00033 
00044 public class Paper implements Storeable {
00045     // Declarations
00046     private boolean isRealData;
00047     private boolean available=true;
00048 
00049     private double[] sumChi2;
00050     private double[] fitChi2;
00051 
00052     private int id=0;
00053     private String directory=null;
00054     private String title;
00055     private String reference;
00056     private String codeAuthor;
00057     private String contact;
00058     private int nplots=0;
00059     private Map<Integer,DataPlot> plots = new HashMap<Integer,DataPlot>();
00060     //private String htmldir=null;
00061     private String collaboration;
00062     private String accelerator;
00063 
00064     //Paper shouldn't need a runseries!
00065     //private RunSeries runSeries;
00066 
00067     private int irn;
00068 
00069     private double chi2;
00070     private double dof;
00071 
00072     // Constructors.
00073 
00080     public Paper(int paperId) throws JetWebException {
00081     this(true);
00082     id = paperId;
00083     retrieve();
00084     }
00085 
00086 
00092     public Paper(boolean realData)  {
00093 
00094     this.isRealData = realData;
00095 
00096     }
00097 
00107     public Paper(Paper dataPaper, Model model) throws JetWebException {
00108 
00109     id=dataPaper.getId();
00110         this.isRealData = false;
00111 
00112     irn = dataPaper.getIRN();
00113     reference = dataPaper.getReference();
00114     collaboration = dataPaper.getCollaboration();
00115     title     = dataPaper.getTitle();
00116     accelerator = dataPaper.getAccelerator();
00117     contact   = dataPaper.getContact();
00118     codeAuthor= dataPaper.getCodeAuthor();
00119     directory = dataPaper.getDirName();
00120 
00121     for (DataPlot compPlot : dataPaper.getPlots()){
00122         
00123         //System.out.println("instantiating predicted plot");
00124         try { 
00125 
00126         RunSeries runSeries = 
00127             model.getRunSeriesCollection().getRunSeries(compPlot);
00128 
00129         PredictedPlot plot = new PredictedPlot(compPlot,runSeries);
00130         //System.out.println("instantiated predicted plot");
00131         if (plot.hasData()){
00132             //System.out.println("Paper: found a plot with data "+plot.getId());
00133             plot.setCompPlot(compPlot);
00134             addPlot(plot);
00135         }
00136         } catch (JetWebException jwe) {
00137         // No need to do anything here. This DataPlot simply 
00138         // doesn't have a prediction
00139         // yet with this Model.     
00140         }
00141     }
00142     }
00143 
00190     // Public methods.
00191 
00192     public boolean retrieve() throws JetWebException {
00193     DBPlotManager.fillPaper(this);
00194     return true;
00195     }
00196 
00197     //    public RunSeries getRunSeries(){
00198     //return runSeries;
00199     //}
00200 
00204     public int getIRN(){
00205     return irn;
00206     }
00207 
00211     public void setIRN(int newirn){
00212     irn = newirn;
00213     }
00214 
00215 
00217     public boolean isReal(){ return isRealData; }
00218 
00220     public boolean isAvailable(){ return available; }
00221 
00226     public int getId(){ return id; }
00231     public void setID(int newid){ id=newid; }
00232 
00233     public int getSpiresId(){
00234     return(getIRN());
00235     }
00236 
00237     public Paper setSpiresId(int id){
00238     setIRN(id);
00239     return this;
00240     }
00241 
00245     public String getDirName(){ return directory; }
00249     public void setDirName(String s){ directory = s; }
00250 
00252     //public String getHTMLDir(){ 
00253     //  return htmldir; 
00254     //}
00255 
00257     public String getTitle(){return title;}
00259     public void setTitle(String newtitle){title = newtitle;}
00260 
00262     public String getCollaboration(){return collaboration;}
00264     public void setCollaboration(String newcollab){collaboration = newcollab;}
00265 
00267     public String getReference(){return reference;}
00269     public void setReference(String newreference){reference = newreference;}
00270 
00272     public String getCodeAuthor(){return codeAuthor;}
00274     public void setCodeAuthor(String newcodeAuthor)
00275     {codeAuthor = newcodeAuthor;}
00276 
00278     public String getContact(){return contact;}
00279 
00280     public String getCodeContact(){return(getContact());}
00281 
00283     public void setContact(String newcontact){contact = newcontact;}
00284 
00285     public void setCodeContact(String newcontact){
00286     setContact(newcontact);
00287     return;
00288     };
00289 
00291     public int getNPlots(){return nplots;}
00293     public void setNPlots(int newnplots){nplots = newnplots;}
00294 
00296     public Collection<? extends DataPlot> getPlots(){
00297     return plots.values();
00298     }
00299 
00301     public Map<Integer,? extends DataPlot> getIndexedPlots(){
00302     Map<Integer,DataPlot> retn = new HashMap<Integer,DataPlot>();
00303     retn.putAll(plots);
00304     return retn;
00305     }
00306 
00308     public String getAccelerator(){ return accelerator; }
00310     public void setAccelerator(String newaccelerator){ 
00311     accelerator = newaccelerator; 
00312     }
00316     public void setDirectory(String newDir){ 
00317     directory = newDir;
00318     createFileStructure();
00319     }
00320 
00321     //call this from set directory
00322     private void createFileStructure(){
00323 
00324     File dirFile = new File(JetWebConfig.rootDirectory+directory);
00325     if (!dirFile.exists()){
00326         dirFile.mkdir();
00327     }
00328 
00329     File afile = new File(directory+"/UNAVAILABLE");
00330     if (afile.exists()){
00331         available = false;
00332     }
00333     }
00334 
00335 
00337     public void addPlot(DataPlot plot){
00338     try {            
00339         this.plots.put(plot.getId(),plot);
00340     } catch (Exception e){
00341         System.out.println("Paper.addPlot - can't add plot id:"+plot.getId());
00342     }
00343     nplots=plots.size();
00344         //System.out.println("no of plots= " + plots.size());
00345     }
00346 
00361     public void setFrom(Paper paper,boolean comp) throws JetWebException {
00362 
00363     //System.out.println("Called Paper.setFrom");
00364     //Exception e = new Exception();
00365     //e.printStackTrace();
00366     //this.runSeries = paper.getRunSeries();
00367     this.reference = paper.getReference();
00368     this.collaboration = paper.getCollaboration();
00369     this.title = paper.getTitle();
00370     this.accelerator = paper.getAccelerator();
00371     this.id=paper.getId();
00372     this.contact = paper.getContact();
00373     this.codeAuthor = paper.getCodeAuthor();
00374     this.irn = paper.getIRN();
00375     this.directory = paper.getDirName();
00376         //System.out.println("im here");
00377         
00378     for (DataPlot oldplot : paper.getPlots() ){
00379         DataPlot plot;
00380 
00381         //is this paper a real data paper?
00382         if (isRealData){
00383         // looks very wasteful.
00384         plot = new RealPlot(oldplot.getNumber(),id);
00385         } else {
00386         // not real data
00387         DataPlot compPlot;
00388         RunSeries runSeries;
00389         if(oldplot instanceof PredictedPlot){
00390             runSeries = ((PredictedPlot)oldplot).getRunSeries();
00391         }else if(oldplot instanceof FittedPlot){
00392             Integer rsId = ((FittedPlot)oldplot).getRunSeriesId();
00393             runSeries = RunSeries.Maker(rsId);
00394         }else{
00395             throw new JetWebException
00396             ("Paper.setFrom() ERROR:"+
00397              "Real data plot in predicted paper", 
00398              "Paper id:" + id + "  plot id:" + oldplot.getId());
00399         }
00400             if (comp) { 
00401                 compPlot = oldplot;
00402         } else {
00403             if (oldplot instanceof PredictedPlot){
00404             compPlot = ((PredictedPlot)oldplot).getCompPlot();
00405             } else if (oldplot instanceof FittedPlot){
00406             compPlot = ((FittedPlot)oldplot).getCompPlot();
00407             } else {
00408             throw new JetWebPlotException("Attempt to get comparison plots from real data paper","Old PaperId:"+paper.getId()+" New PaperId:"+this.getId());
00409             }
00410         }
00411         plot = new PredictedPlot(compPlot,runSeries);
00412         }
00413         plot.setDataPoints(oldplot.getDataPoints());
00414         this.addPlot(plot);
00415     }
00416     }
00417    
00422     public void add(Paper paper2) throws JetWebException {
00423     
00424     for (DataPlot one : getPlots() ){
00425 
00426         PredictedPlot onep = (PredictedPlot)one;
00427         PredictedPlot two = (PredictedPlot)paper2.getPlot(one.getId());
00428         onep.add(two);
00429     }
00430     }
00437     public DataPlot getPlot(int csnId){
00438     DataPlot ret = null;
00439     try {
00440         Integer i = new Integer(csnId);
00441         ret = (DataPlot)plots.get(i);
00442     } catch (Exception e){
00443         System.out.println("Paper.getPlot(): Attempt to retreive plot failed, plot id:"+csnId);
00444     }
00445 
00446     return ret;
00447     }
00448 
00460     public Paper fit(final Paper dataPaper, double scale, PlotSelection selection, 
00461               boolean scaleall) throws JetWebException {
00462     if (isReal()){
00463         throw new JetWebPlotException("Attempt to run a fit on Real Data: only valid for Predicted Data",
00464                       "PaperId:"+this.getId());
00465     }
00466     Paper scaled = new Paper(false);    
00467     boolean useOldPlots = false;
00468     scaled.setFrom(this,useOldPlots);
00469 
00470     //need to add all matching data points to the fit at this point
00471 
00472 
00473     scaled.sumChi2 = new double[2];
00474     scaled.fitChi2 = new double[2];
00475 
00476     Collection<? extends DataPlot> scaledPlots = scaled.getPlots();
00477 
00478     for (DataPlot mcPlot1 : scaledPlots){
00479         double chi2=0;
00480         PredictedPlot mcPlot = (PredictedPlot)mcPlot1;
00481         Vector<DataPoint> mcPoints = new Vector<DataPoint>();
00482         RealPlot dataPlot = (RealPlot)dataPaper.getPlot(mcPlot.getId());
00483         if (mcPlot.validMCProcessType()){
00484 
00485         if (selection.getSumPlots().contains(dataPlot) || scaleall){
00486             
00487             if (mcPlot.hasData()){
00488             mcPoints   = mcPlot.getDataPoints();
00489             
00490             double local_scale;
00491             // Shape plots never get scaled (they are ratios).
00492             if (mcPlot.isShape()){
00493                 local_scale = 1;
00494             } else {
00495                 local_scale = scale;
00496             }
00497             double sysScale = dataPlot.getSysScale();
00498             Vector<DataPoint> dataPoints = dataPlot.getDataPoints();
00499             
00500             if (mcPoints.size()!=dataPoints.size()){
00501                 System.out.println("Error: attempt to fit unlike histograms: plot"
00502                            +mcPlot.getNumber()+": "+dataPoints.size()
00503                            +" "+mcPoints.size());
00504                 
00505             } else {
00506                 
00507                 int missing=0;
00508                 for (int pointnum=0; pointnum<mcPoints.size(); pointnum++){
00509                 DataPoint mcp = (DataPoint)mcPoints.elementAt(pointnum);
00510                 DataPoint dp = (DataPoint)dataPoints.elementAt(pointnum);
00511                 
00512                 // Skip points with no errors.
00513                 if ( (mcp.getYUp()!=0.0) && 
00514                      (dp.getYUp()!=0.0) ){
00515 
00516                     // Scale the x value.
00517                     double error2 = Math.pow(local_scale*(mcp.getYUp()+mcp.getYDown())/2.0,2)
00518                     +Math.pow(sysScale*(dp.getYUp()+dp.getYDown())/2.0,2);
00519                     // Calculate the chi2
00520                     double cont = Math.pow(mcp.getY()*local_scale-sysScale*dp.getY(),2)/error2;   
00521                     Double testing = new Double(cont);
00522                     if (testing.isNaN() || testing.isInfinite()){
00523                     System.out.println("Error: plot"+mcPlot.getNumber());
00524                     System.out.println(error2+" , "+ mcp.toString()+":"+dp.toString());
00525                     } else {
00526                     chi2 = chi2+cont;
00527                     }
00528                     mcp.setY(mcp.getY()*local_scale);
00529                     mcp.setYUp(mcp.getYUp()*local_scale);
00530                     mcp.setYDown(mcp.getYDown()*local_scale);
00531                     mcp.setChi2(cont);
00532                     
00533                 } else {
00534                     missing++;
00535                 }
00536                 }
00537                 double[] tmp = new double[] {chi2, mcPoints.size()-missing};
00538                 mcPlot.setChi2(tmp);
00539                 mcPlot.setSysScale(local_scale);
00540             }
00541             } else {
00542             System.out.println("No data for plot");
00543             }
00544         }
00545         
00546 
00547         if (selection.getSumPlots().contains(dataPlot)){
00548             scaled.sumChi2[0] = scaled.sumChi2[0]+mcPlot.getChi2()[0];
00549             scaled.sumChi2[1] = scaled.sumChi2[1]+mcPlot.getChi2()[1];
00550         }
00551         if (selection.getFitPlots().contains(dataPlot)){
00552             scaled.fitChi2[0] = scaled.fitChi2[0]+mcPlot.getChi2()[0];
00553             scaled.fitChi2[1] = scaled.fitChi2[1]+mcPlot.getChi2()[1];
00554         }
00555         }
00556     }
00557     return scaled;
00558      
00559     }
00560 
00561     public boolean store() throws JetWebException {
00562     
00563     if (!this.isRealData) {
00564         System.out.println("Error: Attempt to write MC paper to DB");
00565         throw new JetWebException("Error: Attempt to write MC paper to DB",
00566                       "Paper ID "+id);
00567     }
00568     
00569     boolean paperExisted = DBPlotManager.addPaper(this);
00570     return !paperExisted;
00571     }
00572 
00573 
00790     public void fixFit(Fit fit) throws JetWebException{
00791 
00792     Map<Integer,DataPlot> fittedPlots = new HashMap<Integer,DataPlot>();
00793     //Iterator<DataPlot> plotList = getPlots().iterator();
00794 
00795     Collection<? extends DataPlot> plotList = getPlots();
00796 
00797     for (DataPlot plot : getPlots()){    
00798 
00799         try {
00800         FittedPlot fittedPlot = new FittedPlot((PredictedPlot)plot, fit );
00801         fittedPlots.put(new Integer(fittedPlot.getId()),fittedPlot);
00802         } catch (Exception e){
00803         System.out.println("Paper.fixFit() cannot add plotId:"+plot.getId());
00804         throw new JetWebException(e);
00805         }
00806 
00807     }
00808     plots = fittedPlots;
00809     }
00810 
00811 
00821     public double[] getSumChi2(){return sumChi2;}
00822 
00832     public double[] getFitChi2(){return fitChi2;}
00833 
00834 
00839     public double getChi2(int fitId){
00840 
00841     if (chi2>0) return chi2;
00842     setChi2DOF(fitId);
00843     return chi2;
00844     }
00849     public double getDOF(int fitId){
00850 
00851     if (dof>0) return dof;
00852     setChi2DOF(fitId);
00853     return dof;
00854     }
00855 
00856 
00857     private void setChi2DOF(int fitId){
00858 
00859     if (!isRealData) { 
00860         for (DataPlot plot : getPlots()){
00861         double[] chi2d = plot.getChi2(fitId); 
00862         chi2+=chi2d[0];
00863         dof+=chi2d[1];
00864         }
00865     }
00866     }
00867 
00871     public void delete() throws JetWebException {
00872 
00873     try {
00874         if (getPlots()!=null){
00875         for (DataPlot plot : getPlots() ){
00876             
00877             RealPlot rplot = (RealPlot)plot;
00878             rplot.delete();
00879             
00880         }
00881         getPlots().clear();
00882         }
00883         
00884         DBPlotManager.delete(this);
00885     } catch (Exception e) {
00886         throw new JetWebException(e);
00887     }
00888 
00889     }
00890 
00895     public boolean containsDataFor(MCProcessType proc) throws JetWebException {
00896 
00897     for (DataPlot plot : getPlots()) {
00898 
00899         if (plot.getMCProcessType().equals(proc)){
00900         return true;
00901         }
00902         
00903     }
00904     return false;
00905     }
00906 
00910     public boolean containsDataFor(Collision coll) throws JetWebException {
00911 
00912     for (DataPlot plot : getPlots()) {
00913 
00914         if (plot.getMCProcessType().getCollision().equals(coll)){
00915         return true;
00916         }
00917         
00918     }
00919     return false;
00920     }
00921 
00922 }
00923 
00924 
00925 
00926 
00927 
00928 
00929 

Generated Wed Jan 17 09:14:27 GMT 2007