00001
00002
00003
00004
00005
00006
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
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
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 }
00150 }
00151 else{
00152 MSG("NCEventAdder", Msg::kInfo) << "Creating NC::RealDataAdder" << endl;
00153 adder = new NC::RealDataAdder;
00154 }
00155 }
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
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
00264 fSkipData = fSkipMC && tmpi;
00265
00266 assert(ok);
00267 }
00268
00269
00270
00271 void NC::FakeDataAdder::AddEvents(NCExtrapolationModule* mod,
00272 NCEventInfo* eventInfo,
00273 TChain* ,
00274 TChain* nearMC,
00275 TChain* ,
00276 TChain* farMC,
00277 TChain* farMCTau,
00278 TChain* farMCElectron) const
00279 {
00280 if(fSkipMC && fSkipData) return;
00281
00282
00283 AddEventsToExtrapolations(mod, eventInfo, nearMC);
00284
00285
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
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
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
00322 if(!fSkipData) mod->AddEventToExtrapolations(true);
00323 }
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* ,
00356 TChain* nearMC,
00357 TChain* ,
00358 TChain* farMC,
00359 TChain* farMCTau,
00360 TChain* farMCElectron) const
00361 {
00362
00363 AddEventsToExtrapolations(mod, eventInfo, nearMC);
00364
00365
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
00390 int limit = (det == Detector::kNear) ? fRunLimitNearMC : fRunLimitFarMC;
00391
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
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
00409 asFakeData = randomGen.Uniform() > .5;
00410 }
00411 else{
00412
00413 asFakeData = (runLimitHelper.GetRunCounter()%2 == 0);
00414 }
00415
00416 mod->AddEventToExtrapolations(asFakeData);
00417
00418 }
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){
00461 AddEventsToExtrapolations(mod, eventInfo, nearMC);
00462
00463
00464
00465 AddEventsToExtrapolations(mod, eventInfo, nearData);
00466 }
00467
00468 if(!fSkipMC){
00469 AddEventsToExtrapolations(mod, eventInfo, farMCTau);
00470 AddEventsToExtrapolations(mod, eventInfo, farMCElectron);
00471 AddEventsToExtrapolations(mod, eventInfo, farMC);
00472 }
00473 AddEventsToExtrapolations(mod, eventInfo, farData);
00474 }
00475
00476
00477
00478 void NC::RealDataAdder::
00479 AddEventsToExtrapolations(NCExtrapolationModule* mod,
00480 NCEventInfo* eventInfo,
00481 TChain* chain) const
00482 {
00483 if(chain->GetEntries() < 1){
00484 MSG("NC::RealDataAdder", Msg::kError) << "No entries in chain!" << endl;
00485 return;
00486 }
00487
00488 eventInfo->FillFromChain(chain, 0);
00489
00490 using namespace Detector;
00491 using namespace SimFlag;
00492
00493 const Detector_t det = Detector_t(eventInfo->header->detector);
00494 const SimFlag_t sim = SimFlag_t(eventInfo->header->dataType);
00495
00496 ReportOnEventInfo(eventInfo);
00497
00498
00499 int limit = -1;
00500 if(det == kNear && sim == kMC) limit = fRunLimitNearMC;
00501 if(det == kNear && sim == kData) limit = fRunLimitNearData;
00502 if(det == kFar && sim == kMC) limit = fRunLimitFarMC;
00503 if(det == kFar && sim == kData) limit = fRunLimitFarData;
00504 assert(limit >= 0);
00505
00506 const int eventTot = chain->GetEntries();
00507
00508 LoopRunLimitHelper runLimitHelper(limit, eventTot, eventInfo);
00509
00510
00511 for(int i = 0; i < eventTot; ++i){
00512 eventInfo->FillFromChain(chain, i);
00513
00514 if(runLimitHelper.AreDone(i, eventInfo)) break;
00515
00516 mod->AddEventToExtrapolations(false);
00517
00518 }
00519 }
00520
00521
00523
00524
00525
00526 NC::MockDataAdder::MockDataAdder() : fMockDataSubRun(-1)
00527 {
00528 }
00529
00530
00531
00532 NC::MockDataAdder::~MockDataAdder()
00533 {
00534 }
00535
00536
00537
00538 void NC::MockDataAdder::Config(const Registry& r)
00539 {
00540 NC::EventAdderBase::Config(r);
00541
00542 bool ok = true;
00543
00544 const char* tmps;
00545 ok = ok && r.Get("MockDataSet", tmps);
00546 fMockDataSet = tmps;
00547
00548 ok = ok && r.Get("MockDataSubRun", fMockDataSubRun);
00549
00550 assert(ok);
00551 }
00552
00553
00554
00555 void NC::MockDataAdder::AddEvents(NCExtrapolationModule* mod,
00556 NCEventInfo* eventInfo,
00557 TChain* ,
00558 TChain* nearMC,
00559 TChain* farData,
00560 TChain* farMC,
00561 TChain* farMCTau,
00562 TChain* farMCElectron) const
00563 {
00564 AddEventsToExtrapolations(mod, eventInfo, nearMC);
00565
00566 AddEventsToExtrapolations(mod, eventInfo, farMCTau);
00567 AddEventsToExtrapolations(mod, eventInfo, farMCElectron);
00568 AddEventsToExtrapolations(mod, eventInfo, farMC);
00569 AddEventsToExtrapolations(mod, eventInfo, farData);
00570 }
00571
00572
00573
00574 void NC::MockDataAdder::
00575 AddEventsToExtrapolations(NCExtrapolationModule* mod,
00576 NCEventInfo* eventInfo,
00577 TChain* chain) const
00578 {
00579 if(chain->GetEntries() < 1){
00580 MSG("NC::MockDataAdder", Msg::kError) << "No entries in chain!" << endl;
00581 return;
00582 }
00583
00584 eventInfo->FillFromChain(chain, 0);
00585
00586 using namespace Detector;
00587 using namespace SimFlag;
00588
00589 const Detector_t det = Detector_t(eventInfo->header->detector);
00590 const SimFlag_t sim = SimFlag_t(eventInfo->header->dataType);
00591 const NCType::EFileType fileType = NCType::FindFileType(eventInfo->header);
00592
00593 ReportOnEventInfo(eventInfo);
00594
00595
00596 int limit = INT_MAX;
00597 if(det == kNear && sim == kMC) limit = fRunLimitNearMC;
00598 if(det == kFar && sim == kMC) limit = fRunLimitFarMC;
00599 assert(limit >= 0);
00600
00601 const int eventTot = chain->GetEntries();
00602
00603 LoopRunLimitHelper runLimitHelper(limit, eventTot, eventInfo);
00604
00605
00606 for(int i = 0; i < eventTot; ++i){
00607 eventInfo->FillFromChain(chain, i);
00608
00609 const int thisSubRun = eventInfo->header->subRun;
00610
00611
00612 if(fileType == NCType::kMockFile &&
00613 fMockDataSubRun != -1 &&
00614 thisSubRun != fMockDataSubRun) continue;
00615
00616 if(runLimitHelper.AreDone(i, eventInfo)) break;
00617
00618 mod->AddEventToExtrapolations(false);
00619 if(det == kNear) mod->AddEventToExtrapolations(true);
00620
00621 }
00622 }