1 |
// $Id: RunLumiRangeMap.cc,v 1.1 2010/05/29 18:10:14 bendavid Exp $
|
2 |
|
3 |
#include "MitAna/DataCont/interface/RunLumiRangeMap.h"
|
4 |
#include <boost/property_tree/ptree.hpp>
|
5 |
#include <boost/property_tree/json_parser.hpp>
|
6 |
#include <boost/lexical_cast.hpp>
|
7 |
#include "MitCommon/JSONSpirit/src/json_spirit.h"
|
8 |
#include <TClass.h>
|
9 |
|
10 |
ClassImp(mithep::RunLumiRangeMap)
|
11 |
|
12 |
//--------------------------------------------------------------------------------------------------
|
13 |
Bool_t mithep::RunLumiRangeMap::HasRunLumi(const RunLumiPairType &runLumi) const
|
14 |
{
|
15 |
// Check if a given run,lumi pair is included in the mapped lumi ranges
|
16 |
|
17 |
//check if run is included in the map
|
18 |
MapType::const_iterator it = fMap.find(runLumi.first);
|
19 |
if (it!=fMap.end()) {
|
20 |
//check lumis
|
21 |
const MapType::mapped_type &lumiPairList = it->second;
|
22 |
for (MapType::mapped_type::const_iterator jt = lumiPairList.begin(); jt<lumiPairList.end(); ++jt) {
|
23 |
if (runLumi.second >= jt->first && runLumi.second <= jt->second) {
|
24 |
//found lumi in accepted range
|
25 |
return kTRUE;
|
26 |
}
|
27 |
}
|
28 |
}
|
29 |
|
30 |
return kFALSE;
|
31 |
|
32 |
}
|
33 |
|
34 |
//--------------------------------------------------------------------------------------------------
|
35 |
void mithep::RunLumiRangeMap::AddJSONFile(const std::string &filepath)
|
36 |
{
|
37 |
|
38 |
//read json file into boost property tree
|
39 |
boost::property_tree::ptree jsonTree;
|
40 |
boost::property_tree::read_json(filepath,jsonTree);
|
41 |
|
42 |
//loop through boost property tree and fill the MapType structure with the list of good lumi
|
43 |
//ranges for each run
|
44 |
for (boost::property_tree::ptree::const_iterator it = jsonTree.begin(); it!=jsonTree.end(); ++it) {
|
45 |
UInt_t runNumber = boost::lexical_cast<UInt_t>(it->first);
|
46 |
MapType::mapped_type &lumiPairList = fMap[runNumber];
|
47 |
boost::property_tree::ptree lumiPairListTree = it->second;
|
48 |
for (boost::property_tree::ptree::const_iterator jt = lumiPairListTree.begin(); jt!=lumiPairListTree.end(); ++jt) {
|
49 |
boost::property_tree::ptree lumiPairTree = jt->second;
|
50 |
if (lumiPairTree.size()==2) {
|
51 |
UInt_t firstLumi = boost::lexical_cast<UInt_t>(lumiPairTree.begin()->second.data());
|
52 |
UInt_t lastLumi = boost::lexical_cast<UInt_t>((++lumiPairTree.begin())->second.data());
|
53 |
lumiPairList.push_back(std::pair<UInt_t,UInt_t>(firstLumi,lastLumi));
|
54 |
}
|
55 |
}
|
56 |
}
|
57 |
|
58 |
//dump run and lumi ranges from MapType structure to verify correct json parsing
|
59 |
if (0) {
|
60 |
printf("Iterating over parsed JSON:\n");
|
61 |
for (MapType::const_iterator it = fMap.begin(); it != fMap.end(); ++it) {
|
62 |
printf(" Run %u:\n",it->first);
|
63 |
for (MapType::mapped_type::const_iterator jt = it->second.begin(); jt < it->second.end(); ++jt) {
|
64 |
printf(" Lumis %u - %u\n",jt->first,jt->second);
|
65 |
}
|
66 |
}
|
67 |
|
68 |
}
|
69 |
|
70 |
}
|
71 |
|
72 |
//--------------------------------------------------------------------------------------------------
|
73 |
void mithep::RunLumiRangeMap::DumpJSONFile(const std::string &filepath)
|
74 |
{
|
75 |
|
76 |
json_spirit::Object jsonTree;
|
77 |
|
78 |
for (MapType::const_iterator it = fMap.begin(); it!=fMap.end(); ++it) {
|
79 |
UInt_t runnum = it->first;
|
80 |
json_spirit::Array lumiPairListArray;
|
81 |
const MapType::mapped_type &lumiPairList = it->second;
|
82 |
for (MapType::mapped_type::const_iterator jt = lumiPairList.begin(); jt<lumiPairList.end(); ++jt) {
|
83 |
json_spirit::Array lumiPairArray;
|
84 |
lumiPairArray.push_back(int(jt->first));
|
85 |
lumiPairArray.push_back(int(jt->second));
|
86 |
|
87 |
lumiPairListArray.push_back(lumiPairArray);
|
88 |
}
|
89 |
json_spirit::Pair runPair(boost::lexical_cast<std::string>(runnum), lumiPairListArray);
|
90 |
jsonTree.push_back(runPair);
|
91 |
}
|
92 |
|
93 |
ofstream os(filepath.c_str());
|
94 |
json_spirit::write(jsonTree,os);
|
95 |
|
96 |
}
|
97 |
|
98 |
//--------------------------------------------------------------------------------------------------
|
99 |
void mithep::RunLumiRangeMap::FillRunLumiSet(const RunLumiSet &rlSet)
|
100 |
{
|
101 |
fMap.clear();
|
102 |
const RunLumiSet::SetType &theset = rlSet.runLumiSet();
|
103 |
|
104 |
UInt_t firstlumi = 0;
|
105 |
for (RunLumiSet::SetType::const_iterator it = theset.begin(); it!=theset.end(); ++it) {
|
106 |
|
107 |
if (firstlumi==0) firstlumi = it->second;
|
108 |
MapType::mapped_type &lumiPairList = fMap[it->first];
|
109 |
|
110 |
RunLumiSet::SetType::const_iterator itnext = it;
|
111 |
++itnext;
|
112 |
|
113 |
if ( itnext==theset.end() || itnext->first!=it->first || itnext->second!=(it->second+1) ) {
|
114 |
lumiPairList.push_back(std::pair<UInt_t,UInt_t>(firstlumi,it->second));
|
115 |
firstlumi = 0;
|
116 |
}
|
117 |
|
118 |
}
|
119 |
} |