Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

NuEZFitter.cxx

Go to the documentation of this file.
00001 #include <string>
00002 #include <map>
00003 #include <iostream>
00004 
00005 #include "NtupleUtils/NuMatrixSpectrum.h"
00006 #include "NtupleUtils/NuMMHelperCPT.h"
00007 #include "NtupleUtils/NuMMRunFC.h"
00008 #include "NtupleUtils/NuSystFitter.h"
00009 #include "NtupleUtils/NuMMParameters.h"
00010 
00011 #include "NtupleUtils/NuEZFitter.h"
00012 
00013 #include "MessageService/MsgService.h"
00014 
00015 CVSID("$Id: NuEZFitter.cxx,v 1.4 2009/10/15 16:41:38 ahimmel Exp $");
00016 
00017 using namespace std;
00018 
00019 void NuEZFitter::Init(void)
00020 {
00021   fNDDataPQ = fNDDataNQ = fFDDataPQ = fFDDataNQ = 0;
00022   fHelper = 0; fRun = 0; fFitter = 0; fFitResult = 0;
00023   
00024   // Make a dummy fit result
00025   fFitResult = new NuMMParameters();
00026   fFitResult->MinuitPass(false);
00027 }
00028 
00029 void NuEZFitter::Init_Helper_Near(string Helper, string NeardetData)
00030 {
00031   // Reading near detector data
00032   MSG("NuEZFitter",Msg::kInfo) << "Reading Near detector data " << NeardetData << endl;
00033   fNDDataPQ = new NuMatrixSpectrum(NeardetData, "RecoEnergyPQ_ND");
00034   fNDDataNQ = new NuMatrixSpectrum(NeardetData, "RecoEnergy_ND");
00035   
00036   // Grab the helper file
00037   MSG("NuEZFitter",Msg::kInfo) << "Extrapolating with helper " << Helper << endl;
00038   string xsec = "$SRT_PRIVATE_CONTEXT/NtupleUtils/data/xsec_minos_modbyrs4_v3_5_0_mk.root";
00039   fHelper = new NuMMHelperCPT(Helper.c_str(),xsec);
00040 }
00041 NuEZFitter::NuEZFitter(string Helper, string NeardetData) // :
00042  //  fNDDataPQ(0), fNDDataNQ(0), fFDDataPQ(0), fFDDataNQ(0),
00043  //  fHelper(0), fRun(0), fFitter(0), fFitResult(0)
00044 {
00045   Init();
00046   Init_Helper_Near(Helper, NeardetData);
00047   Rebuild();
00048 }
00049 
00050 NuEZFitter::NuEZFitter(string Helper, string NeardetData, string FardetData)
00051 {
00052   Init();
00053   Init_Helper_Near(Helper, NeardetData);
00054   UseFDData(FardetData);
00055 }
00056 
00057 NuEZFitter::NuEZFitter(const NuEZFitter &o)
00058 { 
00059   Init();
00060   
00061   if (o.fNDDataPQ) fNDDataPQ = new NuMatrixSpectrum(*o.fNDDataPQ);
00062   if (o.fNDDataNQ) fNDDataNQ = new NuMatrixSpectrum(*o.fNDDataNQ);
00063   if (o.fFDDataPQ) fFDDataPQ = new NuMatrixSpectrum(*o.fFDDataPQ);
00064   if (o.fFDDataNQ) fFDDataNQ = new NuMatrixSpectrum(*o.fFDDataNQ);
00065   
00066   if (o.fHelper) fHelper = new NuMMHelperCPT(*o.fHelper);
00067   if (o.fRun) fRun = new NuMMRunFC(*o.fRun);
00068   if (o.fFitter) fFitter = new NuSystFitter(*o.fFitter);
00069   if (o.fFitResult) fFitResult = new NuMMParameters(*o.fFitResult);
00070 }
00071 
00072 NuEZFitter::~NuEZFitter()
00073 {
00074   // Free all of our memory
00075   if (fNDDataPQ) delete fNDDataPQ; fNDDataPQ = 0;
00076   if (fNDDataNQ) delete fNDDataNQ; fNDDataNQ = 0;
00077   
00078   if (fFDDataPQ) delete fFDDataPQ;  fFDDataPQ = 0;
00079   if (fFDDataNQ) delete fFDDataNQ;  fFDDataNQ = 0;
00080 
00081   if (fHelper) delete fHelper; fHelper = 0;
00082   if (fRun) delete fRun; fRun = 0;
00083   if (fFitter) delete fFitter; fFitter = 0;
00084 
00085   if (fFitResult) delete fFitResult; fFitResult = 0;
00086 // if () delete ;  = 0;
00087 
00088 }
00089 
00090 //void NuEZFitter::Make_Blank_Fardet(void)
00091 //{
00092 //
00093 //}
00094 
00095 void NuEZFitter::Rebuild(void)
00096 {
00097   // Check the needed plots exist
00098   if (!(fHelper || fNDDataNQ || fNDDataPQ || fFDDataNQ || fFDDataPQ)) {
00099     // We don't have all the required plots - just return without doing anything
00100     MSG("NuEZFitter",Msg::kWarning) << "Rebuild() was called without all the plots in place" << endl;
00101     return;
00102   }
00103   
00104   // Delete the old objects if necessary
00105   if (fRun) delete fRun; fRun = 0;
00106   if (fFitter) delete fFitter; fFitter = 0;
00107   
00108   // Build the run object
00109   // Use a different initialisation if we have no far detector data
00110   if (!(fFDDataPQ && fFDDataNQ)) {
00111     fRun = new NuMMRunFC(fHelper, fNDDataNQ, fNDDataPQ, 0);
00112   } else {
00113     fRun = new NuMMRunFC(fHelper, fNDDataNQ, fNDDataPQ, fFDDataNQ, fFDDataPQ);
00114   }
00115 
00116   // Limit the maximum fitting energy
00117   fRun->SetMaximumEnergy(49.99999);
00118   
00119   // And the fitter
00120   fFitter = new NuSystFitter();
00121   fFitter->push_back(fRun);
00122 }
00123 
00124 void NuEZFitter::MakeFDData(Double_t POT, Double_t dm2bar, Double_t sn2bar, Double_t dm2, Double_t sn2)
00125 {
00126     // Make the extrapolator
00127     NuMatrixSpectrum blankpot(POT);
00128     NuMMRunFC mmFakeFar(fHelper, fNDDataNQ, fNDDataPQ, &blankpot, &blankpot);
00129     mmFakeFar.QuietModeOn();
00130     mmFakeFar.SetMaximumEnergy(49.999);
00131  
00132     NuMMParameters mmPars;
00133     mmPars.Dm2(dm2);
00134     mmPars.Sn2(sn2);
00135     mmPars.Dm2Bar(dm2bar);
00136     mmPars.Sn2Bar(sn2bar);
00137     
00138     // Clear any existing far detector spectra
00139     if (fFDDataPQ) delete fFDDataPQ; fFDDataPQ = 0;
00140     if (fFDDataNQ) delete fFDDataNQ; fFDDataNQ = 0;
00141 
00142     // Grab the extrapolations
00143     // NuMatrixSpectrum *nqms = 0;
00144     // NuMatrixSpectrum *pqms = 0;
00145     mmFakeFar.MakeFDPredNoPair(mmPars, &fFDDataNQ, &fFDDataPQ);
00146     
00147     // Rebuild the internal objects
00148     Rebuild();
00149 }
00150 
00151 void NuEZFitter::UseFDData(string FardetData)
00152 {
00153   // Clear any existing far detector spectra
00154   if (fFDDataPQ) delete fFDDataPQ; fFDDataPQ = 0;
00155   if (fFDDataNQ) delete fFDDataNQ; fFDDataNQ = 0;
00156 
00157   MSG("NuEZFitter",Msg::kInfo) << "Reading Far detector data " << FardetData << endl;
00158   fFDDataPQ = new NuMatrixSpectrum(FardetData, "RecoEnergyPQ_FD");
00159   fFDDataNQ = new NuMatrixSpectrum(FardetData, "RecoEnergy_FD");
00160   
00161   // Rebuild the internal objects
00162   Rebuild();
00163 }
00164 
00165 void NuEZFitter::UseFDData(const NuMatrixSpectrum &fdNQdata, const NuMatrixSpectrum &fdPQdata)
00166 {
00167   // Clear any existing far detector spectra
00168   if (fFDDataPQ) delete fFDDataPQ; fFDDataPQ = 0;
00169   if (fFDDataNQ) delete fFDDataNQ; fFDDataNQ = 0;
00170   
00171   fFDDataNQ = new NuMatrixSpectrum(fdNQdata);
00172   fFDDataPQ = new NuMatrixSpectrum(fdPQdata);
00173   
00174   // Now rebuild the fitters
00175   Rebuild();
00176 }
00177   
00178 void NuEZFitter::UseFDFakeData(Double_t POT, std::string FardetData, std::string FarDetTauData)
00179 {
00180   // Clear any existing far detector spectra
00181   if (fFDDataPQ) delete fFDDataPQ; fFDDataPQ = 0;
00182   if (fFDDataNQ) delete fFDDataNQ; fFDDataNQ = 0;
00183   
00184   // Combine the fake far data and taudata then use as the spectrum
00185   MSG("NuEZFitter",Msg::kInfo) << "Reading Far detector fake-data " << FardetData << endl;
00186   MSG("NuEZFitter",Msg::kInfo) << "Reading Far detector fake-Tau data " << FarDetTauData << endl;
00187   
00188   NuMatrixSpectrum *fdDataPQ = new NuMatrixSpectrum(FardetData, "RecoEnergyPQ_ND");
00189   NuMatrixSpectrum *fdDataNQ = new NuMatrixSpectrum(FardetData, "RecoEnergy_ND");
00190   NuMatrixSpectrum *fdTauDataPQ = new NuMatrixSpectrum(FarDetTauData, "RecoEnergyPQ_ND");
00191   NuMatrixSpectrum *fdTauDataNQ = new NuMatrixSpectrum(FarDetTauData, "RecoEnergy_ND");
00192 
00193   if (fdDataPQ->PoT() < POT || fdTauDataPQ->PoT() < POT) {
00194     MSG("NuEZFitter",Msg::kWarning) << "Fake Data files have lower POT than requested - scaling data up" << endl;
00195   }
00196   
00197   fdDataPQ->ScaleToPot(POT);
00198   fdDataNQ->ScaleToPot(POT);
00199   fdTauDataPQ->ScaleToPot(POT);
00200   fdTauDataNQ->ScaleToPot(POT);
00201   
00202   fdDataPQ->Add(*fdTauDataPQ);
00203   fdDataNQ->Add(*fdTauDataNQ);
00204   
00205   fFDDataNQ = fdDataNQ;
00206   fFDDataPQ = fdDataPQ;
00207   
00208   // Rebuild the internal objects
00209   Rebuild();
00210 }
00211 
00212 void NuEZFitter::SetPoT(Double_t POT)
00213 {
00214   // If we don't have far detector spectra,
00215   if (!fFDDataPQ || !fFDDataNQ) {
00216     MSG("NuEZFitter",Msg::kWarning) << "No far detector spectra exists. Creating blank spectra" << endl;
00217     // In case we somehow have one of the two
00218     if (fFDDataPQ) delete fFDDataPQ; fFDDataPQ = 0;
00219     if (fFDDataNQ) delete fFDDataPQ; fFDDataPQ = 0;
00220     
00221     fFDDataPQ = new NuMatrixSpectrum(POT);
00222     fFDDataNQ = new NuMatrixSpectrum(POT);
00223   } else {
00224     fFDDataPQ->ResetPoT(POT);
00225     fFDDataNQ->ResetPoT(POT);
00226   }
00227 
00228   // Far detector spectra have changed... rebuild
00229   Rebuild();
00230 }
00231 
00232 void NuEZFitter::ScaleToPot(Double_t POT)
00233 {
00234     // If we don't have far detector spectra,
00235     if (!fFDDataPQ || !fFDDataNQ) {
00236         MSG("NuEZFitter",Msg::kWarning) << "No far detector spectra exists. Cannot rescale pot." << endl;
00237         return;
00238     } else {
00239         fFDDataPQ->ScaleToPot(POT);
00240         fFDDataNQ->ScaleToPot(POT);
00241     }
00242     
00243     // Far detector spectra have changed... rebuild
00244     Rebuild();
00245 }
00246 
00247 Double_t NuEZFitter::Chi2(Double_t dm2bar, Double_t sn2bar, Double_t dm2, Double_t sn2) const
00248 {
00249   NuMMParameters mmPars;
00250   mmPars.Dm2(dm2);
00251   mmPars.Sn2(sn2);
00252   mmPars.Dm2Bar(dm2bar);
00253   mmPars.Sn2Bar(sn2bar);
00254   
00255   // return (*fFitter)(mmPars.VectorParameters());
00256   return Chi2(mmPars);
00257 }
00258 
00259 Double_t NuEZFitter::Chi2(const NuMMParameters &params) const
00260 {
00261   if (fFitter) {
00262     return (*fFitter)(params.VectorParameters());
00263   } else {
00264     MSG("NuEZFitter",Msg::kError) << "No Fitter exists. Have you added FD data?" << endl;
00265     return 0;
00266   }
00267 }
00268 
00269 NuMatrixSpectrum NuEZFitter::PredictionNQ(Double_t dm2bar, Double_t sn2bar,
00270                                         Double_t dm2, Double_t sn2) const
00271 {
00272   NuMMParameters mmPars;
00273   mmPars.Dm2(dm2);
00274   mmPars.Sn2(sn2);
00275   mmPars.Dm2Bar(dm2bar);
00276   mmPars.Sn2Bar(sn2bar);
00277   
00278   return PredictionNQ(mmPars);
00279 }
00280 
00281 NuMatrixSpectrum NuEZFitter::PredictionPQ(Double_t dm2bar, Double_t sn2bar,
00282                                         Double_t dm2, Double_t sn2) const
00283 {
00284   NuMMParameters mmPars;
00285   mmPars.Dm2(dm2);
00286   mmPars.Sn2(sn2);
00287   mmPars.Dm2Bar(dm2bar);
00288   mmPars.Sn2Bar(sn2bar);
00289   
00290   return PredictionPQ(mmPars);
00291 }
00292 
00293 NuMatrixSpectrum NuEZFitter::PredictionNQ(const NuMMParameters &params) const
00294 {
00295   // Generate a prediction and return it in a memory-safe way
00296   NuMatrixSpectrum *pred = fRun->MakeFDPredNuMu(params);
00297   NuMatrixSpectrum ret(*pred);
00298   if (pred) delete pred; pred = 0;
00299   
00300   return ret;
00301 }
00302 
00303 NuMatrixSpectrum NuEZFitter::PredictionPQ(const NuMMParameters &params) const
00304 {
00305   // Generate a prediction and return it in a memory-safe way
00306   NuMatrixSpectrum *pred = fRun->MakeFDPredNuBar(params);
00307   NuMatrixSpectrum ret(*pred);
00308   if (pred) delete pred; pred = 0;
00309   
00310   return ret;
00311 }
00312 
00313 Bool_t NuEZFitter::Fit(const NuMMParameters &Initial)
00314 {
00315   // Can we do a fit?
00316   if (!fFitter) {
00317     MSG("NuEZFitter",Msg::kError) << "No Fitter exists. Have you added FD data?" << endl;
00318     fFitResult = new NuMMParameters(Initial);
00319     return false;
00320   }
00321   
00322   // Do the fit
00323   // Bool_t minuitresult = false;
00324   NuMMParameters mmFit = fFitter->Minimise(Initial);
00325   // Copy the fit result to our class member
00326   if (fFitResult) delete fFitResult; fFitResult = 0;
00327   fFitResult = new NuMMParameters(mmFit);
00328   
00329   return fFitResult->MinuitPass();
00330 }
00331   

Generated on Mon Feb 15 11:07:13 2010 for loon by  doxygen 1.3.9.1