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