NuFCEventManager Class Reference

#include <NuFCEventManager.h>

List of all members.

Public Member Functions

 NuFCEventManager (void)
TString SampleName (const NuFCSample &sample) const
void AddFile (const TString filename)
 Adds a file of NuFCEvents to this object.
void AddEvent (const NuFCEvent2 &nu, Double_t POT)
 Adds a single event to this object.
void Print (void) const
 Print a summary of events held.
const NuEventVector< NuFCEvent2 > & NDEvents (void) const
 Return the list of ND events.
const std::vector< NuFCSample > & Samples (void) const
 Return a list of FD samples.
const NuEventVector< NuFCEvent2 > & FDEvents (const NuFCSample &sample) const
 Returns the event vector for a single sample.
void ReweightSamplesPerBin (const NuMatrix1D &truebins)
UInt_t Size (void) const
 Returns the number of items held by this class.
UInt_t Bytes (void) const
 Returns the storage this object takes up, in bytes.

Private Member Functions

void CheckSample (const NuFCSample &sample, std::vector< NuFCSample > &vec)
NuFCSample Classify (const NuEvent &evt) const
 Classifies an event into different samples - use NuMMRun eventually.
void ReweightSamples (void)
 Reweight all of the samples to have a maximum reweight value.
void BuildSampleList (void)
 Builds the list of samples from our sample map.

Private Attributes

std::map< NuFCSample,
NuEventVector< NuFCEvent2 > > 
fFDSamples
 A map of far detector samples.
std::vector< NuFCSamplefSamples
NuEventVector< NuFCEvent2fNDEvents
 Near detector events are special: Store them separately.

Detailed Description

Manages a set of NuFCEvent2 objects. Class includes functions for randomly picking, reading and sorting reduced events.

Definition at line 18 of file NuFCEventManager.h.


Constructor & Destructor Documentation

NuFCEventManager::NuFCEventManager ( void   ) 

Definition at line 31 of file NuFCEventManager.cxx.

00032 {
00033   
00034 }


Member Function Documentation

void NuFCEventManager::AddEvent ( const NuFCEvent2 nu,
Double_t  POT 
)

Adds a single event to this object.

Definition at line 190 of file NuFCEventManager.cxx.

References BuildSampleList(), Classify(), NuFCEvent2::Detector(), fFDSamples, fNDEvents, Detector::kNear, and NuEventVector< T >::push_back().

Referenced by TestExperimentGenerator::testAccessSummary(), TestExperimentGenerator::testBuildSummaries(), TestEventManager::testEventAddFD(), TestEventManager::testEventAddND(), TestExperimentGenerator::testNDGenSingleEvent(), and TestEventManager::testReweightSamplesPerBin().

00191 {
00192   if (nu.Detector() == Detector::kNear)
00193   {
00194     fNDEvents.push_back(nu, POT);
00195   } else {
00196     NuFCSample smp = Classify(nu);
00197     
00198     if (smp == NuFCSample(0)) {
00199       return;
00200     }
00201     
00202     // Add this even to the samples list
00203     fFDSamples[smp].push_back(nu, POT);
00204   }
00205   // Rebuild the sample list
00206   BuildSampleList();
00207 }

void NuFCEventManager::AddFile ( const TString  filename  ) 

Adds a file of NuFCEvents to this object.

Definition at line 38 of file NuFCEventManager.cxx.

References BuildSampleList(), CheckSample(), Classify(), det, NuFCEvent2::Detector(), fFDSamples, fNDEvents, Msg::kError, Detector::kFar, Msg::kInfo, Detector::kNear, Detector::kUnknown, MSG, NuUtilities::ProgressBar(), NuEventVector< T >::push_back(), ReweightSamples(), and SampleName().

Referenced by NuFCRunInfo::AddFile(), and TestEventManager::testFileAdd().

00039 {
00040   MSG("NuFCEventManager",Msg::kInfo) << "Reading event library " << filename << ". " << endl;
00041   
00042   // Open the file
00043   TFile tf(filename);
00044   if (tf.IsZombie()) {
00045     MSG("NuFCEventManager",Msg::kError) << "Failed to open event library " << filename << endl;
00046     throw runtime_error("Failed to read MC events file");
00047   }
00048   
00049   // Find the POT histogram
00050   TObject *hobj= tf.Get("hTotalPot");
00051   TH1 *hTotalPot = dynamic_cast<TH1*>(hobj);
00052   if (!hTotalPot) {
00053     MSG("NuFCEventManager",Msg::kError) << "Failed to read hTotalPot in file " << filename << endl;
00054     throw runtime_error("Failed to read POT histogram");
00055   }
00056   // Record the POT of this file... even if we don't need it
00057   Double_t POT = hTotalPot->Integral();
00058   
00059   // Find the event tree
00060   TTree *tree = dynamic_cast<TTree*>(tf.Get("FCTree2"));
00061   if (!tree) {
00062     MSG("NuFCEventManager",Msg::kError) << "Failed to read Tree FCTree2 in file " << filename << endl;
00063     throw runtime_error("Failed to read Event tree");
00064   }
00065 
00066   // What detector is this library for?
00067   Detector::Detector_t det = Detector::kUnknown;
00068   
00069   // Read all the events out of this tree
00070   NuFCEvent2 *nu = new NuFCEvent2();
00071   tree->SetBranchAddress("NuFCEvent2", &nu);
00072   
00073   // Store a list of FD samples contained in this file
00074   vector<NuFCSample> readSamples;
00075   UInt_t             addCount = 0;
00076   
00077   // Loop through the file and read every event
00078   for (Int_t i=0; i < tree->GetEntries(); ++i)
00079   {
00080     // Update progress and grab the event
00081     NuUtilities::ProgressBar(i, tree->GetEntries()-1, 5);
00082     tree->GetEntry(i);
00083     
00084     // If this is the first event, store the unchanging vars
00085     if (i == 0) {
00086       det = nu->Detector();
00087       // Near detector is treated specially
00088       if (det == Detector::kNear) {
00089         // Make sure we have no events already
00090         assert(fNDEvents.size() == 0);
00091         // Set the POT count for the near detector
00092         // OBSELETE: The NuEventVector now stores this
00093         // fNDPot = POT;
00094       }
00095     } // first event
00096     
00097     // Validation: Make sure we don't have mixed-detector files
00098     assert (nu->Detector() == det);
00099     
00100     // Classify the event - is it near detector?
00101     if (nu->Detector() == Detector::kNear)
00102     {
00103       // Add this to our near event collection directly
00104       fNDEvents.push_back(*nu, POT);
00105       ++addCount;
00106     } else {
00107       // Classify the event
00108       // nu->PrintVars();
00109       
00110       NuFCSample smp = Classify(*nu);
00111 
00112       // Ignore events with a sample class of zero - the extrapolation doesn't need these
00113       if (smp == NuFCSample(0)) {
00114         // Just get the next event
00115         continue;
00116       }
00117       
00118       // Check that this file uniquely defines a sample
00119       // NOTE: We might not need this so much now that the event vector
00120       // can easily combine differing POT files, but there may be other
00121       // good reasons to still do this i.e. making sure we don't double-count
00122       CheckSample(smp, readSamples);
00123       
00124       // Add this even to the samples list
00125       fFDSamples[smp].push_back(*nu, POT);
00126       ++addCount;
00127       
00128     } // far detector event
00129     
00130   } // Every event in the tree
00131 
00132   // Reweight all of the samples
00133   if (det == Detector::kFar) ReweightSamples();
00134   
00135   // Rebuild the sample list
00136   BuildSampleList();
00137   
00138   // We have finished. Print out some info
00139   MSG("NuFCEventManager",Msg::kInfo) << "Added " << addCount << " events to detector: " << det << endl;
00140 
00141 
00142   // Print out a list of sample categories
00143   if (det == Detector::kFar) {
00144 
00145     cout << "Added events to " << readSamples.size() << " categories: ";
00146     
00147     for (UInt_t i = 0; i < readSamples.size(); ++i) {
00148       if (i != 0) cout  << ", ";;
00149       cout << SampleName(readSamples[i]);
00150         // << ": " << fFDSamples[readSamples[i]].size() << endl;
00151     }
00152     cout << endl;
00153   }
00154 
00155 }

void NuFCEventManager::BuildSampleList ( void   )  [private]

Builds the list of samples from our sample map.

Definition at line 337 of file NuFCEventManager.cxx.

References fFDSamples, fSamples, and it.

Referenced by AddEvent(), and AddFile().

00338 {
00339   fSamples.clear();
00340   std::map<NuFCSample, NuEventVector<NuFCEvent2> >::iterator it;
00341   for (it = fFDSamples.begin(); it != fFDSamples.end(); ++it)
00342   {
00343     fSamples.push_back((*it).first);
00344   }
00345 }

UInt_t NuFCEventManager::Bytes ( void   )  const

Returns the storage this object takes up, in bytes.

Definition at line 315 of file NuFCEventManager.cxx.

00316 {
00317   assert(0);
00318 }

void NuFCEventManager::CheckSample ( const NuFCSample sample,
std::vector< NuFCSample > &  vec 
) [private]

Only allow one file to have one sample. Verify that if a sample does not exist in the vector, and then check no events exist in the member function set either. This function updates the samples vector if everything is correct.

Referenced by AddFile().

NuFCSample NuFCEventManager::Classify ( const NuEvent evt  )  const [private]

Classifies an event into different samples - use NuMMRun eventually.

Definition at line 212 of file NuFCEventManager.cxx.

References NuEvent::charge, NuEvent::iaction, NuEvent::inu, NuEvent::inunoosc, Msg::kError, Sample::kNCNQ, Sample::kNCPQ, Sample::kSignalNQ, Sample::kSignalPQ, Sample::kTauNQ, Sample::kTauPQ, Sample::kWrongSignNQ, Sample::kWrongSignPQ, and MSG.

Referenced by AddEvent(), and AddFile().

00213 {
00214   // For now, copy the sample definitions out of the NuFCExperimentFactory
00215   // this is a rough, bad, CC-specific way of doing this, and should be tailored
00216   // to each individual run (preferably inside the run itself).
00217   
00218   // Deal with Tau samples
00219   if (nu.inu == 16 || nu.inu == -16) {
00220     // Ignore taus in the following cases.. from NuE, NC taus
00221     if (nu.inunoosc == 12 || nu.inunoosc==-12 || nu.iaction == 0) return 0;
00222     // It is a tau! store based on the charge...
00223     if (nu.charge == -1) return Sample::kTauNQ;
00224     if (nu.charge == +1) return Sample::kTauPQ;
00225     // Should never reach here
00226     assert(0);
00227   }
00228   
00229   // Deal with non-tau NC samples
00230   if (nu.iaction == 0)
00231   {
00232     if (nu.charge == +1) return Sample::kNCPQ;
00233     if (nu.charge == -1) return Sample::kNCNQ;
00234     // Should never reach here
00235     cout << "Could not deal with charge " << nu.charge << endl;
00236     assert(0);
00237   }
00238   
00239   // Deal with non-tau CC event types
00240   if (nu.iaction == 1)
00241   {
00242     if (nu.inu == 14) {
00243       if (nu.charge == +1) return Sample::kWrongSignPQ;
00244       if (nu.charge == -1) return Sample::kSignalNQ;
00245     } else if (nu.inu == -14) {
00246       if (nu.charge == +1) return Sample::kSignalPQ;
00247       if (nu.charge == -1) return Sample::kWrongSignNQ;
00248     }
00249     
00250     // Ignore NuEs
00251     if (nu.inu == 12 || nu.inu == -12) return 0;
00252     
00253     cout << "Got CC event with inu: " << nu.inu << "; unexpected" << endl;
00254     // Should never reach here
00255     assert(0);
00256   }
00257   
00258   // Should never reach here
00259   MSG("NuFCEventManager",Msg::kError) << "Reached end of classification function without classifying" << endl;
00260   
00261   assert(0);
00262 
00263   return 0;
00264 }

const NuEventVector< NuFCEvent2 > & NuFCEventManager::FDEvents ( const NuFCSample sample  )  const

Returns the event vector for a single sample.

Definition at line 349 of file NuFCEventManager.cxx.

References fFDSamples, and it.

Referenced by NuFCRunInfo::BuildFDSummaries(), NuFCExperimentGenerator::GenerateFDSample(), TestEventManager::testEventAddFD(), and TestEventManager::testReweightSamplesPerBin().

00350 {
00351   map<NuFCSample, NuEventVector<NuFCEvent2> >::const_iterator it;
00352   it = fFDSamples.find(sample);
00353   // It is probably an error if we have no events. Handle ungracefully for now
00354   assert(it != fFDSamples.end());
00355   
00356   return it->second;
00357 }

const NuEventVector<NuFCEvent2>& NuFCEventManager::NDEvents ( void   )  const [inline]

Return the list of ND events.

Definition at line 61 of file NuFCEventManager.h.

References fNDEvents.

Referenced by NuFCRunInfo::BuildNDSummary(), NuFCExperimentGenerator::GenerateND(), and TestEventManager::testEventAddND().

00061 { return fNDEvents; }

void NuFCEventManager::Print ( void   )  const

Print a summary of events held.

Definition at line 293 of file NuFCEventManager.cxx.

References fFDSamples, fNDEvents, it, Msg::kInfo, MSG, and SampleName().

00294 {
00295   MSG("NuFCEventManager",Msg::kInfo) << "Near Detector: " << fNDEvents.size() << " events" << endl;
00296   MSG("NuFCEventManager",Msg::kInfo) << "Far detector samples:" << endl;
00297   std::map<NuFCSample, NuEventVector<NuFCEvent2> >::const_iterator it;
00298   for (it = fFDSamples.begin(); it != fFDSamples.end(); ++it)
00299   {
00300     MSG("NuFCEventManager",Msg::kInfo)
00301       << "  " << setw(14) << SampleName((*it).first)
00302       << ": " << (*it).second.size() << endl;
00303   }
00304   
00305 }

void NuFCEventManager::ReweightSamples ( void   )  [private]

Reweight all of the samples to have a maximum reweight value.

Definition at line 322 of file NuFCEventManager.cxx.

References fFDSamples, it, and NuEventVector< T >::scale().

Referenced by AddFile().

00323 {
00324   // The value to weight the spectrum to be maximum at
00325   const Double_t MaxWeight = 1.0;
00326   
00327   std::map<NuFCSample, NuEventVector<NuFCEvent2> >::iterator it;
00328   for (it = fFDSamples.begin(); it != fFDSamples.end(); ++it)
00329   {
00330     Double_t scale = MaxWeight / (*it).second.maxrw();
00331     (*it).second.scale(scale);
00332   }
00333 }

void NuFCEventManager::ReweightSamplesPerBin ( const NuMatrix1D truebins  ) 

Reweight each of the samples, based on energy bins. This will mean that the rw of the samples will be reweighted based on the maximum sample in a particular bin, rather than overall. You probably should

Definition at line 361 of file NuFCEventManager.cxx.

References fFDSamples, fSamples, Msg::kInfo, NuEventVector< T >::maxrw(), MSG, NuEvent::rw, and NuMatrix1D::Spectrum().

Referenced by TestEventManager::testReweightSamplesPerBin().

00362 {
00363   // Validate that we get a spectrum to use
00364   assert(truebins.Spectrum());
00365   TH1D *binHist = dynamic_cast<TH1D*>(truebins.Spectrum());
00366   assert(binHist);
00367   
00368   // For each sample
00369   vector<NuFCSample>::const_iterator sample;
00370   for (sample = fSamples.begin(); sample != fSamples.end(); ++sample)
00371   {
00372     MSG("NuFCEventManager",Msg::kInfo) << "==== Processing sample " << sample->GetName() << " ====" << endl;
00373     
00374     // Create a pointer storage vector for each bin (+overflows)
00375     vector<vector<NuFCEvent2*> > bins(binHist->GetNbinsX()+2);
00376     
00377     // Fill this vector with pointers to events
00378     for ( NuEventVector<NuFCEvent2>::iterator evt = fFDSamples[*sample].begin(); evt != fFDSamples[*sample].end(); ++evt)
00379     {
00380       Int_t bin = binHist->GetXaxis()->FindFixBin(evt->EnergyMC());
00381       bins[bin].push_back(&(*evt));
00382       
00383     } // end of loop over events
00384     
00385     // Now, for each bin
00386     vector<vector<NuFCEvent2*> >::iterator bin;
00387     for (bin = bins.begin(); bin != bins.end(); ++bin)
00388     {
00389       // Find the maximum weight in this bin
00390       Double_t maxrw = 0.0;
00391       vector<NuFCEvent2*>::const_iterator evt;
00392       for (evt = bin->begin(); evt != bin->end(); ++evt)
00393       {
00394         // Is this the first event?
00395         if (evt == bin->begin()) maxrw = (*evt)->Rw();
00396         // Check if this is larger...
00397         if ((*evt)->Rw() > maxrw) maxrw = (*evt)->Rw();
00398       }
00399       MSG("NuFCEventManager",Msg::kInfo)
00400         << "Found max rw " << maxrw << " over " << bin->size() << " events" << endl;
00401       // Loop over all events again, this time scaling the rw
00402       for (evt = bin->begin(); evt != bin->end(); ++evt)
00403       {
00404         // Decompress, change and then recompress the event
00405         NuEvent nu;
00406         (*evt)->Expand(nu);
00407         nu.rw /= maxrw;
00408         (*evt)->FillFrom(nu);
00409       }
00410     } // loop over bins in this sample
00411     
00412   } // end of loop over samples
00413   
00414 }

TString NuFCEventManager::SampleName ( const NuFCSample sample  )  const

Returns a string name for a sample. This will be superceeded by the NuFCSample object having it's own\ name, but is here for quickly getting the system up and running.

Definition at line 268 of file NuFCEventManager.cxx.

References NuFCSample::GetName().

Referenced by AddFile(), NuFCExperimentGenerator::GenerateFDSample(), and Print().

00269 {
00270   // Int_t sample = s;
00271   // 
00272   // if (sample == 1) return "kSignalPQ";
00273   // if (sample ==-1) return "kSignalNQ";
00274   // if (sample == 2) return "kWrongSignPQ";
00275   // if (sample ==-2) return "kWrongSignNQ";
00276   // if (sample == 3) return "kNCPQ";
00277   // if (sample ==-3) return "kNCNQ";
00278   // if (sample == 4) return "kTauPQ";
00279   // if (sample ==-4) return "kTauNQ";
00280   // if (sample == 5) return "kAppearedPQ";
00281   // if (sample == 6) return "kAppearedNQ";
00282   // 
00283   // if (sample == 0) return "UnknownSample";
00284   // 
00285   // // If not one of these, they may be using a completely different scheme. abort.
00286   // cout << "Unknown Sample: " << sample << endl;
00287   // assert(0);
00288   return s.GetName();
00289 }

const std::vector<NuFCSample>& NuFCEventManager::Samples ( void   )  const [inline]

Return a list of FD samples.

Definition at line 64 of file NuFCEventManager.h.

References fSamples.

Referenced by NuFCRunInfo::BuildFDSummaries(), NuFCRunInfo::BuildSampleWeights(), NuFCExperimentGenerator::GenerateFD(), and TestEventManager::testEventAddFD().

00064 { return fSamples; }

UInt_t NuFCEventManager::Size ( void   )  const

Returns the number of items held by this class.

Definition at line 308 of file NuFCEventManager.cxx.

00309 {
00310   assert(0);
00311 }


Member Data Documentation

A map of far detector samples.

Definition at line 22 of file NuFCEventManager.h.

Referenced by AddEvent(), AddFile(), BuildSampleList(), FDEvents(), Print(), ReweightSamples(), and ReweightSamplesPerBin().

Near detector events are special: Store them separately.

Definition at line 26 of file NuFCEventManager.h.

Referenced by AddEvent(), AddFile(), NDEvents(), and Print().

std::vector<NuFCSample> NuFCEventManager::fSamples [private]

Definition at line 23 of file NuFCEventManager.h.

Referenced by BuildSampleList(), ReweightSamplesPerBin(), and Samples().


The documentation for this class was generated from the following files:

Generated on 22 Nov 2017 for loon by  doxygen 1.6.1