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

NCEventAdder.cxx

Go to the documentation of this file.
00001 
00002 //$Id: NCEventAdder.cxx,v 1.2 2009/08/10 17:56:47 bckhouse Exp $
00003 //
00004 //NCEventAdder.cxx
00005 //
00006 //C. Backhouse 7/2009
00008 
00009 
00010 #include "NCEventAdder.h"
00011 
00012 #include "NCExtrapolationModule.h"
00013 
00014 #include "../NCUtility.h"
00015 
00016 #include "../NCEventInfo.h"
00017 
00018 #include "AnalysisNtuples/ANtpBeamInfo.h"
00019 #include "AnalysisNtuples/ANtpHeaderInfo.h"
00020 #include "Conventions/SimFlag.h"
00021 #include "MessageService/MsgService.h"
00022 
00023 #include "TRandom3.h"
00024 #include "TStopwatch.h"
00025 
00026 #include <cassert>
00027 #include <climits>
00028 
00029 CVSID("");
00030 
00031 
00032 //-----------------------------------------------------------------------
00033 NC::EventAdderBase::EventAdderBase()
00034   : fRunLimitNearMC(-1),
00035     fRunLimitNearData(-1),
00036     fRunLimitFarMC(-1),
00037     fRunLimitFarData(-1)
00038 {
00039 }
00040 
00041 
00042 //-----------------------------------------------------------------------
00043 NC::EventAdderBase::~EventAdderBase()
00044 {
00045 }
00046 
00047 
00048 //-----------------------------------------------------------------------
00049 void NC::EventAdderBase::Config(const Registry& r)
00050 {
00051   bool ok = true;
00052 
00053   ok = ok && r.Get("RunLimitNearMC",   fRunLimitNearMC);
00054   ok = ok && r.Get("RunLimitNearData", fRunLimitNearData);
00055   ok = ok && r.Get("RunLimitFarMC",    fRunLimitFarMC);
00056   ok = ok && r.Get("RunLimitFarData",  fRunLimitFarData);
00057 
00058   assert(ok);
00059 }
00060 
00061 
00062 //-----------------------------------------------------------------------
00063 void NC::EventAdderBase::ReportOnEventInfo(const NCEventInfo* eventInfo) const
00064 {
00065   using namespace Detector;
00066   using namespace SimFlag;
00067   using namespace BeamType;
00068 
00069   const Detector_t det = Detector_t(eventInfo->header->detector);
00070   const SimFlag_t sim = SimFlag_t(eventInfo->header->dataType);
00071   const BeamType_t beam = BeamType_t(eventInfo->beam->beamType);
00072 
00073   MSG("NC::EventAdderBase", Msg::kInfo)
00074     << "This is "
00075     << eventInfo->header->softVersion << " "
00076     << AsString(det) << " "
00077     << AsString(beam) << " "
00078     << AsString(sim)
00079     << endl;
00080 
00081 
00082   // Hackish check to refuse to run on old-style files
00083   if(beam == 2){
00084     MSG("NC::EventAdderBase", Msg::kFatal)
00085       << "This file has beam.beamType==2, which is L000z200i. "
00086       << "You are probably running on an old file with the Zarko "
00087       << "numbering scheme (where 2 is L010z185i), but if you "
00088       << "really do want L000z200i, remove this message and recompile."
00089       << endl;
00090     abort();
00091   }
00092 }
00093 
00094 
00095 //-----------------------------------------------------------------------
00096 const Registry& NC::EventAdderDefaultConfig()
00097 {
00098   static Registry r;
00099 
00100   r.UnLockValues();
00101 
00102   r.Set("RunLimitNearData", INT_MAX);
00103   r.Set("RunLimitFarData",  INT_MAX);
00104   r.Set("RunLimitNearMC",   INT_MAX);
00105   r.Set("RunLimitFarMC",    INT_MAX);
00106 
00107   r.Set("UseMCAsData",      false);
00108   r.Set("SplitMC",          false);
00109   r.Set("SplitMCSeed",      -1);
00110   r.Set("UseMockData",      false);
00111   r.Set("MockDataSet",      "F21910001");
00112   r.Set("MockDataSubRun",    -1);
00113 
00114   r.LockValues();
00115   return r;
00116 }
00117 
00118 
00119 //-----------------------------------------------------------------------
00120 NC::IEventAdder* NC::EventAdderBuilder(const Registry& r)
00121 {
00122   NC::IEventAdder* adder = 0;
00123 
00124   // These should be bools, but the registry is stupid
00125   int useMCAsData, splitMC, useMockData;
00126 
00127   bool ok = true;
00128   ok = ok && r.Get("UseMCAsData", useMCAsData);
00129   ok = ok && r.Get("SplitMC",     splitMC);
00130   ok = ok && r.Get("UseMockData", useMockData);
00131   assert(ok);
00132 
00133   if(useMockData){
00134     MSG("NCEventAdder", Msg::kInfo)
00135       << "Creating NC::MockDataAdder" << endl;
00136     adder = new NC::MockDataAdder;
00137   }
00138   else{
00139     if(useMCAsData){
00140       if(splitMC){
00141         MSG("NCEventAdder", Msg::kInfo)
00142           << "Creating NC::SplitFakeDataAdder" << endl;
00143         adder = new NC::SplitFakeDataAdder;
00144       }
00145       else{
00146         MSG("NCEventAdder", Msg::kInfo)
00147           << "Creating NC::FakeDataAdder" << endl;
00148         adder = new NC::FakeDataAdder;
00149       } // end if splitMC
00150     }
00151     else{
00152       MSG("NCEventAdder", Msg::kInfo) << "Creating NC::RealDataAdder" << endl;
00153       adder = new NC::RealDataAdder;
00154     } // end if useMCAsData
00155   } // end if useMockData
00156 
00157   assert(adder);
00158   adder->Config(r);
00159   return adder;
00160 }
00161 
00162 
00163 //-----------------------------------------------------------------------
00165 class LoopRunLimitHelper
00166 {
00167 public:
00172   LoopRunLimitHelper(int runLimit, int eventTot, const NCEventInfo* eventInfo)
00173     : fEventTot(eventTot), fRunLimit(runLimit), fRunCounter(0)
00174   {
00175     fPrevSubRun = eventInfo->header->subRun;
00176     fPrevRun = eventInfo->header->run;
00177 
00178     if(fRunLimit != INT_MAX){
00179       MSG("RunLimitHelper", Msg::kInfo)
00180         << fEventTot << " total events "
00181         << "limited to " << fRunLimit  << " (sub)runs" << endl;
00182     }
00183     else{
00184       MSG("RunLimitHelper", Msg::kInfo)
00185         << fEventTot << " total events, no (sub)run limit" << endl;
00186     }
00187   }
00188 
00189   ~LoopRunLimitHelper()
00190   {
00191      NC::Utility::ReportProgress(1, fStopwatch);
00192      MSG("RunLimitHelper", Msg::kInfo) << endl;
00193   }
00194 
00203   bool AreDone(int i, const NCEventInfo* eventInfo)
00204   {
00205     const int thisSubRun = eventInfo->header->subRun;
00206     const int thisRun = eventInfo->header->run;
00207 
00208     if(thisRun != fPrevRun || thisSubRun != fPrevSubRun){
00209       fPrevRun = thisRun;
00210       fPrevSubRun = thisSubRun;
00211       ++fRunCounter;
00212     }
00213 
00214     // Don't do it on the very first few, other people want to print
00215     if(i % 1000 == 10)
00216       NC::Utility::ReportProgress(std::max(float(i)/fEventTot,
00217                                            float(fRunCounter)/fRunLimit),
00218                                   fStopwatch);
00219 
00220     return (fRunCounter > fRunLimit || i >= fEventTot);
00221   }
00222 
00223   int GetRunCounter(){return fRunCounter;}
00224 
00225 protected:
00226   TStopwatch fStopwatch; 
00227 
00228   int fEventTot;   
00229   int fRunLimit;   
00230   int fPrevSubRun; 
00231   int fPrevRun;    
00232   int fRunCounter; 
00233 };
00234 
00235 
00237 
00238 
00239 //-----------------------------------------------------------------------
00240 NC::FakeDataAdder::FakeDataAdder() : fSkipMC(false), fSkipData(false)
00241 {
00242 }
00243 
00244 
00245 //-----------------------------------------------------------------------
00246 NC::FakeDataAdder::~FakeDataAdder()
00247 {
00248 }
00249 
00250 
00251 //-----------------------------------------------------------------------
00252 void NC::FakeDataAdder::Config(const Registry& r)
00253 {
00254   NC::EventAdderBase::Config(r);
00255 
00256   bool ok = true;
00257 
00258   const char* tmps;
00259   ok = ok && r.Get("LoadExtrapolationsFile", tmps);
00260   fSkipMC = TString(tmps) != "";
00261   int tmpi;
00262   ok = ok && r.Get("IncludeDataFromLoadedExtrapolations", tmpi);
00263   // This key only takes effect when we're loading anyway
00264   fSkipData = fSkipMC && tmpi;
00265 
00266   assert(ok);
00267 }
00268 
00269 
00270 //-----------------------------------------------------------------------
00271 void NC::FakeDataAdder::AddEvents(NCExtrapolationModule* mod,
00272                                   NCEventInfo* eventInfo,
00273                                   TChain* /*nearData*/,
00274                                   TChain* nearMC,
00275                                   TChain* /*farData*/,
00276                                   TChain* farMC,
00277                                   TChain* farMCTau,
00278                                   TChain* farMCElectron) const
00279 {
00280   if(fSkipMC && fSkipData) return;
00281 
00282   // Add near MC as both MC and fake data
00283   AddEventsToExtrapolations(mod, eventInfo, nearMC);
00284 
00285   // Add all the far MC and as data as well
00286   AddEventsToExtrapolations(mod, eventInfo, farMCTau);
00287   AddEventsToExtrapolations(mod, eventInfo, farMCElectron);
00288   AddEventsToExtrapolations(mod, eventInfo, farMC);
00289 }
00290 
00291 
00292 //-----------------------------------------------------------------------
00293 void NC::FakeDataAdder::AddEventsToExtrapolations(NCExtrapolationModule* mod,
00294                                                   NCEventInfo* eventInfo,
00295                                                   TChain* chain) const
00296 {
00297   if(chain->GetEntries() < 1){
00298     MSG("NC::FakeDataAdder", Msg::kError) << "No entries in chain!" << endl;
00299     return;
00300   }
00301 
00302   eventInfo->FillFromChain(chain, 0);
00303 
00304   ReportOnEventInfo(eventInfo);
00305 
00306   // The file limit
00307   Detector::Detector_t det = Detector::Detector_t(eventInfo->header->detector);
00308   const int limit = (det == Detector::kNear) ? fRunLimitNearMC : fRunLimitFarMC;
00309 
00310   const int eventTot = chain->GetEntries();
00311 
00312   LoopRunLimitHelper runLimitHelper(limit, eventTot, eventInfo);
00313 
00314   //loop over the entries in the tree
00315   for(int i = 0; i < eventTot; ++i){
00316     eventInfo->FillFromChain(chain, i);
00317 
00318     if(runLimitHelper.AreDone(i, eventInfo)) break;
00319 
00320     if(!fSkipMC) mod->AddEventToExtrapolations(false);
00321     //    chain->GetEntry(i);
00322     if(!fSkipData) mod->AddEventToExtrapolations(true);
00323   }//end loop over data chain
00324 }
00325 
00326 
00328 
00329 
00330 //-----------------------------------------------------------------------
00331 NC::SplitFakeDataAdder::SplitFakeDataAdder() : fSplitMCSeed(-1)
00332 {
00333 }
00334 
00335 
00336 //-----------------------------------------------------------------------
00337 NC::SplitFakeDataAdder::~SplitFakeDataAdder()
00338 {
00339 }
00340 
00341 
00342 //-----------------------------------------------------------------------
00343 void NC::SplitFakeDataAdder::Config(const Registry& r)
00344 {
00345   NC::EventAdderBase::Config(r);
00346 
00347   const bool ok = r.Get("SplitMCSeed", fSplitMCSeed);
00348   assert(ok);
00349 }
00350 
00351 
00352 //-----------------------------------------------------------------------
00353 void NC::SplitFakeDataAdder::AddEvents(NCExtrapolationModule* mod,
00354                                        NCEventInfo* eventInfo,
00355                                        TChain* /*nearData*/,
00356                                        TChain* nearMC,
00357                                        TChain* /*farData*/,
00358                                        TChain* farMC,
00359                                        TChain* farMCTau,
00360                                        TChain* farMCElectron) const
00361 {
00362   // Add near MC as both MC and fake data
00363   AddEventsToExtrapolations(mod, eventInfo, nearMC);
00364 
00365   // Add all the far MC and as data as well
00366   AddEventsToExtrapolations(mod, eventInfo, farMCTau);
00367   AddEventsToExtrapolations(mod, eventInfo, farMCElectron);
00368   AddEventsToExtrapolations(mod, eventInfo, farMC);
00369 }
00370 
00371 
00372 //-----------------------------------------------------------------------
00373 void NC::SplitFakeDataAdder::
00374 AddEventsToExtrapolations(NCExtrapolationModule* mod,
00375                           NCEventInfo* eventInfo,
00376                           TChain* chain) const
00377 {
00378   if(chain->GetEntries() < 1){
00379     MSG("NC::SplitFakeDataAdder", Msg::kError) << "No entries in chain!" << endl;
00380     return;
00381   }
00382 
00383   eventInfo->FillFromChain(chain, 0);
00384 
00385   Detector::Detector_t det = Detector::Detector_t(eventInfo->header->detector);
00386 
00387   ReportOnEventInfo(eventInfo);
00388 
00389   // The file limit
00390   int limit = (det == Detector::kNear) ? fRunLimitNearMC : fRunLimitFarMC;
00391   // Need an even number of runs so we can split them equally
00392   if(TMath::Odd(limit)) --limit;
00393 
00394   const int eventTot = chain->GetEntries();
00395 
00396   LoopRunLimitHelper runLimitHelper(limit, eventTot, eventInfo);
00397 
00398   TRandom3 randomGen(fSplitMCSeed);
00399 
00400   //loop over the entries in the tree
00401   for(int i = 0; i < eventTot; ++i){
00402     eventInfo->FillFromChain(chain, i);
00403 
00404     if(runLimitHelper.AreDone(i, eventInfo)) break;
00405 
00406     bool asFakeData;
00407     if(fSplitMCSeed >= 0){
00408       // Put each event randomly into one spectrum or another
00409       asFakeData = randomGen.Uniform() > .5;
00410     }
00411     else{
00412       // Put alternate runs into data or MC
00413       asFakeData = (runLimitHelper.GetRunCounter()%2 == 0);
00414     }
00415 
00416     mod->AddEventToExtrapolations(asFakeData);
00417 
00418   }//end loop over data chain
00419 }
00420 
00421 
00423 
00424 
00425 //-----------------------------------------------------------------------
00426 NC::RealDataAdder::RealDataAdder() : fSkipMC(false)
00427 {
00428 }
00429 
00430 
00431 //-----------------------------------------------------------------------
00432 NC::RealDataAdder::~RealDataAdder()
00433 {
00434 }
00435 
00436 
00437 //-----------------------------------------------------------------------
00438 void NC::RealDataAdder::Config(const Registry& r)
00439 {
00440   NC::EventAdderBase::Config(r);
00441 
00442   const char* tmps;
00443   const bool ok = r.Get("LoadExtrapolationsFile", tmps);
00444   fSkipMC = TString(tmps) != "";
00445 
00446   assert(ok);
00447 }
00448 
00449 
00450 //-----------------------------------------------------------------------
00451 void NC::RealDataAdder::AddEvents(NCExtrapolationModule* mod,
00452                                   NCEventInfo* eventInfo,
00453                                   TChain* nearData,
00454                                   TChain* nearMC,
00455                                   TChain* farData,
00456                                   TChain* farMC,
00457                                   TChain* farMCTau,
00458                                   TChain* farMCElectron) const
00459 {
00460   if(!fSkipMC) AddEventsToExtrapolations(mod, eventInfo, nearMC);
00461   AddEventsToExtrapolations(mod, eventInfo, nearData);
00462 
00463   if(!fSkipMC){
00464     AddEventsToExtrapolations(mod, eventInfo, farMCTau);
00465     AddEventsToExtrapolations(mod, eventInfo, farMCElectron);
00466     AddEventsToExtrapolations(mod, eventInfo, farMC);
00467   }
00468   AddEventsToExtrapolations(mod, eventInfo, farData);
00469 }
00470 
00471 
00472 //-----------------------------------------------------------------------
00473 void NC::RealDataAdder::
00474 AddEventsToExtrapolations(NCExtrapolationModule* mod,
00475                           NCEventInfo* eventInfo,
00476                           TChain* chain) const
00477 {
00478   if(chain->GetEntries() < 1){
00479     MSG("NC::RealDataAdder", Msg::kError) << "No entries in chain!" << endl;
00480     return;
00481   }
00482 
00483   eventInfo->FillFromChain(chain, 0);
00484 
00485   using namespace Detector;
00486   using namespace SimFlag;
00487 
00488   const Detector_t det = Detector_t(eventInfo->header->detector);
00489   const SimFlag_t sim = SimFlag_t(eventInfo->header->dataType);
00490 
00491   ReportOnEventInfo(eventInfo);
00492 
00493   // The file limit
00494   int limit = -1;
00495   if(det == kNear && sim == kMC)   limit = fRunLimitNearMC;
00496   if(det == kNear && sim == kData) limit = fRunLimitNearData;
00497   if(det == kFar  && sim == kMC)   limit = fRunLimitFarMC;
00498   if(det == kFar  && sim == kData) limit = fRunLimitFarData;
00499   assert(limit >= 0);
00500 
00501   const int eventTot = chain->GetEntries();
00502 
00503   LoopRunLimitHelper runLimitHelper(limit, eventTot, eventInfo);
00504 
00505   //loop over the entries in the tree
00506   for(int i = 0; i < eventTot; ++i){
00507     eventInfo->FillFromChain(chain, i);
00508 
00509     if(runLimitHelper.AreDone(i, eventInfo)) break;
00510 
00511     mod->AddEventToExtrapolations(false);
00512 
00513   }//end loop over data chain
00514 }
00515 
00516 
00518 
00519 
00520 //-----------------------------------------------------------------------
00521 NC::MockDataAdder::MockDataAdder() : fMockDataSubRun(-1)
00522 {
00523 }
00524 
00525 
00526 //-----------------------------------------------------------------------
00527 NC::MockDataAdder::~MockDataAdder()
00528 {
00529 }
00530 
00531 
00532 //-----------------------------------------------------------------------
00533 void NC::MockDataAdder::Config(const Registry& r)
00534 {
00535   NC::EventAdderBase::Config(r);
00536 
00537   bool ok = true;
00538 
00539   const char* tmps;
00540   ok = ok && r.Get("MockDataSet", tmps);
00541   fMockDataSet = tmps;
00542 
00543   ok = ok && r.Get("MockDataSubRun", fMockDataSubRun);
00544 
00545   assert(ok);
00546 }
00547 
00548 
00549 //-----------------------------------------------------------------------
00550 void NC::MockDataAdder::AddEvents(NCExtrapolationModule* mod,
00551                                   NCEventInfo* eventInfo,
00552                                   TChain* /*nearData*/,
00553                                   TChain* nearMC,
00554                                   TChain* farData,
00555                                   TChain* farMC,
00556                                   TChain* farMCTau,
00557                                   TChain* farMCElectron) const
00558 {
00559   AddEventsToExtrapolations(mod, eventInfo, nearMC);
00560 
00561   AddEventsToExtrapolations(mod, eventInfo, farMCTau);
00562   AddEventsToExtrapolations(mod, eventInfo, farMCElectron);
00563   AddEventsToExtrapolations(mod, eventInfo, farMC);
00564   AddEventsToExtrapolations(mod, eventInfo, farData);
00565 }
00566 
00567 
00568 //-----------------------------------------------------------------------
00569 void NC::MockDataAdder::
00570 AddEventsToExtrapolations(NCExtrapolationModule* mod,
00571                           NCEventInfo* eventInfo,
00572                           TChain* chain) const
00573 {
00574   if(chain->GetEntries() < 1){
00575     MSG("NC::MockDataAdder", Msg::kError) << "No entries in chain!" << endl;
00576     return;
00577   }
00578 
00579   eventInfo->FillFromChain(chain, 0);
00580 
00581   using namespace Detector;
00582   using namespace SimFlag;
00583 
00584   const Detector_t det = Detector_t(eventInfo->header->detector);
00585   const SimFlag_t sim = SimFlag_t(eventInfo->header->dataType);
00586   const NCType::EFileType fileType = NCType::FindFileType(eventInfo->header);
00587 
00588   ReportOnEventInfo(eventInfo);
00589 
00590   // The file limit
00591   int limit = INT_MAX;
00592   if(det == kNear && sim == kMC) limit = fRunLimitNearMC;
00593   if(det == kFar  && sim == kMC) limit = fRunLimitFarMC;
00594   assert(limit >= 0);
00595 
00596   const int eventTot = chain->GetEntries();
00597 
00598   LoopRunLimitHelper runLimitHelper(limit, eventTot, eventInfo);
00599 
00600   //loop over the entries in the tree
00601   for(int i = 0; i < eventTot; ++i){
00602     eventInfo->FillFromChain(chain, i);
00603 
00604     const int thisSubRun = eventInfo->header->subRun;
00605 
00606     // For the case of just one mock data subrun, only use the subrun requested
00607     if(fileType == NCType::kMockFile &&
00608        fMockDataSubRun != -1 &&
00609        thisSubRun != fMockDataSubRun) continue;
00610 
00611     if(runLimitHelper.AreDone(i, eventInfo)) break;
00612 
00613     mod->AddEventToExtrapolations(false);
00614     if(det == kNear) mod->AddEventToExtrapolations(true);
00615 
00616   }//end loop over data chain
00617 }

Generated on Mon Nov 23 05:27:28 2009 for loon by  doxygen 1.3.9.1