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

CombinedPaper.java

Go to the documentation of this file.
00001 package cedar.jetweb.model.paper;
00002 
00003 //import cedar.jetweb.Storeable;
00004 import cedar.jetweb.JetWebException;
00005 import cedar.jetweb.model.plots.*;
00006 import cedar.jetweb.model.Model;
00007 import cedar.jetweb.model.fit.Fit;
00008 import cedar.jetweb.model.RunSeries;
00009 
00010 import java.util.HashMap;
00011 import java.util.Collection;
00012 import java.util.Collections;
00013 import java.util.Vector;
00014 import java.util.Map;
00021 public class CombinedPaper {
00022 
00023     private RealPaper paper_;
00024 
00025     //The model and fit Ids for which this paper contains plots
00026     private Integer modelId_ = -1;
00027     private Fit fit_;
00028 
00029     private HashMap<Integer, MergedPlot> predictedPlots_ = 
00030     new HashMap<Integer, MergedPlot>();
00031 
00032     private HashMap<Integer, FittedPlot> fittedPlots_ = 
00033     new HashMap<Integer, FittedPlot>();
00034 
00035     private Double chi2_;
00036     private Double dof_;
00037 
00038     private Boolean dirtyChi2_ = true;
00039 
00040     private Double scale_ = 1.0;
00041 
00046     public CombinedPaper(){
00047     
00048     }
00049     
00054     public CombinedPaper(RealPaper paper) throws JetWebException{
00055     
00056     paper_ = paper;
00057     }
00058 
00059     public CombinedPaper(RealPaper paper, Model model)throws JetWebException{
00060 
00061     paper_ = paper;
00062     modelId_ = model.getId();
00063 
00064     // now fill all the predicted plots for that model
00065     model.addAllRunSeries();
00066 
00067     for(RealPlot plot: paper.getPlots()){
00068         MergedPlot existingPlot = null;
00069         boolean foundData = false;
00070 
00071         for(RunSeries runseries: 
00072             model.getRunSeriesCollection().getRunSeriesList()){
00073 
00074         PredictedPlot newPlot = new PredictedPlot(plot, runseries);
00075 
00076         if(newPlot.hasData()){
00077             foundData = true;
00078             existingPlot = 
00079             predictedPlots_.get(newPlot.getId());
00080             if(existingPlot==null){
00081 
00082             existingPlot = new MergedPlot();
00083             }
00084 
00085             existingPlot.addPlot(newPlot, false);
00086             predictedPlots_.put(newPlot.getId(), existingPlot);
00087         }
00088         }
00089         if(foundData){
00090         existingPlot.mergePoints();
00091         }
00092     }
00093 
00094     }
00095 
00102     public CombinedPaper(CombinedPaper paper) 
00103     throws JetWebException{
00104 
00105     paper_ = paper.getRealPaper();
00106     fit_ = paper.getFit();
00107     modelId_ = paper.getModelId();
00108     scale_ = paper.getScale();
00109     chi2_ = paper.getFitChi2();
00110     dof_ = paper.getFitDOF();
00111     dirtyChi2_ = false;
00112     predictedPlots_ = 
00113         new HashMap<Integer, MergedPlot>(paper.getIndexedPredictedPlots());
00114     fittedPlots_ = 
00115         new HashMap<Integer, FittedPlot>(paper.getIndexedFittedPlots());
00116 
00117     }
00118 
00119     public CombinedPaper setFit(Fit fit){
00120     fit_ = fit;
00121 
00122     for(FittedPlot plot: fittedPlots_.values()){
00123         plot.setFit(fit);
00124     }
00125 
00126     dirtyChi2_ = true;
00127 
00128     return this;
00129     }
00130 
00131     public Fit getFit(){
00132     return fit_;
00133     }
00134 
00135     public Integer getModelId(){
00136     return modelId_;
00137     }
00138 
00139     public CombinedPaper add(PredictedPlot plot)
00140     throws JetWebException{
00141     MergedPlot existingPlot = predictedPlots_.get(plot.getId());
00142     if(existingPlot==null){
00143         existingPlot = new MergedPlot();
00144     }
00145 
00146     existingPlot.addPlot(plot);
00147     predictedPlots_.put(plot.getId(), existingPlot);
00148     dirtyChi2_ = true;
00149     return this;
00150     }
00151 
00152     public RealPaper getRealPaper(){
00153     return paper_;
00154     }
00155 
00156     public Collection<FittedPlot> getFittedPlots()
00157     throws JetWebException{
00158     
00159     if(fit_.getId()<0){
00160         fittedPlots_.clear();
00161 
00162         dirtyChi2_ = true;
00163         return fittedPlots_.values();
00164     }
00165 
00166     for(MergedPlot plot: getPredictedPlots()){
00167         
00168         FittedPlot fPlot = fittedPlots_.get(plot.getId());
00169         
00170         if(fPlot==null || 
00171            fPlot.getFit()==null || 
00172            fPlot.getFit().getId()!=fit_.getId()){
00173 
00174         fPlot = new FittedPlot(plot, fit_);
00175 
00176         //set other stuff here needed for fittedplot 
00177         //(not sure if there is any)
00178 
00179         fittedPlots_.put(plot.getId(), fPlot);
00180         dirtyChi2_ = true;
00181         }
00182     }
00183 
00184     return fittedPlots_.values();
00185     }
00186 
00187     public Collection<MergedPlot> getPredictedPlots(){
00188     return predictedPlots_.values();
00189     }
00190 
00191     public HashMap<Integer, MergedPlot> getIndexedPredictedPlots(){
00192     return predictedPlots_;
00193     }
00194 
00195     public HashMap<Integer, FittedPlot> getIndexedFittedPlots(){
00196     return fittedPlots_;
00197     }
00198 
00199     public Integer getNPredictedPlots(){
00200     return predictedPlots_.size();
00201     }
00202 
00203     public CombinedPaper setScale(Double scale){
00204     scale_ = scale;
00205     dirtyChi2_ = true;
00206     return this;
00207     }
00208 
00209     public Double getScale(){
00210     return scale_;
00211     }
00212 
00214 
00221     public Double getFitChi2(){
00222     return chi2_;
00223     }
00224 
00231     public Double getFitChi2(PlotSelection selection)
00232     throws JetWebException{
00233 
00234     if(!dirtyChi2_) return chi2_;
00235     dirtyChi2_ = false;
00236     Double chi2 = 0.0;
00237 
00238     Vector<Integer> wantedIds = selection.getPlotIds();
00239 
00240     for(Map.Entry<Integer, MergedPlot> entry: predictedPlots_.entrySet()){
00241         if(wantedIds.contains(entry.getKey())&&
00242            entry.getValue().hasData()){
00243 
00244         RealPlot realPlot = paper_.getPlot(entry.getKey());
00245         Double localScale = 1.0;
00246 
00247         if(!entry.getValue().isShape()){
00248             localScale = scale_;
00249         }
00250 
00251         if(realPlot!=null){
00252             Vector<DataPoint> mcPts = entry.getValue().getDataPoints();
00253             Vector<DataPoint> realPts = realPlot.getDataPoints();
00254             Vector<DataPoint> fPts = new Vector<DataPoint>();
00255 
00256             //sort the data points so they are in the same order
00257 
00258             Collections.sort(mcPts);
00259             Collections.sort(realPts);
00260 
00261             if(mcPts.size()==realPts.size()){
00262             Double sysScale = realPlot.getSysScale();
00263             for(Integer pntNum=0; pntNum!=mcPts.size(); ++pntNum){
00264                 DataPoint mcPt = mcPts.get(pntNum);
00265                 DataPoint realPt = realPts.get(pntNum);
00266                 
00267                 if( mcPt.getYUp()!=0.0 && 
00268                 realPt.getYUp() !=0.0){
00269                 
00270                 Double errorMc = 
00271                     0.5 * localScale*
00272                     (mcPt.getYUp()+mcPt.getYDown());
00273                     
00274                 Double error2 = 0.5 * sysScale *
00275                     (realPt.getYUp() + realPt.getYDown());
00276 
00277                 error2 = error2 * error2;
00278                 error2 = error2 + errorMc*errorMc;
00279 
00280                 Double incr = mcPt.getY()*localScale - 
00281                     realPt.getY()*sysScale;
00282                 incr = incr * incr / error2;
00283                 
00284                 if(incr.isNaN() || incr.isInfinite()){
00285                     System.out.println
00286                     ("Error: plot" + 
00287                      entry.getValue().getNumber());
00288                     System.out.println
00289                     (error2+" , "+ mcPt.toString()+":"+
00290                      realPt.toString());
00291 
00292                 }
00293                 
00294                 chi2 = chi2 + incr;
00295                 mcPt.setChi2(incr);
00296                 DataPoint fPt = (DataPoint)mcPt.clone();
00297                 fPt.setY(mcPt.getY()*localScale);
00298                 fPt.setYUp(mcPt.getYUp()*localScale);
00299                 fPt.setYDown(mcPt.getYDown()*localScale);
00300                 fPts.add(fPt);
00301                 }
00302             }
00303             }
00304             
00305             FittedPlot fPlot = fittedPlots_.get(entry.getKey());
00306             if(fPlot==null){
00307             fPlot = new FittedPlot(entry.getValue(), fit_);
00308             }
00309             fPlot.setDataPoints(fPts);
00310             fittedPlots_.put(entry.getKey(), fPlot);
00311         }
00312         }
00313     }
00314 
00315     return chi2;
00316     }
00318     public Double getFitDOF(){
00319     return 1.0;
00320     }
00321 
00322 }

Generated Wed Jan 17 09:14:27 GMT 2007