CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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>
19  unsigned int prescale;
20 };
21 struct l1PerBitInfo{
23  unsigned int prescale;
24 };
26  unsigned int lsnum;
27  float livefraction;
28  float intglumi;
29  unsigned long long deadcount;
30 };
31 
33 public:
34 
35  explicit LumiCalculator(edm::ParameterSet const& pset);
36  virtual ~LumiCalculator();
37 
38 private:
39  virtual void beginJob();
40  virtual void beginRun(const edm::Run& run, const edm::EventSetup& c);
41  virtual void analyze(edm::Event const& e, edm::EventSetup const& c);
42  virtual void endLuminosityBlock(edm::LuminosityBlock const& lumiBlock,
43  edm::EventSetup const& c);
44  virtual void endRun(edm::Run const&, edm::EventSetup const&);
45  virtual void endJob();
46  std::vector<std::string> splitpathstr(const std::string& strValue,const std::string separator);
48  std::multimap<std::string,std::string> trgpathMmap_;//key:hltpath,value:l1bit
49  std::map<std::string,hltPerPathInfo> hltmap_;
50  std::map<std::string,l1PerBitInfo> l1map_;//
51  std::vector<MyPerLumiInfo> perrunlumiinfo_;
52 private:
55  unsigned int currentlumi_;
56 };//end class
57 
58 // -----------------------------------------------------------------
59 
60 LumiCalculator::LumiCalculator(edm::ParameterSet const& pset):log_( new edm::LogInfo("LumiReport")),currentlumi_(0){
61  showTrgInfo_=pset.getUntrackedParameter<bool>("showTriggerInfo",false);
62 }
63 
64 // -----------------------------------------------------------------
65 
67  delete log_; log_=0;
68 }
69 
70 // -----------------------------------------------------------------
71 
73 
74 }
75 
76 // -----------------------------------------------------------------
77 
79 
80 }
81 
82 // -----------------------------------------------------------------
83 
85  //std::cout<<"I'm in run number "<<run.run()<<std::endl;
86  //if(!hltConfig_.init("HLT")){
87  // throw cms::Exception("HLT process cannot be initialized");
88  //}
89  bool changed(true);
90  const std::string processname("HLT");
91  if(!hltConfig_.init(run,c,processname,changed)){
92  throw cms::Exception("HLT process cannot be initialized");
93  }
94  perrunlumiinfo_.clear();
95  trgpathMmap_.clear();
96  hltmap_.clear();
97  l1map_.clear();
98  //hltConfig_.dump("processName");
99  //hltConfig_.dump("TableName");
100  //hltConfig_.dump("Triggers");
101  //hltConfig_.dump("Modules");
102  if(showTrgInfo_){
103  *log_<<"======Trigger Configuration Overview======\n";
104  *log_<<"Run "<<run.run()<<" Trigger Table : "<<hltConfig_.tableName()<<"\n";
105  }
106  unsigned int totaltrg=hltConfig_.size();
107  for (unsigned int t=0;t<totaltrg;++t){
109  std::vector<std::string> numpathmodules=hltConfig_.moduleLabels(hltname);
110  if(showTrgInfo_){
111  *log_<<t<<" HLT path\t"<<hltname<<"\n";
112  }
114  hlt.prescale=1;
115  hltmap_.insert(std::make_pair(hltname,hlt));
116  std::vector<std::string>::iterator hltpathBeg=numpathmodules.begin();
117  std::vector<std::string>::iterator hltpathEnd=numpathmodules.end();
118  unsigned int mycounter=0;
119  for(std::vector<std::string>::iterator numpathmodule = hltpathBeg;
120  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->size()!=0 && showTrgInfo_) *log_<<"\t\tseed: "<<*i<<"\n";
156  if(i==seeds.begin()){//for now we take the first one from OR
157  trgpathMmap_.insert(std::make_pair(hltname,*i));
158  }
159  }
160  }
161  }else if (l1pathname.find("AND")!=std::string::npos){
162  if(showTrgInfo_){
163  *log_<<" L1SeedsLogicalExpression(ANDed)\t"<< l1pathname<< "\n";
164  }
165  std::vector<std::string> seeds=splitpathstr(l1pathname," AND ");
166  if(seeds.size()>2){
167  if(showTrgInfo_){
168  *log_<<"\tskip:contain >1 AND\n";
169  }
170  continue;
171  }else{
172  for(std::vector<std::string>::iterator i=seeds.begin();
173  i!=seeds.end();++i){
174  if(i->size()!=0 && showTrgInfo_) *log_<<"\t\tseed: "<<*i<<"\n";
175  if(i==seeds.begin()){//for now we take the first one
176  trgpathMmap_.insert(std::make_pair(hltname,*i));
177  }
178  }
179  }
180  }else{
181  if(showTrgInfo_){
182  *log_<<" L1SeedsLogicalExpression(ONE)\t"<< l1pathname<<"\n";
183  }
184  if(splitpathstr(l1pathname," NOT ").size()>1){
185  if(showTrgInfo_){
186  *log_<<"\tskip:contain NOT\n";
187  }
188  continue;
189  }
190  trgpathMmap_.insert(std::make_pair(hltname,l1pathname));
191  }
192  }
193  }
194  if(showTrgInfo_){
195  *log_<<"================\n";
196  }
197 }
198 
199 // -----------------------------------------------------------------
201  edm::EventSetup const& c){
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,std::multimap<std::string,std::string>::iterator > TRGMAPIT;
237  unsigned int c=0;
238  for(hltit=hltitBeg;hltit!=hltitEnd;++hltit){
239  std::string hltname=hltit->first;
240  *log_<<c<<" HLT path "<<hltname<<" , prescale : "<<hltit->second.prescale<<"\n";
241  TRGMAPIT ppp;
242  ppp=trgpathMmap_.equal_range(hltname);
243  if(ppp.first==ppp.second){
244  *log_<<" no L1\n";
245  }
246  for(std::multimap<std::string,std::string>::iterator mit=ppp.first; mit!=ppp.second; ++mit){
247  std::string l1name=mit->second;
248  *log_<<" L1 name : "<<l1name;
249  LumiSummary::L1 l1result = lumiSummary->l1info(lumiSummaryRH->getL1Index(l1name));
250  *log_<<" prescale : "<<l1result.prescale<<"\n";
251  *log_<<"\n";
252  }
253  ++c;
254  }
255  }
256  //
257  //accumulate hlt counts. Absent for now
258  //
264  //
265  //accumulate l1 counts
266  //
267  size_t n=lumiSummary->nTriggerLine();
268  for(size_t i=0;i<n;++i){
269  std::string l1bitname = lumiSummaryRH->getL1Name(lumiSummary->l1info(i).triggernameidx);
270  l1PerBitInfo t;
271  if(currentlumi_==0){
272  t.prescale=lumiSummary->l1info(i).prescale;
273  l1map_.insert(std::make_pair(l1bitname,t));
274  }
275  }
276 
277  perrunlumiinfo_.push_back(l);
278 
279  ++currentlumi_;
280 }
281 
282 // -----------------------------------------------------------------
300  //std::cout<<"valid trigger lines "<<trgpathMmap_.size()<<std::endl;
301  //std::cout<<"total lumi lines "<<perrunlumiinfo_.size()<<std::endl;
302  std::vector<MyPerLumiInfo>::const_iterator lumiIt;
303  std::vector<MyPerLumiInfo>::const_iterator lumiItBeg=perrunlumiinfo_.begin();
304  std::vector<MyPerLumiInfo>::const_iterator lumiItEnd=perrunlumiinfo_.end();
305  float recorded=0.0;
306 
307  *log_<<"================ Run Summary "<<run.run()<<"================\n";
308  for(lumiIt=lumiItBeg;lumiIt!=lumiItEnd;++lumiIt){//loop over LS
309  recorded += lumiIt->intglumi*lumiIt->livefraction;
310  }
311  *log_<<" CMS Recorded Lumi (e+27cm^-2) : "<<recorded<<"\n";
312  *log_<<" Effective Lumi (e+27cm^-2) per trigger path: "<<"\n\n";
313  std::multimap<std::string,std::string>::iterator it;
314  std::multimap<std::string,std::string>::iterator itBeg=trgpathMmap_.begin();
315  std::multimap<std::string,std::string>::iterator itEnd=trgpathMmap_.end();
316  unsigned int cc=0;
317  for(it=itBeg;it!=itEnd;++it){
318  *log_<<" "<<cc<<" "<<it->first<<" - "<<it->second<<" : ";
319  ++cc;
320  std::map<std::string,hltPerPathInfo>::const_iterator hltIt=hltmap_.find(it->first);
321  if( hltIt==hltmap_.end() ){
322  std::cout<<"HLT path "<<it->first<<" not found"<<std::endl;
323  *log_<<"\n";
324  continue;
325  }
326  std::map<std::string,l1PerBitInfo>::const_iterator l1It=l1map_.find(it->second);
327  if( l1It==l1map_.end() ){
328  std::cout<<"L1 bit "<<it->second<<" not found"<<std::endl;
329  *log_<<"\n";
330  continue;
331  }
332  unsigned int hltprescale=hltIt->second.prescale;
333  unsigned int l1prescale=l1It->second.prescale;
334  if( hltprescale!=0 && l1prescale!=0 ){
335  float effectiveLumi=recorded/(hltprescale*l1prescale);
336  *log_<<effectiveLumi<<"\n";
337  }else{
338  *log_<<"0 prescale exception\n";
339  continue;
340  }
341  *log_<<"\n";
342  }
343 }
344 
345 
346 // -----------------------------------------------------------------
348 }
349 
350 std::vector<std::string>
351 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
std::map< std::string, l1PerBitInfo > l1map_
unsigned int size() const
number of trigger paths in trigger table
LuminosityBlockID id() const
T getParameter(std::string const &) const
bool getByLabel(std::string const &label, Handle< PROD > &result) const
Definition: Run.h:176
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
unsigned int prescale
const std::string moduleType(const std::string &module) const
C++ class name of module.
RunNumber_t run() const
Definition: RunBase.h:42
const std::string & triggerName(unsigned int triggerIndex) const
virtual void endRun(edm::Run const &, edm::EventSetup const &)
std::vector< std::string > splitpathstr(const std::string &strValue, const std::string separator)
unsigned int currentlumi_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void endJob()
tuple lumiSummary
Definition: runregparse.py:290
const std::string & tableName() const
HLT ConfDB table name.
const edm::ParameterSet & modulePSet(const std::string &module) const
ParameterSet of module.
std::vector< MyPerLumiInfo > perrunlumiinfo_
bool getByLabel(std::string const &label, Handle< PROD > &result) const
virtual void analyze(edm::Event const &e, edm::EventSetup const &c)
unsigned int lsnum
unsigned int prescale
Definition: LumiSummary.h:27
virtual ~LumiCalculator()
unsigned int prescale
LumiCalculator(edm::ParameterSet const &pset)
#define end
Definition: vmac.h:38
virtual void beginRun(const edm::Run &run, const edm::EventSetup &c)
unsigned long long deadcount
HLTConfigProvider hltConfig_
const std::vector< std::string > & moduleLabels(unsigned int trigger) const
label(s) of module(s) on a trigger path
virtual void beginJob()
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
LuminosityBlockNumber_t luminosityBlock() const
edm::LogInfo * log_
std::map< std::string, hltPerPathInfo > hltmap_
tuple cout
Definition: gather_cfg.py:121
tuple size
Write out results.
virtual void endLuminosityBlock(edm::LuminosityBlock const &lumiBlock, edm::EventSetup const &c)
std::multimap< std::string, std::string > trgpathMmap_
Definition: Run.h:36
Run const & getRun() const