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

NuMatrix2D.cxx

Go to the documentation of this file.
00001 
00002 // NuMatrix2D.cxx
00003 // A slightly less abstract, 2D data storage class
00005 
00006 #include "NuMatrix2D.h"
00007 #include "NuUtilities.h"
00008 
00009 #include "MessageService/MsgService.h"
00010 
00011 #include "TH2D.h"
00012 
00013 CVSID("$Id: NuMatrix2D.cxx,v 1.3 2009/11/30 13:03:10 nickd Exp $");
00014 
00015 ClassImp(NuMatrix2D)
00016 
00017 void Inst2d(void)
00018 {
00019   NuMatrix2D a;
00020 }
00022 // Constructors
00023 
00024 NuMatrix2D::NuMatrix2D() : NuMatrix(), fSpectrum(0)
00025 {
00026   
00027 }
00028 
00029 // The POT-only constructor
00030 NuMatrix2D::NuMatrix2D(Double_t POT) : NuMatrix(POT), fSpectrum(0)
00031 {
00032   
00033 }
00034 
00035 // The spectrum-only constructor
00036 NuMatrix2D::NuMatrix2D(const TH2D& spectrum) : NuMatrix(0), fSpectrum(0)
00037 {
00038   fSpectrum = new TH2D(spectrum);
00039 }
00040 
00041 // The spectrum *and* POT constructor
00042 NuMatrix2D::NuMatrix2D(const TH2D& spectrum, Double_t POT) : NuMatrix(POT), fSpectrum(0)
00043 {
00044   fSpectrum = new TH2D(spectrum);
00045 }
00046 
00047 // The copy-constructor
00048 NuMatrix2D::NuMatrix2D(const NuMatrix2D& original) : NuMatrix(original), fSpectrum(0)
00049 {
00050   fSpectrum = new TH2D(*original.fSpectrum);
00051 }
00052 
00053 // The assignment operator
00054 NuMatrix2D & NuMatrix2D::operator=(const NuMatrix2D& orig)
00055 {
00056   if (this == &orig) return *this;
00057   
00058   // Copy the base class stuff
00059   this->NuMatrix::operator=(orig);
00060   
00061   // Copy the histogram
00062   if (fSpectrum) delete fSpectrum;
00063   fSpectrum = new TH2D(*orig.fSpectrum);
00064   
00065   return *this;
00066 }
00067 
00069 
00070 NuMatrix2D::~NuMatrix2D()
00071 {
00072   if (fSpectrum) delete fSpectrum; fSpectrum = 0;
00073 }
00074 
00076 // Regular member functions ////////////////////////////////////////////
00078 
00079 Double_t NuMatrix2D::StatsLikelihood(const NuMatrix* with) const
00080 {
00081   // Convert the NuMatrix to a NuMatrix2D
00082   const NuMatrix2D *other = dynamic_cast<const NuMatrix2D*>(with);
00083   if (other == 0) {
00084     MSG("NuMatrix2D",Msg::kWarning) << "Attempting to compare a NuMatrix2D to something other than a NuMatrix2D" << endl;
00085     return -1;
00086   }
00087   
00088   // // Do the statistical comparison
00089   // MSG("NuMatrix2D",Msg::kError) << "Comparing statistical likelihood - but function not implemented" << endl;
00090   // return 0;
00091   return this->StatsLikelihood(other);
00092 }
00093 
00095 
00096 Double_t NuMatrix2D::StatsLikelihood(const NuMatrix2D* with) const
00097 {
00098   // Do the same as the 1D, but over all bins
00099   Double_t like = 0.0;
00100   
00101   //Aim to minimise -2lnL. That's what I'm returning.
00102   for (Int_t i=1; i<= fSpectrum->GetNbinsX(); ++i)
00103   {
00104     for (Int_t j=1; j <= fSpectrum->GetNbinsY(); ++j)
00105     {
00106       //Bizarre limits because root histograms are silly
00107       Double_t mnu = fSpectrum->GetBinContent(i, j);
00108       Double_t dnu = with->Spectrum()->GetBinContent(i, j);
00109 
00110       like += NuUtilities::LogLikelihood(dnu,mnu);
00111     }
00112   }
00113   
00114   return like;
00115 }
00116 
00118 
00119 void NuMatrix2D::ScaleToPOT(Double_t new_POT)
00120 {
00121   // If the old POT is zero, or the spectrum does not exist
00122   if (GetPOT() == 0.0 || fSpectrum == 0) {
00123     // Simply set the new POT (avoids infinite scaling)
00124     ResetPOT(new_POT);
00125   } else {  
00126     // The spectrum must exist - calculate the scale factor
00127     Double_t scale_factor = new_POT / GetPOT();
00128     fSpectrum->Scale(scale_factor);
00129   }
00130 }
00131 
00133 
00134 void NuMatrix2D::Scale(Double_t value)
00135 {
00136   fSpectrum->Scale(value);
00137 }
00138 
00140 
00142 void NuMatrix2D::Divide(const NuMatrix &correction)
00143 {
00144   // Convert the NuMatrix to a NuMatrix1D
00145   const NuMatrix2D *other = dynamic_cast<const NuMatrix2D*>(&correction);
00146   if (other == 0) {
00147     MSG("NuMatrix2D",Msg::kWarning) << "Attempting to divide a NuMatrix2D to something other than a NuMatrix2D" << endl;
00148   }
00149   
00150   this->Divide(*other);
00151 }
00152 
00154 
00155 void NuMatrix2D::Divide(const NuMatrix2D &correction)
00156 {
00157   fSpectrum->Divide(correction.Spectrum());
00158 }
00159 
00161 
00162 void NuMatrix2D::Divide(const TH1& correction, const Option_t *option)
00163 {
00164   // fSpectrum->Divide(&correction);
00165   fSpectrum->Divide(fSpectrum, &correction, 1, 1, option);
00166 }
00167 
00169 
00170 void NuMatrix2D::Divide(const TGraph &)
00171 {
00172   MSG("NuMatrix2D",Msg::kWarning)
00173     << "2D histograms cannot be corrected with graphs without further information"
00174     << endl;
00175 }
00176 
00178 
00179 
00181 void NuMatrix2D::Multiply(const NuMatrix &correction)
00182 {
00183   // Convert the NuMatrix to a NuMatrix1D
00184   const NuMatrix2D *other = dynamic_cast<const NuMatrix2D*>(&correction);
00185   if (other == 0) {
00186     MSG("NuMatrix2D",Msg::kWarning) << "Attempting to divide a NuMatrix2D to something other than a NuMatrix2D" << endl;
00187   }
00188   
00189   this->Multiply(*other);
00190 }
00191 
00193 
00194 void NuMatrix2D::Multiply(const NuMatrix2D &correction)
00195 {
00196   fSpectrum->Multiply(correction.Spectrum());
00197 }
00198 
00200 
00201 void NuMatrix2D::Multiply(const TH1& correction, const Option_t *option)
00202 {
00203   // fSpectrum->Multiply(&correction);
00204   fSpectrum->Multiply(fSpectrum, &correction, 1, 1, option);
00205 }
00206 
00208 
00209 void NuMatrix2D::Multiply(const TGraph &)
00210 {
00211   MSG("NuMatrix2D",Msg::kWarning)
00212     << "2D histograms cannot be corrected with graphs without further information"
00213     << endl;
00214 }
00215 
00217 
00218 // Matrix method functions
00219 void NuMatrix2D::RecoToTrue(const TH2D &)
00220 {
00221   MSG("NuMatrix2D",Msg::kWarning) << "Base NuMatrix2D cannot Reco->True" << endl;
00222 }
00223 
00225 
00226 void NuMatrix2D::TrueToReco(const TH2D &)
00227 {
00228   MSG("NuMatrix2D",Msg::kWarning) << "Base NuMatrix2D cannot True->Reco" << endl;  
00229 }
00230 
00232 
00233 void NuMatrix2D::ExtrapolateNDToFD(const TH2D &)
00234 {
00235   MSG("NuMatrix2D",Msg::kWarning) << "Base NuMatrix2D cannot be extrapolated" << endl;
00236 }
00237 
00239 
00240 void NuMatrix2D::SetValue(Double_t val)
00241 {
00242   // No spectrum - don't set
00243   if (!fSpectrum) {
00244     MSG("NuMatrix1D",Msg::kWarning) << "Cannot set constant value: No histogram" << endl;
00245     return;
00246   }
00247   
00248   for (Int_t i = 1; i <= fSpectrum->GetNbinsX(); ++i)
00249   {
00250     for (Int_t j = 1; j <= fSpectrum->GetNbinsY(); ++j)
00251     {
00252       fSpectrum->SetBinContent(i, j, val);
00253     }
00254   }
00255 }
00256 
00258 
00259 Int_t NuMatrix2D::Write(const TString &name)
00260 {
00261   if (fSpectrum) {
00262     if (name == "")
00263     {
00264       return fSpectrum->Write();
00265     } else {
00266       return fSpectrum->Write(name);
00267     }
00268   } else {
00269     return 0;
00270   }
00271 }
00272 
00274 
00275 void NuMatrix2D::Oscillate(const Double_t , const Double_t )
00276 {
00277   MSG("NuMatrix2D",Msg::kWarning) << "Base NuMatrix2D cannot be oscillated" << endl;  
00278 }
00279   
00281 
00282 void NuMatrix2D::Draw(Option_t* option)
00283 {
00284   if(fSpectrum) {
00285     fSpectrum->Draw(option);
00286   } else {
00287     MSG("NuMatrix2D",Msg::kWarning) << "No spectrum exists: Cannot draw" << endl;
00288   }
00289 }
00290   

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