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

NuMatrix1D.cxx

Go to the documentation of this file.
00001 
00002 // NuMatrix1D.cxx
00003 // A slightly less abstract, 1D data storage class
00005 
00006 #include <cassert>
00007 
00008 #include "NuMatrix1D.h"
00009 #include "NtupleUtils/NuMatrixSpectrum.h"
00010 #include "NtupleUtils/NuUtilities.h"
00011 
00012 #include "MessageService/MsgService.h"
00013 #include "Conventions/Munits.h"
00014 
00015 #include "TFile.h"
00016 #include "TH1D.h"
00017 #include "TMath.h"
00018 #include "TGraph.h"
00019 
00020 CVSID("$Id: NuMatrix1D.cxx,v 1.3 2010/01/11 09:43:30 nickd Exp $");
00021 
00022 ClassImp(NuMatrix1D)
00023 
00024 
00025 // Constructors
00026 
00027 NuMatrix1D::NuMatrix1D() : NuMatrix(), fSpectrum(0)
00028 {
00029   
00030 }
00031 
00032 // The POT-only constructor
00033 NuMatrix1D::NuMatrix1D(Double_t POT) : NuMatrix(POT), fSpectrum(0)
00034 {
00035   
00036 }
00037 
00038 // The spectrum-only constructor
00039 NuMatrix1D::NuMatrix1D(const TH1D& spectrum) : NuMatrix(0), fSpectrum(0)
00040 {
00041   fSpectrum = new TH1D(spectrum);
00042   fSpectrum->SetDirectory(0);
00043 }
00044 
00045 // The spectrum *and* POT constructor
00046 NuMatrix1D::NuMatrix1D(const TH1D& spectrum, Double_t POT) : NuMatrix(POT), fSpectrum(0)
00047 {
00048   fSpectrum = new TH1D(spectrum);
00049   fSpectrum->SetDirectory(0);
00050 }
00051 
00052 // The copy-constructor
00053 NuMatrix1D::NuMatrix1D(const NuMatrix1D& original) : NuMatrix(original), fSpectrum(0)
00054 {
00055   if (original.fSpectrum) {
00056     fSpectrum = new TH1D(*original.fSpectrum);
00057     fSpectrum->SetDirectory(0);
00058   }
00059 }
00060 
00061 // The assignment operator
00062 NuMatrix1D & NuMatrix1D::operator=(const NuMatrix1D& orig)
00063 {
00064   if (this != &orig) {
00065     // Copy the base class stuff
00066     this->NuMatrix::operator=(orig);
00067   
00068     // Copy the histogram
00069     if (fSpectrum) delete fSpectrum; fSpectrum = 0;
00070     if (orig.fSpectrum) {
00071       fSpectrum = new TH1D(*orig.fSpectrum);
00072       fSpectrum->SetDirectory(0);
00073     }
00074   }  
00075   return *this;
00076 }
00077 
00078 // Copy a NuMatrixSpectrum
00079 NuMatrix1D::NuMatrix1D(const NuMatrixSpectrum& original) : fSpectrum(0)
00080 {
00081   // Copy the POT from the NuMatrixSpectrum
00082   ResetPOT(original.PoT());
00083   
00084   // Copy the spectrum from a NuMatrixSpectrum!
00085   fSpectrum = new TH1D(*original.Spectrum());
00086 }
00087 
00088 // Assigned from a NuMatrixspectrum
00089 NuMatrix1D &NuMatrix1D::operator=(const NuMatrixSpectrum& orig)
00090 {
00091   // Copy the POT count
00092   this->ResetPOT(orig.PoT());
00093   
00094   // Copy the spectrum from a NuMatrixSpectrum!
00095   if (fSpectrum) delete fSpectrum;
00096   fSpectrum = new TH1D(*orig.Spectrum());
00097   
00098   return *this;
00099 }
00100 
00102 
00103 NuMatrix1D::~NuMatrix1D()
00104 {
00105   if (fSpectrum) delete fSpectrum; fSpectrum = 0;
00106 }
00107 
00109 // Regular member functions ////////////////////////////////////////////
00111 
00112 Double_t NuMatrix1D::StatsLikelihood(const NuMatrix* with) const
00113 {
00114   // Convert the NuMatrix to a NuMatrix1D
00115   const NuMatrix1D *other = dynamic_cast<const NuMatrix1D*>(with);
00116   if (other == 0) {
00117     MSG("NuMatrix1D",Msg::kWarning) << "Attempting to compare a NuMatrix1D to something other than a NuMatrix1D" << endl;
00118     return -1;
00119   }
00120   
00121   // // Do the statistical comparison
00122   // MSG("NuMatrix1D",Msg::kError) << "Comparing statistical likelihood - but function not implemented" << endl;
00123   // return 0;
00124   return this->StatsLikelihood(other);
00125 }
00126 
00128 
00129 void NuMatrix1D::ScaleToPOT(Double_t new_POT)
00130 {
00131   // check we have POT and a spectrum
00132   if (GetPOT() != 0.0 && fSpectrum) {
00133     Double_t scale_factor = new_POT / GetPOT();
00134     fSpectrum->Scale(scale_factor);
00135   }
00136   
00137   // Reset the internal POT
00138   ResetPOT(new_POT);
00139 }
00140 
00142 
00143 // Load the 1D Matrix from a histogram, with a given POT
00144 NuMatrix1D::NuMatrix1D( const TString &filename,
00145                         const TString &histname,
00146                         Double_t POT)
00147   : NuMatrix(), fSpectrum(0)
00148 {
00149   // Attempt to open the named file
00150   TFile tf(filename);
00151   if (!tf.IsOpen())
00152   {
00153     MSG("NuMatrix1D",Msg::kError) << "Cannot open file " << filename << endl;
00154     return;
00155   }
00156   
00157   // Now we have opened, grab the histogram
00158   fSpectrum = dynamic_cast<TH1D*>(tf.Get(histname));
00159   // Detach the spectrum from the file
00160   fSpectrum->SetDirectory(0);
00161   
00162   // Check this worked
00163   if (fSpectrum == 0)
00164   {
00165     MSG("NuMatrix1D",Msg::kError) << "Cannot read histogram " << histname
00166       << " from file " << filename << endl;
00167     return;
00168   }
00169   
00170   // Now we have successfully read the histogram, set the POT
00171   this->ResetPOT(POT);
00172   
00173 }
00174 
00176 
00177 // Load the 1D matrix from a file, and read the POT from another histogram
00178 NuMatrix1D::NuMatrix1D( const TString &filename,
00179                         const TString &histname,
00180                         const TString &pothistname)
00181                       : NuMatrix(), fSpectrum(0)
00182 {
00183   // Attempt to open the named file
00184   TFile tf(filename);
00185   if (!tf.IsOpen())
00186   {
00187     MSG("NuMatrix1D",Msg::kError) << "Cannot open file " << filename << endl;
00188     return;
00189   }
00190   
00191   // Only try to read the histogram if we have been given a name!
00192   if (histname != "") {
00193     // Now we have opened, grab the histogram
00194     TH1D *spect = dynamic_cast<TH1D*>(tf.Get(histname));
00195     // Check this worked
00196     if (spect == 0)
00197     {
00198       MSG("NuMatrix1D",Msg::kError) << "Cannot read histogram " << histname
00199         << " from file " << filename << endl;
00200       return;
00201     }
00202     fSpectrum = new TH1D(*spect);
00203     // Detach the spectrum from the file
00204     fSpectrum->SetDirectory(0);
00205   }
00206   
00207   // Now we have successfully read the histogram, get the POT histogram
00208   TH1 *hTotalPot = dynamic_cast<TH1*>(tf.Get(pothistname));
00209   if (hTotalPot == 0)
00210   {
00211     MSG("NuMatrix1D",Msg::kError) << "Cannot read POT histogram " << pothistname
00212       << " from file " << filename << endl;
00213     fSpectrum = 0;
00214     return;
00215   }
00216   // Set the POT from this histogram
00217   this->ResetPOT(hTotalPot->Integral());
00218 }
00219 
00221 
00222 // Returns a NuMAtrixSpectrum
00223 NuMatrixSpectrum NuMatrix1D::GetNuMatrixSpectrum(void) const
00224 {
00225   return NuMatrixSpectrum(*fSpectrum, GetPOT());
00226 }
00227 
00229 
00230 void NuMatrix1D::Divide(const NuMatrix &by)
00231 {
00232   // Convert the NuMatrix to a NuMatrix1D
00233   const NuMatrix1D *other = dynamic_cast<const NuMatrix1D*>(&by);
00234   if (other == 0) {
00235     MSG("NuMatrix1D",Msg::kWarning) << "Attempting to divide a NuMatrix1D to something other than a NuMatrix1D" << endl;
00236   }
00237   
00238   this->Divide(*other);
00239 }
00240 
00242 
00243 void NuMatrix1D::Divide(const NuMatrix1D &by)
00244 {
00245   fSpectrum->Divide(by.Spectrum());
00246 }
00247 
00249 
00250 void NuMatrix1D::Divide(const TH1& correction, const Option_t *option)
00251 {
00252   fSpectrum->Divide(fSpectrum, &correction, 1, 1, option);
00253 }
00254 
00256 
00257 void NuMatrix1D::Divide(const TGraph &correction)
00258 {
00259   // copied form NuMatrixSpectrum
00260   for(int i=1; i<=fSpectrum->GetNbinsX(); i++) {
00261     fSpectrum->SetBinContent(i,fSpectrum->GetBinContent(i) /
00262                                correction.Eval(fSpectrum->GetBinCenter(i),0,""));
00263     fSpectrum->SetBinError(i,fSpectrum->GetBinError(i) /
00264                              correction.Eval(fSpectrum->GetBinCenter(i),0,""));
00265   }
00266   
00267 }
00268 
00270 
00271 void NuMatrix1D::Multiply(const NuMatrix &by)
00272 {
00273   // Convert the NuMatrix to a NuMatrix1D
00274   const NuMatrix1D *other = dynamic_cast<const NuMatrix1D*>(&by);
00275   if (other == 0) {
00276     MSG("NuMatrix1D",Msg::kWarning) << "Attempting to multiply a NuMatrix1D to something other than a NuMatrix1D" << endl;
00277   }
00278   
00279   this->Multiply(*other);
00280 }
00281 
00283 
00284 void NuMatrix1D::Multiply(const NuMatrix1D &by)
00285 {
00286   fSpectrum->Multiply(by.Spectrum());
00287 }
00288 
00290 
00291 void NuMatrix1D::Multiply(const TH1& correction, const Option_t *option)
00292 {
00293   fSpectrum->Multiply(fSpectrum, &correction, 1, 1, option);
00294 }
00295 
00297 
00298 void NuMatrix1D::Multiply(const TGraph &correction)
00299 {
00300   // copied form NuMatrixSpectrum
00301   for(int i=1; i<=fSpectrum->GetNbinsX(); i++) {
00302     fSpectrum->SetBinContent(i,fSpectrum->GetBinContent(i) *
00303                                correction.Eval(fSpectrum->GetBinCenter(i),0,""));
00304     fSpectrum->SetBinError(i,fSpectrum->GetBinError(i) *
00305                              correction.Eval(fSpectrum->GetBinCenter(i),0,""));
00306   }
00307   
00308 }
00309 
00311 
00312 void NuMatrix1D::Oscillate(const Double_t dm2, const Double_t sn2)
00313 {
00314   // Copy the simple oscillation function from NuMatrixSpectrum for now
00315   for(int i=1;i<=fSpectrum->GetNbinsX();i++) {
00316     Double_t energy = fSpectrum->GetBinCenter(i);
00317     Double_t oscProb =
00318           1. - sn2*TMath::
00319           Power(TMath::Sin(1.27*dm2*(NuUtilities::FDDistance()/Munits::km)
00320           /energy),2);
00321     fSpectrum->SetBinContent(i,fSpectrum->GetBinContent(i)*oscProb);
00322     fSpectrum->SetBinError(i,fSpectrum->GetBinError(i)*oscProb);
00323   }
00324 }
00325 
00327 
00328 Double_t NuMatrix1D::StatsLikelihood(const NuMatrix1D* with) const
00329 {
00330   Double_t like = 0;
00331 
00332   //Aim to minimise -2lnL. That's what I'm returning.
00333   for (Int_t i=1; i<= fSpectrum->GetNbinsX(); ++i)
00334   {
00335     Double_t mnu = fSpectrum->GetBinContent(i);
00336     Double_t dnu = with->Spectrum()->GetBinContent(i);
00337 
00338     like += NuUtilities::LogLikelihood(dnu,mnu);
00339   }
00340   return like;
00341 }
00342 
00344 
00345 
00346 void NuMatrix1D::SetValue(Double_t val)
00347 {
00348   // No spectrum - don't set
00349   if (!fSpectrum) {
00350     MSG("NuMatrix1D",Msg::kWarning) << "Cannot set constant value: No histogram" << endl;
00351     return;
00352   }
00353   
00354   for (Int_t i = 1; i <= fSpectrum->GetNbinsX(); ++i)
00355   {
00356     fSpectrum->SetBinContent(i, val);
00357   }
00358 }
00359 
00361 
00362 Int_t NuMatrix1D::Write(const TString &name)
00363 {
00364   if (fSpectrum) {
00365     if (name == "")
00366     {
00367       return fSpectrum->Write();
00368     } else {
00369       return fSpectrum->Write(name);
00370     }
00371   } else {
00372     return 0;
00373   }
00374 }
00375   
00377 
00378 void NuMatrix1D::Scale(Double_t value)
00379 {
00380   if (fSpectrum) fSpectrum->Scale(value);
00381 }
00382 
00384 
00385 void NuMatrix1D::RecoToTrue(const TH2D &correction)
00386 {
00387   // MSG("NuMatrix1D",Msg::kWarning) << "RecoToTrue not implemented uet for NuMatrix1D" << endl;
00388   this->YToX(correction);
00389 }
00390 
00392 
00393 void NuMatrix1D::TrueToReco(const TH2D &correction)
00394 {
00395   // MSG("NuMatrix1D",Msg::kWarning) << "TrueToReco not implemented uet for NuMatrix1D" << endl;
00396   this->XToY(correction);
00397 }
00398 
00400 
00401 void NuMatrix1D::ExtrapolateNDToFD(const TH2D &correction)
00402 {
00403   // MSG("NuMatrix1D",Msg::kWarning) << "ExtrapolateNDToFD not implemented uet for NuMatrix1D" << endl;
00404   this->XToY(correction);
00405 }
00406 
00408 
00409 void NuMatrix1D::XToY(const TH2D &correction)
00410 {
00411   // This routine taken from NuMatrixspectrum - looks suspect (especially
00412   // with the differences between the two functions). Do quick validation.
00413   assert(fSpectrum->GetNbinsX() == correction.GetNbinsX());
00414   assert(correction.GetNbinsX() == correction.GetNbinsY());
00415   
00416   //working space:
00417   Double_t *val = new Double_t[fSpectrum->GetNbinsX()+1];  
00418   for(int i=1;i<=fSpectrum->GetNbinsX();i++){
00419     val[i-1] = 0;
00420   }
00421 
00422   for(int i=1;i<=fSpectrum->GetNbinsX();i++){ //loop over true
00423     for(int j=1;j<=fSpectrum->GetNbinsX()+1;j++){ //loop over reco
00424       val[j-1] += ( fSpectrum->GetBinContent(i) * correction.GetBinContent(i,j));
00425     }
00426   }
00427   for(int i=1;i<=fSpectrum->GetNbinsX()+1;i++) {
00428     fSpectrum->SetBinContent(i,val[i-1]);
00429   }
00430   if (val) {delete[] val; val = 0;}
00431 }
00432 
00433 //____________________________________________________________________72
00434 void NuMatrix1D::YToX(const TH2D &correction)
00435 {
00436   // This routine taken from NuMatrixspectrum - looks suspect (especially
00437   // with the differences between the two functions). Do quick validation.
00438   assert(fSpectrum->GetNbinsX() == correction.GetNbinsX());
00439   assert(correction.GetNbinsX() == correction.GetNbinsY());
00440   
00441   //working space:
00442   Double_t *val = new Double_t[fSpectrum->GetNbinsX()+1];
00443   for(int i=1;i<=fSpectrum->GetNbinsX();i++) { val[i-1] = 0; }
00444   
00445   for(int i=1;i<=fSpectrum->GetNbinsX();i++){
00446     for(int j=1;j<=correction.GetNbinsY();j++){
00447       val[j-1] += ( fSpectrum->GetBinContent(i) * correction.GetBinContent(j,i) );
00448     }
00449   }
00450   for(int i=1;i<=fSpectrum->GetNbinsX();i++) {
00451     fSpectrum->SetBinContent(i,val[i-1]);
00452   }
00453   if (val) {delete[] val; val = 0;}
00454   return;
00455 }
00456 
00457 
00459 
00460 
00461 
00462 
00463 
00464 

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