CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
L1TRate_Offline.cc
Go to the documentation of this file.
1  /*
2  * \file L1TRate_Offline.cc
3  *
4  * $Date: 2012/12/03 15:03:34 $
5  * $Revision: 1.6 $
6  * \author J. Pela, P. Musella
7  *
8  */
9 
10 // L1TMonitor includes
12 
14 
16 
21 #include "DataFormats/Common/interface/ConditionsInEdm.h" // Parameters associated to Run, LS and Event
22 #include "DataFormats/Luminosity/interface/LumiDetails.h" // Luminosity Information
23 #include "DataFormats/Luminosity/interface/LumiSummary.h" // Luminosity Information
24 
32 
34 
35 #include "TList.h"
36 
37 using namespace edm;
38 using namespace std;
39 
40 //_____________________________________________________________________
42 
43  m_maxNbins = 2500; // Maximum LS for each run (for binning purposes)
44  m_parameters = ps;
45 
46  // Mapping parameter input variables
47  m_scalersSource = m_parameters.getParameter <InputTag>("inputTagScalersResults");
48  m_l1GtDataDaqInputTag = m_parameters.getParameter <InputTag>("inputTagL1GtDataDaq");
49  m_verbose = m_parameters.getUntrackedParameter<bool> ("verbose",false);
50  m_refPrescaleSet = m_parameters.getParameter <int> ("refPrescaleSet");
51  m_lsShiftGTRates = m_parameters.getUntrackedParameter<int> ("lsShiftGTRates",0);
52 
53  // Getting which categories to monitor
54  ParameterSet Categories = ps.getParameter<ParameterSet>("categories");
55  m_inputCategories["Mu"] = Categories.getUntrackedParameter<bool>("Mu");
56  m_inputCategories["EG"] = Categories.getUntrackedParameter<bool>("EG");
57  m_inputCategories["IsoEG"] = Categories.getUntrackedParameter<bool>("IsoEG");
58  m_inputCategories["Jet"] = Categories.getUntrackedParameter<bool>("Jet");
59  m_inputCategories["CenJet"] = Categories.getUntrackedParameter<bool>("CenJet");
60  m_inputCategories["ForJet"] = Categories.getUntrackedParameter<bool>("ForJet");
61  m_inputCategories["TauJet"] = Categories.getUntrackedParameter<bool>("TauJet");
62  m_inputCategories["ETM"] = Categories.getUntrackedParameter<bool>("ETM");
63  m_inputCategories["ETT"] = Categories.getUntrackedParameter<bool>("ETT");
64  m_inputCategories["HTT"] = Categories.getUntrackedParameter<bool>("HTT");
65  m_inputCategories["HTM"] = Categories.getUntrackedParameter<bool>("HTM");
66 
67  // Inicializing Variables
68  if (m_verbose) {
69  cout << "[L1TRate_Offline:] ____________ Storage inicialization ____________ " << endl;
70  cout << "[L1TRate_Offline:] Setting up dbe folder: L1T/L1TRate_Offline" << endl;
71  }
72 
73  dbe = Service < DQMStore > ().operator->();
74  dbe->setVerbose(0);
75  dbe->setCurrentFolder("L1T/L1TRate_Offline");
76 
77  // Inicializing Variables
78  if (m_verbose) {cout << "[L1TRate_Offline:] Pointer for DQM Store: " << dbe << endl;}
79 }
80 
81 //_____________________________________________________________________
83 
84 //_____________________________________________________________________
86 
87  if (m_verbose) {cout << "[L1TRate_Offline:] Called beginJob." << endl;}
88 
89  // get hold of back-end interface
90  DQMStore *dbe = 0;
91  dbe = Service < DQMStore > ().operator->();
92 
93  if (dbe) {
94  dbe->setCurrentFolder("L1T/L1TRate_Offline");
95  dbe->rmdir("L1T/L1TRate_Offline");
96  }
97 
98 }
99 
100 //_____________________________________________________________________
102 
103  if (m_verbose) {cout << "[L1TRate_Offline:] Called endJob." << endl;}
104 
105  if (m_outputFile.size() != 0 && dbe)
106  dbe->save(m_outputFile);
107 
108  return;
109 
110 }
111 
112 //_____________________________________________________________________
113 // BeginRun: as input you get filtered events...
114 //_____________________________________________________________________
116 
117  if (m_verbose) {cout << "[L1TRate_Offline:] Called beginRun." << endl;}
118 
121 
122  iSetup.get<L1GtTriggerMenuRcd>() .get(menuRcd);
123  iSetup.get<L1GtPrescaleFactorsAlgoTrigRcd>().get(l1GtPfAlgo);
124 
125  const L1GtTriggerMenu* menu = menuRcd .product();
126  const L1GtPrescaleFactors* m_l1GtPfAlgo = l1GtPfAlgo.product();
127 
128  // Initializing DQM Monitor Elements
129  dbe->setCurrentFolder("L1T/L1TRate_Offline");
130  m_ErrorMonitor = dbe->book1D("ErrorMonitor", "ErrorMonitor",2,0,2);
131  m_ErrorMonitor->setBinLabel(UNKNOWN ,"UNKNOWN");
132  m_ErrorMonitor->setBinLabel(WARNING_PY_MISSING_FIT,"WARNING_PY_MISSING_FIT");
133 
134  cout << "[L1TRate_Offline:] m_ErrorMonitor: " << m_ErrorMonitor << endl;
135 
136  // Retriving the list of prescale sets
137  m_listsPrescaleFactors = &(m_l1GtPfAlgo->gtPrescaleFactors());
138 
139  // Getting Lowest Prescale Single Object Triggers from the menu
140  L1TMenuHelper myMenuHelper = L1TMenuHelper(iSetup);
141  m_selectedTriggers = myMenuHelper.getLUSOTrigger(m_inputCategories,m_refPrescaleSet);
142 
143  //-> Getting template fits for the algLo cross sections
144  getXSexFitsPython(m_parameters);
145 
146  for (CItAlgo algo = menu->gtAlgorithmMap().begin(); algo!=menu->gtAlgorithmMap().end(); ++algo){
147  m_algoBit[(algo->second).algoAlias()] = (algo->second).algoBitNumber();
148  }
149 
150  double minInstantLuminosity = m_parameters.getParameter<double>("minInstantLuminosity");
151  double maxInstantLuminosity = m_parameters.getParameter<double>("maxInstantLuminosity");
152 
153  // Initializing DQM Monitor Elements
154  for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
155 
156  TString tCategory = (*i).first;
157  TString tTrigger = (*i).second;
158 
159  TString tErrorMessage = "";
160  TF1* tTestFunction;
161 
162  if(tTrigger != "Undefined" && m_templateFunctions.find(tTrigger) != m_templateFunctions.end()){
163  tTestFunction = m_templateFunctions[tTrigger];
164  }
165  else if(tTrigger == "Undefined"){
166  TString tFunc = "-1";
167  tTestFunction = new TF1("FitParametrization_"+tTrigger,tFunc,0,double(m_maxNbins)-0.5);
168  }
169  else if(m_templateFunctions.find(tTrigger) == m_templateFunctions.end()){
170  TString tFunc = "-1";
171  tTestFunction = new TF1("FitParametrization_"+tTrigger,tFunc,0,double(m_maxNbins)-0.5);
172  tErrorMessage = " (Undefined Test Function)";
173  }
174  else{
175  TString tFunc = "-1";
176  tTestFunction = new TF1("FitParametrization_"+tTrigger,tFunc,0,double(m_maxNbins)-0.5);
177  }
178 
179  if(tTrigger != "Undefined"){
180 
181  if(myMenuHelper.getPrescaleByAlias(tCategory,tTrigger) != 1){
182  tErrorMessage += " WARNING: Default Prescale = ";
183  tErrorMessage += myMenuHelper.getPrescaleByAlias(tCategory,tTrigger);
184  }
185 
186  if (tCategory == "Mu" && myMenuHelper.getEtaRangeByAlias(tCategory,tTrigger) != 4294967295){ //hexadecimal of the whole range
187  tErrorMessage += " WARNING: Eta Range = ";
188  tErrorMessage += myMenuHelper.getEtaRangeByAlias(tCategory,tTrigger);
189  }
190  else if(tCategory == "EG" && myMenuHelper.getEtaRangeByAlias(tCategory,tTrigger) != 32639){
191  tErrorMessage += " WARNING: Eta Range = ";
192  tErrorMessage += myMenuHelper.getEtaRangeByAlias(tCategory,tTrigger);
193  }
194  else if(tCategory == "IsoEG" && myMenuHelper.getEtaRangeByAlias(tCategory,tTrigger) != 32639){
195  tErrorMessage += " WARNING: Eta Range = ";
196  tErrorMessage += myMenuHelper.getEtaRangeByAlias(tCategory,tTrigger);
197  }
198 
199  if(tCategory == "Mu" && myMenuHelper.getQualityAlias(tCategory,tTrigger) != 240){
200  tErrorMessage += " WARNING: Quality = ";
201  tErrorMessage += myMenuHelper.getQualityAlias(tCategory,tTrigger);
202  }
203 
204  }
205 
206  dbe->setCurrentFolder("L1T/L1TRate_Offline/TriggerCounts"); // trigger counts...
207  m_CountsVsLS[tTrigger] = dbe->bookProfile(tCategory,
208  "Cross Sec. vs Inst. Lumi Algo: "+tTrigger+tErrorMessage,
209  m_maxNbins,
210  minInstantLuminosity,
211  maxInstantLuminosity,0,500);
212  m_CountsVsLS[tTrigger] ->setAxisTitle("Instantaneous Luminosity [10^{30}cm^{-2}s^{-1}]" ,1);
213  m_CountsVsLS[tTrigger] ->setAxisTitle("Algorithm #sigma [#mu b]" ,2);
214  m_CountsVsLS[tTrigger] ->getTProfile()->GetListOfFunctions()->Add(tTestFunction);
215  m_CountsVsLS[tTrigger] ->getTProfile()->SetMarkerStyle(23);
216 
217  m_algoFit[tTrigger] = (TF1*) tTestFunction->Clone("Fit_"+tTrigger); // NOTE: Workaround
218 
219  dbe->setCurrentFolder("L1T/L1TRate_Offline/Ratio");
220  m_xSecObservedToExpected[tTrigger] = dbe->book1D(tCategory, "Algo: "+tTrigger+tErrorMessage,m_maxNbins,-0.5,double(m_maxNbins)-0.5);
221  m_xSecObservedToExpected[tTrigger] ->setAxisTitle("Lumi Section" ,1);
222  m_xSecObservedToExpected[tTrigger] ->setAxisTitle("#sigma_{obs} / #sigma_{exp}" ,2);
223 
224 
225  }
226 
227 }
228 
229 //_____________________________________________________________________
231 
232  if (m_verbose) {cout << "[L1TRate_Offline:] Called endRun." << endl;}
233 }
234 
235 //_____________________________________________________________________
237 
238  if (m_verbose) {cout << "[L1TRate_Offline:] Called beginLuminosityBlock at LS=" << lumiBlock.id().luminosityBlock() << endl;}
239 
240 }
241 
242 //_____________________________________________________________________
244 
245  int eventLS = lumiBlock.id().luminosityBlock();
246  if (m_verbose) {cout << "[L1TRate_Offline:] Called endLuminosityBlock at LS=" << eventLS << endl;}
247 
248  // We can certify LS -1 since we should have available:
249  // gt rates: (current LS)-1
250  // prescale: current LS
251  // lumi : current LS
252  //eventLS--;
253 
254  // Checking if all necessary quantities are defined for our calculations
255  //bool isDefRate,isDefLumi,isDefPrescaleIndex;
256  bool isDefLumi,isDefPrescaleIndex;
257  //map<TString,double>* rates=0;
258  double lumi=0;
259  int prescalesIndex=0;
260 
261  bool isDefCount;
262  map<TString,double>* counts=0;
263 
264  // Resetting MonitorElements so we can refill them
265  for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
266  string tTrigger = (*i).second;
267  m_CountsVsLS [tTrigger]->getTH1()->Reset("ICE");
268  m_xSecObservedToExpected[tTrigger]->getTH1()->Reset("ICE");
269 
270  }
271 
272  //Trying to do the same with Counts....
273  for(map<int,map<TString,double> >::iterator j=m_lsRates.begin() ; j!=m_lsRates.end() ; j++){
274 
275  unsigned int lsOffline = (*j).first;
276  counts = &(*j).second;
277  isDefCount=true;
278 
279  unsigned int lsPreInd;
280 
281  if(m_lsLuminosity.find(lsOffline)==m_lsLuminosity.end()){isDefLumi=false;}
282  else{
283  isDefLumi=true;
284  lumi=m_lsLuminosity[lsOffline];
285  }
286 
287  lsPreInd = lsOffline + 1; // NOTE: Workaround
288 
289  if(m_lsPrescaleIndex.find(lsPreInd)==m_lsPrescaleIndex.end()){isDefPrescaleIndex=false;}
290  else{
291  isDefPrescaleIndex=true;
292  prescalesIndex=m_lsPrescaleIndex[lsPreInd];
293  }
294 
295  if(isDefCount && isDefLumi && isDefPrescaleIndex){
296 
297  //const vector<int>& currentPrescaleFactors = (*m_listsPrescaleFactors).at(prescalesIndex);
298 
299  for(map<string,string>::const_iterator j=m_selectedTriggers.begin() ; j!=m_selectedTriggers.end() ; j++){
300 
301  string tTrigger = (*j).second;
302  double trigCount = (*counts)[tTrigger];
303 
304  // TF1* tTestFunction = (TF1*) m_CountsVsLS[tTrigger]->getTProfile()->GetListOfFunctions()->First();
305  TF1* tTestFunction = m_algoFit[tTrigger]; // NOTE: Workaround....
306 
307 
308  // If trigger name is defined we get the rate fit parameters
309  if(tTrigger != "Undefined"){
310 
311 
312  if(lumi!=0 && trigCount!=0 && prescalesIndex!=0){
313 
314  double AlgoXSec = (prescalesIndex*trigCount)/lumi;
315  double TemplateFunctionValue = tTestFunction->Eval(lumi);
316 
317  // Checking against Template function
318  m_CountsVsLS [tTrigger]->Fill(lumi,AlgoXSec);
319 
320  int ibin = m_xSecObservedToExpected[tTrigger]->getTH1()->FindBin(lsOffline);
321  m_xSecObservedToExpected[tTrigger]->setBinContent(ibin,AlgoXSec/TemplateFunctionValue);
322 
323 
324  }
325  else {
326  m_CountsVsLS [tTrigger]->Fill(0.000001,0.000001);
327 
328  int ibin = m_xSecObservedToExpected[tTrigger]->getTH1()->FindBin(lsOffline);
329  m_xSecObservedToExpected[tTrigger]->setBinContent(ibin,0.000001);
330  }
331  }
332  }
333  }
334  }
335 }
336 
337 
338 //_____________________________________________________________________
339 void L1TRate_Offline::analyze(const Event & iEvent, const EventSetup & eventSetup){
340 
341  edm::Handle<L1GlobalTriggerReadoutRecord> gtReadoutRecordData;
344 
345  iEvent.getByLabel(m_l1GtDataDaqInputTag, gtReadoutRecordData);
346  iEvent.getByLabel(m_scalersSource , colLScal);
347  iEvent.getByLabel(m_scalersSource , triggerScalers);
348 
349  // Integers
350  //int EventRun = iEvent.id().run();
351  unsigned int eventLS = iEvent.id().luminosityBlock();
352 
353  // Getting the trigger trigger rates from GT and buffering it
354  if(triggerScalers.isValid() && triggerScalers->size()){
355 
356  Level1TriggerScalersCollection::const_iterator itL1TScalers = triggerScalers->begin();
357  //Level1TriggerRates trigRates(*itL1TScalers,EventRun);
358 
359  // Trying to get the trigger counts
360  const std::vector<unsigned int> gtAlgoCounts = itL1TScalers->gtAlgoCounts();
361 
362  int gtLS = (*itL1TScalers).lumiSegmentNr()+m_lsShiftGTRates;
363 
364  // If we haven't got the data from this LS yet get it
365  if(m_lsRates.find(gtLS)==m_lsRates.end()){
366 
367  map<TString,double> bufferCount;
368 
369  // Buffer the rate informations for all selected bits
370  for(map<string,string>::const_iterator i=m_selectedTriggers.begin(); i!=m_selectedTriggers.end() ; i++){
371 
372  string tTrigger = (*i).second;
373 
374  // If trigger name is defined we store the rate
375  if(tTrigger != "Undefined"){
376 
377  unsigned int trigBit = m_algoBit[tTrigger];
378  double trigCount = gtAlgoCounts[trigBit];
379 
380  bufferCount[tTrigger] = trigCount;
381 
382  }
383  }
384  m_lsRates[gtLS] = bufferCount;
385  }
386  }
387 
388 
389  // Getting from the SCAL the luminosity information and buffering it
390  if(colLScal.isValid() && colLScal->size()){
391 
392  LumiScalersCollection::const_iterator itLScal = colLScal->begin();
393  unsigned int scalLS = itLScal->sectionNumber();
394 
395  // If we haven't got the data from this SCAL LS yet get it
396  if(m_lsLuminosity.find(scalLS)==m_lsLuminosity.end()){
397 
398  if (m_verbose) {cout << "[L1TRate_Offline:] Buffering SCAL-HF Lumi for LS=" << scalLS << endl;}
399  double instLumi = itLScal->instantLumi(); // Getting Instant Lumi from HF (via SCAL) // <###### WE NEED TO STORE THIS
400  double deadTimeNormHF = itLScal->deadTimeNormalization(); // Getting Dead Time Normalization from HF (via SCAL)
401 
402  // If HF Dead Time Corrections is requested we apply it
403  // NOTE: By default this is assumed false since for now WbM fits do NOT assume this correction
404  if(m_parameters.getUntrackedParameter<bool>("useHFDeadTimeNormalization",false)){
405 
406  // Protecting for deadtime = 0
407  if(deadTimeNormHF==0){instLumi = 0;}
408  else {instLumi = instLumi/deadTimeNormHF;}
409  }
410  // Buffering the luminosity information
411  m_lsLuminosity[scalLS]=instLumi;
412  }
413  }
414 
415  // Getting the prescale index used when this event was triggered
416  if(gtReadoutRecordData.isValid()){
417 
418  // If we haven't got the data from this LS yet get it
419  if(m_lsPrescaleIndex.find(eventLS)==m_lsPrescaleIndex.end()){
420 
421  if (m_verbose) {cout << "[L1TRate_Offline:] Buffering Prescale Index for LS=" << eventLS << endl;}
422 
423  // Getting Final Decision Logic (FDL) Data from GT
424  const vector<L1GtFdlWord>& gtFdlVectorData = gtReadoutRecordData->gtFdlVector();
425 
426  // Getting the index for the fdl data for this event
427  int indexFDL=0;
428  for(unsigned int i=0; i<gtFdlVectorData.size(); i++){
429  if(gtFdlVectorData[i].bxInEvent()==0){indexFDL=i; break;}
430  }
431 
432  int CurrentPrescalesIndex = gtFdlVectorData[indexFDL].gtPrescaleFactorIndexAlgo(); // <###### WE NEED TO STORE THIS
433  m_lsPrescaleIndex[eventLS] = CurrentPrescalesIndex;
434 
435  }
436 
437  }
438 
439 
440 }
441 
442 //_____________________________________________________________________
443 // function: getXSexFitsPython
444 // Imputs:
445 // * const edm::ParameterSet& ps = ParameterSet contaning the fit
446 // functions and parameters for the selected triggers
447 // Outputs:
448 // * int error = Number of algos where you did not find a
449 // corresponding fit
450 //_____________________________________________________________________
452 
453  // error meaning
454  bool noError = true;
455 
456  // Getting fit parameters
457  std::vector<edm::ParameterSet> m_fitParameters = ps.getParameter< vector<ParameterSet> >("fitParameters");
458 
459  double minInstantLuminosity = m_parameters.getParameter<double>("minInstantLuminosity");
460  double maxInstantLuminosity = m_parameters.getParameter<double>("maxInstantLuminosity");
461 
462  // Getting rate fit parameters for all input triggers
463  for(map<string,string>::const_iterator a=m_selectedTriggers.begin() ; a!=m_selectedTriggers.end() ; a++){
464 
465  string tTrigger = (*a).second;
466 
467  // If trigger name is defined we get the rate fit parameters
468  if(tTrigger != "Undefined"){
469 
470  bool foundFit = false;
471 
472  for(unsigned int b=0 ; b<m_fitParameters.size() ; b++){
473 
474  if(tTrigger == m_fitParameters[b].getParameter<string>("AlgoName")){
475 
476  TString tAlgoName = m_fitParameters[b].getParameter< string > ("AlgoName");
477  TString tTemplateFunction = m_fitParameters[b].getParameter< string > ("TemplateFunction");
478  vector<double> tParameters = m_fitParameters[b].getParameter< vector<double> >("Parameters");
479 
480  // Retriving and populating the m_templateFunctions array
481  m_templateFunctions[tTrigger] = new TF1("FitParametrization_"+tAlgoName,tTemplateFunction,
482  minInstantLuminosity,maxInstantLuminosity);
483  m_templateFunctions[tTrigger] ->SetParameters(&tParameters[0]);
484  m_templateFunctions[tTrigger] ->SetLineWidth(1);
485  m_templateFunctions[tTrigger] ->SetLineColor(kRed);
486 
487  foundFit = true;
488  break;
489  }
490 
491  if(!foundFit){
492 
493  noError = false;
494 
495  int eCount = m_ErrorMonitor->getTH1()->GetBinContent(WARNING_PY_MISSING_FIT);
496  eCount++;
497  m_ErrorMonitor->getTH1()->SetBinContent(WARNING_PY_MISSING_FIT,eCount);
498 
499  }
500  }
501  }
502  }
503 
504  return noError;
505 
506 }
507 
508 
509 
510 //define this as a plug-in
virtual void beginLuminosityBlock(edm::LuminosityBlock const &lumiBlock, edm::EventSetup const &c)
LuminosityBlockID id() const
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
void rmdir(const std::string &fullpath)
Definition: DQMStore.cc:2535
tuple lumi
Definition: fjr2json.py:35
0: Unidentified isolated particle
Definition: ParticleCode.h:19
virtual void endLuminosityBlock(edm::LuminosityBlock const &lumiBlock, edm::EventSetup const &c)
DEFINE_FWK_MODULE(HiMixingModule)
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:43
int getPrescaleByAlias(TString iCategory, TString iAlias)
bool getXSexFitsPython(const edm::ParameterSet &ps)
dictionary map
Definition: Association.py:205
unsigned int getQualityAlias(TString iCategory, TString iAlias)
AlgorithmMap::const_iterator CItAlgo
iterators through map containing the algorithms
int iEvent
Definition: GenABIO.cc:243
std::map< std::string, std::string > getLUSOTrigger(std::map< std::string, bool > iCategories, int IndexRefPrescaleFactors)
virtual ~L1TRate_Offline()
L1TRate_Offline(const edm::ParameterSet &ps)
void analyze(const edm::Event &e, const edm::EventSetup &c)
int j
Definition: DBlmapReader.cc:9
bool isValid() const
Definition: HandleBase.h:76
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
void beginRun(const edm::Run &run, const edm::EventSetup &iSetup)
unsigned int getEtaRangeByAlias(TString iCategory, TString iAlias)
const std::vector< std::vector< int > > & gtPrescaleFactors() const
get the prescale factors by reference
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
double b
Definition: hdecay.h:120
LuminosityBlockNumber_t luminosityBlock() const
void endRun(const edm::Run &run, const edm::EventSetup &iSetup)
edm::EventID id() const
Definition: EventBase.h:56
double a
Definition: hdecay.h:121
const AlgorithmMap & gtAlgorithmMap() const
get / set the algorithm map (by name)
tuple cout
Definition: gather_cfg.py:121
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:434
Definition: Run.h:36