CMS 3D CMS Logo

LumiCalculator.cc
Go to the documentation of this file.
1 #ifndef RecoLuminosity_LumiProducer_LumiCalculator_h
2 #define RecoLuminosity_LumiProducer_LumiCalculator_h
14 #include <boost/regex.hpp>
15 #include <iostream>
16 #include <map>
21  unsigned int prescale;
22 };
23 struct l1PerBitInfo {
25  unsigned int prescale;
26 };
27 struct MyPerLumiInfo {
28  unsigned int lsnum;
29  float livefraction;
30  float intglumi;
31  unsigned long long deadcount;
32 };
33 
34 class LumiCalculator : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
35 public:
36  explicit LumiCalculator(edm::ParameterSet const& pset);
37  ~LumiCalculator() override;
38 
39 private:
40  void beginJob() override;
41  void beginRun(const edm::Run& run, const edm::EventSetup& c) override;
42  void analyze(edm::Event const& e, edm::EventSetup const& c) override;
43  void beginLuminosityBlock(edm::LuminosityBlock const& lumiBlock, edm::EventSetup const& c) override {}
44  void endLuminosityBlock(edm::LuminosityBlock const& lumiBlock, edm::EventSetup const& c) override;
45  void endRun(edm::Run const&, edm::EventSetup const&) override;
46  void endJob() override;
47  std::vector<std::string> splitpathstr(const std::string& strValue, const std::string separator);
49  std::multimap<std::string, std::string> trgpathMmap_; //key:hltpath,value:l1bit
50  std::map<std::string, hltPerPathInfo> hltmap_;
51  std::map<std::string, l1PerBitInfo> l1map_; //
52  std::vector<MyPerLumiInfo> perrunlumiinfo_;
53 
54 private:
57  unsigned int currentlumi_;
58 }; //end class
59 
60 // -----------------------------------------------------------------
61 
62 LumiCalculator::LumiCalculator(edm::ParameterSet const& pset) : log_(new edm::LogInfo("LumiReport")), currentlumi_(0) {
63  showTrgInfo_ = pset.getUntrackedParameter<bool>("showTriggerInfo", false);
64  consumes<LumiSummary, edm::InLumi>(edm::InputTag("lumiProducer", ""));
65  consumes<LumiSummaryRunHeader, edm::InRun>(edm::InputTag("lumiProducer", ""));
66 }
67 
68 // -----------------------------------------------------------------
69 
71  delete log_;
72  log_ = nullptr;
73 }
74 
75 // -----------------------------------------------------------------
76 
78 
79 // -----------------------------------------------------------------
80 
82 
83 // -----------------------------------------------------------------
84 
86  //std::cout<<"I'm in run number "<<run.run()<<std::endl;
87  //if(!hltConfig_.init("HLT")){
88  // throw cms::Exception("HLT process cannot be initialized");
89  //}
90  bool changed(true);
91  const std::string processname("HLT");
92  if (!hltConfig_.init(run, c, processname, changed)) {
93  throw cms::Exception("HLT process cannot be initialized");
94  }
95  perrunlumiinfo_.clear();
96  trgpathMmap_.clear();
97  hltmap_.clear();
98  l1map_.clear();
99  //hltConfig_.dump("processName");
100  //hltConfig_.dump("TableName");
101  //hltConfig_.dump("Triggers");
102  //hltConfig_.dump("Modules");
103  if (showTrgInfo_) {
104  *log_ << "======Trigger Configuration Overview======\n";
105  *log_ << "Run " << run.run() << " Trigger Table : " << hltConfig_.tableName() << "\n";
106  }
107  unsigned int totaltrg = hltConfig_.size();
108  for (unsigned int t = 0; t < totaltrg; ++t) {
110  std::vector<std::string> numpathmodules = hltConfig_.moduleLabels(hltname);
111  if (showTrgInfo_) {
112  *log_ << t << " HLT path\t" << hltname << "\n";
113  }
115  hlt.prescale = 1;
116  hltmap_.insert(std::make_pair(hltname, hlt));
117  std::vector<std::string>::iterator hltpathBeg = numpathmodules.begin();
118  std::vector<std::string>::iterator hltpathEnd = numpathmodules.end();
119  unsigned int mycounter = 0;
120  for (std::vector<std::string>::iterator numpathmodule = hltpathBeg; numpathmodule != hltpathEnd; ++numpathmodule) {
121  if (hltConfig_.moduleType(*numpathmodule) != "HLTLevel1GTSeed") {
122  continue;
123  }
124  ++mycounter;
125 
126  edm::ParameterSet l1GTPSet = hltConfig_.modulePSet(*numpathmodule);
127  std::string l1pathname = l1GTPSet.getParameter<std::string>("L1SeedsLogicalExpression");
128  //*log_<<"numpathmodule "<< *numpathmodule <<" : l1pathname "<<l1pathname<<"\n";
129  if (mycounter > 1) {
130  if (showTrgInfo_) {
131  *log_ << "\tskip and erase previous seeds : multiple L1SeedsLogicalExpressions per hlt path\n";
132  }
133  //erase all previously calculated seeds for this path
134  trgpathMmap_.erase(hltname);
135  continue;
136  }
137  if (l1pathname.find('(') != std::string::npos) {
138  if (showTrgInfo_) {
139  *log_ << " L1SeedsLogicalExpression(Complex)\t" << l1pathname << "\n";
140  *log_ << "\tskip:contain complex logic\n";
141  }
142  continue;
143  } else if (l1pathname.find("OR") != std::string::npos) {
144  if (showTrgInfo_) {
145  *log_ << " L1SeedsLogicalExpression(ORed)\t" << l1pathname << "\n";
146  }
147  std::vector<std::string> seeds = splitpathstr(l1pathname, " OR ");
148  if (seeds.size() > 2) {
149  if (showTrgInfo_) {
150  *log_ << "\tskip:contain >1 OR\n";
151  }
152  continue;
153  } else {
154  for (std::vector<std::string>::iterator i = seeds.begin(); i != seeds.end(); ++i) {
155  if (!i->empty() && showTrgInfo_)
156  *log_ << "\t\tseed: " << *i << "\n";
157  if (i == seeds.begin()) { //for now we take the first one from OR
158  trgpathMmap_.insert(std::make_pair(hltname, *i));
159  }
160  }
161  }
162  } else if (l1pathname.find("AND") != std::string::npos) {
163  if (showTrgInfo_) {
164  *log_ << " L1SeedsLogicalExpression(ANDed)\t" << l1pathname << "\n";
165  }
166  std::vector<std::string> seeds = splitpathstr(l1pathname, " AND ");
167  if (seeds.size() > 2) {
168  if (showTrgInfo_) {
169  *log_ << "\tskip:contain >1 AND\n";
170  }
171  continue;
172  } else {
173  for (std::vector<std::string>::iterator i = seeds.begin(); i != seeds.end(); ++i) {
174  if (!i->empty() && showTrgInfo_)
175  *log_ << "\t\tseed: " << *i << "\n";
176  if (i == seeds.begin()) { //for now we take the first one
177  trgpathMmap_.insert(std::make_pair(hltname, *i));
178  }
179  }
180  }
181  } else {
182  if (showTrgInfo_) {
183  *log_ << " L1SeedsLogicalExpression(ONE)\t" << l1pathname << "\n";
184  }
185  if (splitpathstr(l1pathname, " NOT ").size() > 1) {
186  if (showTrgInfo_) {
187  *log_ << "\tskip:contain NOT\n";
188  }
189  continue;
190  }
191  trgpathMmap_.insert(std::make_pair(hltname, l1pathname));
192  }
193  }
194  }
195  if (showTrgInfo_) {
196  *log_ << "================\n";
197  }
198 }
199 
200 // -----------------------------------------------------------------
205  //std::cout<<"I'm in lumi block "<<lumiBlock.id()<<std::endl;
206 
208  lumiBlock.getByLabel("lumiProducer", lumiSummary);
209 
210  edm::Handle<LumiSummaryRunHeader> lumiSummaryRH;
211  lumiBlock.getRun().getByLabel("lumiProducer", lumiSummaryRH);
212 
214  l.lsnum = lumiBlock.id().luminosityBlock();
215 
216  //
217  //collect lumi.
218  //
219  l.deadcount = lumiSummary->deadcount();
220  l.intglumi = lumiSummary->avgInsDelLumi() * 93.244;
221  l.livefraction = lumiSummary->liveFrac();
222 
223  *log_ << "====== Lumi Section " << lumiBlock.id().luminosityBlock() << " ======\n";
224  *log_ << "\t Luminosity " << l.intglumi << "\n";
225  *log_ << "\t Dead count " << l.deadcount << "\n";
226  *log_ << "\t Deadtime corrected Luminosity " << l.intglumi * l.livefraction << "\n";
227 
228  //
229  //print correlated hlt-l1 info, only if you ask
230  //
231  if (showTrgInfo_) {
232  std::map<std::string, hltPerPathInfo>::iterator hltit;
233  std::map<std::string, hltPerPathInfo>::iterator hltitBeg = hltmap_.begin();
234  std::map<std::string, hltPerPathInfo>::iterator hltitEnd = hltmap_.end();
235 
236  typedef std::pair<std::multimap<std::string, std::string>::iterator,
237  std::multimap<std::string, std::string>::iterator>
238  TRGMAPIT;
239  unsigned int c = 0;
240  for (hltit = hltitBeg; hltit != hltitEnd; ++hltit) {
241  std::string hltname = hltit->first;
242  *log_ << c << " HLT path " << hltname << " , prescale : " << hltit->second.prescale << "\n";
243  TRGMAPIT ppp;
244  ppp = trgpathMmap_.equal_range(hltname);
245  if (ppp.first == ppp.second) {
246  *log_ << " no L1\n";
247  }
248  for (std::multimap<std::string, std::string>::iterator mit = ppp.first; mit != ppp.second; ++mit) {
249  std::string l1name = mit->second;
250  *log_ << " L1 name : " << l1name;
251  LumiSummary::L1 l1result = lumiSummary->l1info(lumiSummaryRH->getL1Index(l1name));
252  *log_ << " prescale : " << l1result.prescale << "\n";
253  *log_ << "\n";
254  }
255  ++c;
256  }
257  }
258  //
259  //accumulate hlt counts. Absent for now
260  //
266  //
267  //accumulate l1 counts
268  //
269  size_t n = lumiSummary->nTriggerLine();
270  for (size_t i = 0; i < n; ++i) {
271  std::string l1bitname = lumiSummaryRH->getL1Name(lumiSummary->l1info(i).triggernameidx);
272  l1PerBitInfo t;
273  if (currentlumi_ == 0) {
274  t.prescale = lumiSummary->l1info(i).prescale;
275  l1map_.insert(std::make_pair(l1bitname, t));
276  }
277  }
278 
279  perrunlumiinfo_.push_back(l);
280 
281  ++currentlumi_;
282 }
283 
284 // -----------------------------------------------------------------
302  //std::cout<<"valid trigger lines "<<trgpathMmap_.size()<<std::endl;
303  //std::cout<<"total lumi lines "<<perrunlumiinfo_.size()<<std::endl;
304  std::vector<MyPerLumiInfo>::const_iterator lumiIt;
305  std::vector<MyPerLumiInfo>::const_iterator lumiItBeg = perrunlumiinfo_.begin();
306  std::vector<MyPerLumiInfo>::const_iterator lumiItEnd = perrunlumiinfo_.end();
307  float recorded = 0.0;
308 
309  *log_ << "================ Run Summary " << run.run() << "================\n";
310  for (lumiIt = lumiItBeg; lumiIt != lumiItEnd; ++lumiIt) { //loop over LS
311  recorded += lumiIt->intglumi * lumiIt->livefraction;
312  }
313  *log_ << " CMS Recorded Lumi (e+27cm^-2) : " << recorded << "\n";
314  *log_ << " Effective Lumi (e+27cm^-2) per trigger path: "
315  << "\n\n";
316  std::multimap<std::string, std::string>::iterator it;
317  std::multimap<std::string, std::string>::iterator itBeg = trgpathMmap_.begin();
318  std::multimap<std::string, std::string>::iterator itEnd = trgpathMmap_.end();
319  unsigned int cc = 0;
320  for (it = itBeg; it != itEnd; ++it) {
321  *log_ << " " << cc << " " << it->first << " - " << it->second << " : ";
322  ++cc;
323  std::map<std::string, hltPerPathInfo>::const_iterator hltIt = hltmap_.find(it->first);
324  if (hltIt == hltmap_.end()) {
325  std::cout << "HLT path " << it->first << " not found" << std::endl;
326  *log_ << "\n";
327  continue;
328  }
329  std::map<std::string, l1PerBitInfo>::const_iterator l1It = l1map_.find(it->second);
330  if (l1It == l1map_.end()) {
331  std::cout << "L1 bit " << it->second << " not found" << std::endl;
332  *log_ << "\n";
333  continue;
334  }
335  unsigned int hltprescale = hltIt->second.prescale;
336  unsigned int l1prescale = l1It->second.prescale;
337  if (hltprescale != 0 && l1prescale != 0) {
338  float effectiveLumi = recorded / (hltprescale * l1prescale);
339  *log_ << effectiveLumi << "\n";
340  } else {
341  *log_ << "0 prescale exception\n";
342  continue;
343  }
344  *log_ << "\n";
345  }
346 }
347 
348 // -----------------------------------------------------------------
350 
351 std::vector<std::string> LumiCalculator::splitpathstr(const std::string& strValue, const std::string separator) {
352  std::vector<std::string> vecstrResult;
353  boost::regex re(separator);
354  boost::sregex_token_iterator p(strValue.begin(), strValue.end(), re, -1);
355  boost::sregex_token_iterator end;
356  while (p != end) {
357  vecstrResult.push_back(*p++);
358  }
359  return vecstrResult;
360 }
361 
363 #endif
size
Write out results.
const std::string & triggerName(unsigned int triggerIndex) const
string separator
Definition: mps_merge.py:79
LuminosityBlockNumber_t luminosityBlock() const
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void endLuminosityBlock(edm::LuminosityBlock const &lumiBlock, edm::EventSetup const &c) override
unsigned int prescale
unsigned int getL1Index(const std::string &name) const
Get index of given L1 trigger-name. Returns -1 if not found.
std::vector< std::string > splitpathstr(const std::string &strValue, const std::string separator)
unsigned int currentlumi_
Run const & getRun() const
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
std::string getL1Name(unsigned int idx) const
Get L1 name at given position.
~LumiCalculator() override
const std::string moduleType(const std::string &module) const
C++ class name of module.
std::vector< MyPerLumiInfo > perrunlumiinfo_
std::map< std::string, l1PerBitInfo > l1map_
unsigned int lsnum
void beginJob() override
unsigned int prescale
Definition: LumiSummary.h:26
unsigned int size() const
number of trigger paths in trigger table
void endRun(edm::Run const &, edm::EventSetup const &) override
unsigned int prescale
const edm::ParameterSet & modulePSet(const std::string &module) const
ParameterSet of module.
LumiCalculator(edm::ParameterSet const &pset)
const std::vector< std::string > & moduleLabels(unsigned int trigger) const
label(s) of module(s) on a trigger path
void beginRun(const edm::Run &run, const edm::EventSetup &c) override
bool getByLabel(std::string const &label, Handle< PROD > &result) const
Definition: Run.h:280
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
unsigned long long deadcount
HLTConfigProvider hltConfig_
Log< level::Info, false > LogInfo
bool getByLabel(std::string const &label, Handle< PROD > &result) const
LuminosityBlockID id() const
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
std::multimap< std::string, std::string > trgpathMmap_
edm::LogInfo * log_
const std::string & tableName() const
HLT ConfDB table name.
HLT enums.
std::map< std::string, hltPerPathInfo > hltmap_
void analyze(edm::Event const &e, edm::EventSetup const &c) override
void endJob() override
void beginLuminosityBlock(edm::LuminosityBlock const &lumiBlock, edm::EventSetup const &c) override
Definition: Run.h:45