1 |
|
// $Id$ |
2 |
|
|
3 |
+ |
#include <boost/property_tree/ptree.hpp> |
4 |
+ |
#include <boost/property_tree/json_parser.hpp> |
5 |
+ |
#include <boost/lexical_cast.hpp> |
6 |
|
#include "MitAna/PhysicsMod/interface/RunLumiSelectionMod.h" |
7 |
|
#include <TFile.h> |
8 |
|
#include <TTree.h> |
16 |
|
RunLumiSelectionMod::RunLumiSelectionMod(const char *name, const char *title) : |
17 |
|
BaseMod(name,title), |
18 |
|
fAbort(kTRUE), |
19 |
< |
fDefaultAccept(kFALSE), |
19 |
> |
fAcceptMC(kFALSE), |
20 |
|
fNEvents(0), |
21 |
|
fNAcceped(0), |
22 |
|
fNFailed(0), |
23 |
< |
fAcceptCurrentRun(kFALSE) |
23 |
> |
fAcceptCurrentRunLumi(kFALSE) |
24 |
|
{ |
25 |
|
// Constructor. |
26 |
|
} |
35 |
|
//-------------------------------------------------------------------------------------------------- |
36 |
|
void RunLumiSelectionMod::BeginRun() |
37 |
|
{ |
35 |
– |
//Decide if this run should be accepted and cache decision. |
36 |
– |
UInt_t run = GetRunInfo()->RunNum(); |
37 |
– |
Bool_t accepted = kFALSE; |
38 |
– |
Bool_t excluded = kFALSE; |
39 |
– |
|
40 |
– |
//check if run is explicitly accepted |
41 |
– |
for (UIntBounds::const_iterator it = fAcceptedRuns.begin(); it!=fAcceptedRuns.end(); ++it) { |
42 |
– |
if (run>=it->first && run<=it->second) { |
43 |
– |
accepted = kTRUE; |
44 |
– |
break; |
45 |
– |
} |
46 |
– |
} |
47 |
– |
|
48 |
– |
//check if run is explicitly excluded |
49 |
– |
for (UIntBounds::const_iterator it = fExcludedRuns.begin(); it!=fExcludedRuns.end(); ++it) { |
50 |
– |
if (run>=it->first && run<=it->second) { |
51 |
– |
excluded = kTRUE; |
52 |
– |
break; |
53 |
– |
} |
54 |
– |
} |
55 |
– |
|
56 |
– |
//construct final decision |
57 |
– |
fAcceptCurrentRun = (fDefaultAccept || accepted) && !excluded; |
38 |
|
|
39 |
|
} |
40 |
|
|
45 |
|
|
46 |
|
++fNEvents; |
47 |
|
|
48 |
+ |
RunLumiPairType runLumi(GetEventHeader()->RunNum(),GetEventHeader()->LumiSec()); |
49 |
+ |
|
50 |
+ |
//check decision only if lumi section has changed |
51 |
+ |
if (runLumi != fCurrentRunLumi) { |
52 |
+ |
fAcceptCurrentRunLumi = kFALSE; |
53 |
+ |
//check for MC default accept |
54 |
+ |
if (fAcceptMC && GetEventHeader()->IsMC()) { |
55 |
+ |
fAcceptCurrentRunLumi = kTRUE; |
56 |
+ |
} |
57 |
+ |
else { |
58 |
+ |
//check if run is included |
59 |
+ |
MapType::const_iterator it = fAcceptedRunsLumis.find(runLumi.first); |
60 |
+ |
if (it!=fAcceptedRunsLumis.end()) { |
61 |
+ |
//check lumis |
62 |
+ |
const MapType::mapped_type &lumiPairList = it->second; |
63 |
+ |
for (MapType::mapped_type::const_iterator jt = lumiPairList.begin(); jt<lumiPairList.end(); ++jt) { |
64 |
+ |
if (runLumi.second >= jt->first && runLumi.second <= jt->second) { |
65 |
+ |
//found lumi in accepted range |
66 |
+ |
fAcceptCurrentRunLumi = kTRUE; |
67 |
+ |
} |
68 |
+ |
} |
69 |
+ |
} |
70 |
+ |
else { |
71 |
+ |
//run not in JSON file, reject it |
72 |
+ |
fAcceptCurrentRunLumi = kFALSE; |
73 |
+ |
} |
74 |
+ |
} |
75 |
+ |
fCurrentRunLumi = runLumi; |
76 |
+ |
if (0) { |
77 |
+ |
printf("Run %u, Lumi %u, accepted = %i\n",runLumi.first,runLumi.second,fAcceptCurrentRunLumi); |
78 |
+ |
} |
79 |
+ |
} |
80 |
+ |
|
81 |
|
// take action if failed |
82 |
< |
if (!fAcceptCurrentRun) { |
82 |
> |
if (!fAcceptCurrentRunLumi) { |
83 |
|
++fNFailed; |
84 |
|
OnFailed(); |
85 |
|
if (fAbort) { |
107 |
|
|
108 |
|
SaveNEventsProcessed(); |
109 |
|
} |
110 |
+ |
|
111 |
+ |
//-------------------------------------------------------------------------------------------------- |
112 |
+ |
void RunLumiSelectionMod::AddJSONFile(const std::string &filepath) |
113 |
+ |
{ |
114 |
+ |
|
115 |
+ |
//read json file into boost property tree |
116 |
+ |
boost::property_tree::ptree jsonTree; |
117 |
+ |
boost::property_tree::read_json(filepath,jsonTree); |
118 |
+ |
|
119 |
+ |
//loop through boost property tree and fill the MapType structure with the list of good lumi |
120 |
+ |
//ranges for each run |
121 |
+ |
for (boost::property_tree::ptree::const_iterator it = jsonTree.begin(); it!=jsonTree.end(); ++it) { |
122 |
+ |
UInt_t runNumber = boost::lexical_cast<UInt_t>(it->first); |
123 |
+ |
MapType::mapped_type &lumiPairList = fAcceptedRunsLumis[runNumber]; |
124 |
+ |
boost::property_tree::ptree lumiPairListTree = it->second; |
125 |
+ |
for (boost::property_tree::ptree::const_iterator jt = lumiPairListTree.begin(); jt!=lumiPairListTree.end(); ++jt) { |
126 |
+ |
boost::property_tree::ptree lumiPairTree = jt->second; |
127 |
+ |
if (lumiPairTree.size()==2) { |
128 |
+ |
UInt_t firstLumi = boost::lexical_cast<UInt_t>(lumiPairTree.begin()->second.data()); |
129 |
+ |
UInt_t lastLumi = boost::lexical_cast<UInt_t>((++lumiPairTree.begin())->second.data()); |
130 |
+ |
lumiPairList.push_back(std::pair<UInt_t,UInt_t>(firstLumi,lastLumi)); |
131 |
+ |
} |
132 |
+ |
} |
133 |
+ |
} |
134 |
+ |
|
135 |
+ |
//dump run and lumi ranges from MapType structure to verify correct json parsing |
136 |
+ |
if (0) { |
137 |
+ |
printf("Iterating over parsed JSON:\n"); |
138 |
+ |
for (MapType::const_iterator it = fAcceptedRunsLumis.begin(); it != fAcceptedRunsLumis.end(); ++it) { |
139 |
+ |
printf(" Run %u:\n",it->first); |
140 |
+ |
for (MapType::mapped_type::const_iterator jt = it->second.begin(); jt < it->second.end(); ++jt) { |
141 |
+ |
printf(" Lumis %u - %u\n",jt->first,jt->second); |
142 |
+ |
} |
143 |
+ |
} |
144 |
+ |
|
145 |
+ |
} |
146 |
+ |
|
147 |
+ |
} |