CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
L1TriggerJSONMonitoring.cc
Go to the documentation of this file.
1 
11 
13 
17 
19 
20 #include <fstream>
21 using namespace jsoncollector;
22 
24  level1Results_(ps.getParameter<edm::InputTag>("L1Results")),
25  level1ResultsToken_(consumes<GlobalAlgBlkBxCollection>(level1Results_))
26 {
27 
28 
29 }
30 
32 {
33 }
34 
35 void
38  desc.add<edm::InputTag>("L1Results",edm::InputTag("hltGtStage2Digis"));
39  descriptions.add("L1TMonitoring", desc);
40 }
41 
42 void
44 {
45 
46  using namespace std;
47  using namespace edm;
48 
49  processed_++;
50 
51  int ex = iEvent.experimentType();
52  if (ex == 1) L1Global_[0]++;
53  else if (ex == 2) L1Global_[1]++;
54  else if (ex == 3) L1Global_[2]++;
55  else{
56  LogDebug("L1TriggerJSONMonitoring") << "Not Physics, Calibration or Random. experimentType = " << ex << std::endl;
57  }
58 
59  //Get hold of L1TResults
61  if (not iEvent.getByToken(level1ResultsToken_, l1tResults) or (l1tResults->begin(0) == l1tResults->end(0))){
62  LogDebug("L1TriggerJSONMonitoring") << "Could not get L1 trigger results" << std::endl;
63  return;
64  }
65 
66  //The GlobalAlgBlkBxCollection is a vector of vectors, but the second layer can only ever
67  //have one entry since there can't be more than one collection per bunch crossing. The "0"
68  //here means BX = 0, the "begin" is used to access the first and only element
69  std::vector<GlobalAlgBlk>::const_iterator algBlk = l1tResults->begin(0);
70 
71  for (unsigned int i = 0; i < algBlk->maxPhysicsTriggers; i++){
72  if (algBlk->getAlgoDecisionFinal(i)){
73  L1AlgoAccept_[i]++;
74  if (ex == 1) L1AlgoAcceptPhysics_[i]++;
75  if (ex == 2) L1AlgoAcceptCalibration_[i]++;
76  if (ex == 3) L1AlgoAcceptRandom_[i]++;
77  }
78  }
79 
80  //Prescale index
81  prescaleIndex_ = static_cast<unsigned int>(algBlk->getPreScColumn());
82 
83  //Check that the prescale index hasn't changed inside a lumi section
84  unsigned int newLumi = (unsigned int) iEvent.eventAuxiliary().luminosityBlock();
85  if (oldLumi == newLumi and prescaleIndex_ != oldPrescaleIndex){
86  LogWarning("L1TriggerJSONMonitoring")<<"Prescale index has changed from "<<oldPrescaleIndex<<" to "<<prescaleIndex_<<" inside lumi section "<<newLumi;
87  }
88  oldLumi = newLumi;
90 
91 }//End analyze function
92 
93 void
95  //Reset total number of events
96  processed_ = 0;
97 
98  //Reset per-path counters
99  //Reset L1 per-algo counters -
100  for (unsigned int i = 0; i < L1AlgoAccept_.size(); i++) {
101  L1AlgoAccept_[i] = 0;
102  L1AlgoAcceptPhysics_[i] = 0;
104  L1AlgoAcceptRandom_[i] = 0;
105  }
106  //Reset L1 global counters -
107  for (unsigned int i = 0; i < L1GlobalType_.size(); i++) {
108  L1Global_[i] = 0;
109  }
110 
111  //Prescale index
112  prescaleIndex_ = 100;
113 
114 }//End resetLumi function
115 
116 void
118 {
120  iSetup.get<L1TUtmTriggerMenuRcd>().get(l1GtMenu);
121 
122  //Get the run directory from the EvFDaqDirector
124  else baseRunDir_ = ".";
125 
126  std::string monPath = baseRunDir_ + "/";
127 
128  //Need this to get at maximum number of triggers
129  GlobalAlgBlk alg = GlobalAlgBlk();
130 
131  //Update trigger and dataset names, clear L1 names and counters
132  L1AlgoNames_.resize(alg.maxPhysicsTriggers);
133  for (unsigned int i = 0; i < L1AlgoNames_.size(); i++) {
134  L1AlgoNames_.at(i) = "";
135  }
136 
137  //Get L1 algorithm trigger names -
138  const L1TUtmTriggerMenu* m_l1GtMenu;
139  const std::map<std::string, L1TUtmAlgorithm>* m_algorithmMap;
140 
141  m_l1GtMenu = l1GtMenu.product();
142  m_algorithmMap = &(m_l1GtMenu->getAlgorithmMap());
143 
144  for (std::map<std::string, L1TUtmAlgorithm>::const_iterator itAlgo = m_algorithmMap->begin(); itAlgo != m_algorithmMap->end(); itAlgo++) {
145  int bitNumber = (itAlgo->second).getIndex();
146  L1AlgoNames_.at(bitNumber) = itAlgo->first;
147  }
148 
149  L1GlobalType_.clear();
150  L1Global_.clear();
151 
152  //Set the experimentType -
153  L1GlobalType_.push_back( "Physics" );
154  L1GlobalType_.push_back( "Calibration" );
155  L1GlobalType_.push_back( "Random" );
156 
157  const unsigned int la = L1AlgoNames_.size();
158  const unsigned int lg = L1GlobalType_.size();
159 
160  //Resize per-path counters
161  L1AlgoAccept_.resize(la);
162  L1AlgoAcceptPhysics_.resize(la);
163  L1AlgoAcceptCalibration_.resize(la);
164  L1AlgoAcceptRandom_.resize(la);
165 
166  L1Global_.resize(lg);
167 
168  resetLumi();
169 
170  //Write the once-per-run files if not already written
171  //Eventually must rewrite this with proper multithreading (i.e. globalBeginRun)
172  bool expected = false;
173  if( runCache()->wroteFiles.compare_exchange_strong(expected, true) ){
174  runCache()->wroteFiles = true;
175 
176  unsigned int nRun = iRun.run();
177 
178  //Create definition file for L1 Rates -
179  std::stringstream ssL1Jsd;
180  ssL1Jsd << "run" << std::setfill('0') << std::setw(6) << nRun << "_ls0000";
181  ssL1Jsd << "_streamL1Rates_pid" << std::setfill('0') << std::setw(5) << getpid() << ".jsd";
182  stL1Jsd_ = ssL1Jsd.str();
183 
185 
186  //Write ini files
187  //L1
188  Json::Value l1Ini;
189  Json::StyledWriter writer;
190 
191  Json::Value l1AlgoNamesVal(Json::arrayValue);
192  for (unsigned int ui = 0; ui < L1AlgoNames_.size(); ui++){
193  l1AlgoNamesVal.append(L1AlgoNames_.at(ui));
194  }
195 
196  Json::Value eventTypeVal(Json::arrayValue);
197  for (unsigned int ui = 0; ui < L1GlobalType_.size(); ui++){
198  eventTypeVal.append(L1GlobalType_.at(ui));
199  }
200 
201  l1Ini["L1-Algo-Names"] = l1AlgoNamesVal;
202  l1Ini["Event-Type"] = eventTypeVal;
203 
204  std::string && result = writer.write(l1Ini);
205 
206  std::stringstream ssL1Ini;
207  ssL1Ini << "run" << std::setfill('0') << std::setw(6) << nRun << "_ls0000_streamL1Rates_pid" << std::setfill('0') << std::setw(5) << getpid() << ".ini";
208 
209  std::ofstream outL1Ini( monPath + ssL1Ini.str() );
210  outL1Ini<<result;
211  outL1Ini.close();
212  }
213 
214  //Initialize variables for verification of prescaleIndex
215  oldLumi = 0;
216  oldPrescaleIndex = 100;
217 
218 }//End beginRun function
219 
221 
222 std::shared_ptr<l1Json::lumiVars>
224 {
225  std::shared_ptr<l1Json::lumiVars> iSummary(new l1Json::lumiVars);
226 
227  unsigned int MAXPATHS = 512;
228 
229  iSummary->processed = new HistoJ<unsigned int>(1, 1);
230 
231  iSummary->prescaleIndex = 100;
232 
233  iSummary->L1AlgoAccept = new HistoJ<unsigned int>(1, MAXPATHS);
234  iSummary->L1AlgoAcceptPhysics = new HistoJ<unsigned int>(1, MAXPATHS);
235  iSummary->L1AlgoAcceptCalibration = new HistoJ<unsigned int>(1, MAXPATHS);
236  iSummary->L1AlgoAcceptRandom = new HistoJ<unsigned int>(1, MAXPATHS);
237  iSummary->L1Global = new HistoJ<unsigned int>(1, MAXPATHS);
238 
239  iSummary->baseRunDir = "";
240  iSummary->stL1Jsd = "";
241  iSummary->streamL1Destination = "";
242 
243  return iSummary;
244 }//End globalBeginLuminosityBlockSummary function
245 
246 void
248 
249  //Whichever stream gets there first does the initialiazation
250  if (iSummary->L1AlgoAccept->value().size() == 0){
251  iSummary->processed->update(processed_);
252 
253  iSummary->prescaleIndex = prescaleIndex_;
254 
255  iSummary->baseRunDir = baseRunDir_;
256 
257  for (unsigned int ui = 0; ui < L1AlgoAccept_.size(); ui++){
258  iSummary->L1AlgoAccept ->update(L1AlgoAccept_.at(ui));
262  }
263  for (unsigned int ui = 0; ui < L1GlobalType_.size(); ui++){
264  iSummary->L1Global ->update(L1Global_.at(ui));
265  }
266  iSummary->stL1Jsd = stL1Jsd_;
267 
268  iSummary->streamL1Destination = runCache()->streamL1Destination;
269  }
270 
271  else{
272  iSummary->processed->value().at(0) += processed_;
273 
274  for (unsigned int ui = 0; ui < L1AlgoAccept_.size(); ui++){
275  iSummary->L1AlgoAccept->value().at(ui) += L1AlgoAccept_.at(ui);
276  iSummary->L1AlgoAcceptPhysics->value().at(ui) += L1AlgoAcceptPhysics_.at(ui);
278  iSummary->L1AlgoAcceptRandom->value().at(ui) += L1AlgoAcceptRandom_.at(ui);
279  }
280  for (unsigned int ui = 0; ui < L1Global_.size(); ui++){
281  iSummary->L1Global->value().at(ui) += L1Global_.at(ui);
282  }
283 
284  }
285 
286 }//End endLuminosityBlockSummary function
287 
288 
289 void
291 {
292 
293  unsigned int iLs = iLumi.luminosityBlock();
294  unsigned int iRun = iLumi.run();
295 
296  bool writeFiles=true;
297  if (edm::Service<evf::MicroStateService>().isAvailable()) {
299  if (fms) {
300  writeFiles = fms->shouldWriteFiles(iLumi.luminosityBlock());
301  }
302  }
303 
304  if (writeFiles) {
305  Json::StyledWriter writer;
306 
307  char hostname[33];
308  gethostname(hostname,32);
309  std::string sourceHost(hostname);
310 
311  //Get the output directory
312  std::string monPath = iSummary->baseRunDir + "/";
313 
314  std::stringstream sOutDef;
315  sOutDef << monPath << "output_" << getpid() << ".jsd";
316 
317  //Write the .jsndata files which contain the actual rates
318  //L1 .jsndata file
319  Json::Value l1JsnData;
320  l1JsnData[DataPoint::SOURCE] = sourceHost;
321  l1JsnData[DataPoint::DEFINITION] = iSummary->stL1Jsd;
322 
323  l1JsnData[DataPoint::DATA].append(iSummary->processed->toJsonValue());
324  l1JsnData[DataPoint::DATA].append(iSummary->L1AlgoAccept ->toJsonValue());
325  l1JsnData[DataPoint::DATA].append(iSummary->L1AlgoAcceptPhysics ->toJsonValue());
327  l1JsnData[DataPoint::DATA].append(iSummary->L1AlgoAcceptRandom ->toJsonValue());
328  l1JsnData[DataPoint::DATA].append(iSummary->L1Global ->toJsonValue());
329 
330  l1JsnData[DataPoint::DATA].append(iSummary->prescaleIndex);
331 
332  std::string && result = writer.write(l1JsnData);
333 
334  std::stringstream ssL1JsnData;
335  ssL1JsnData << "run" << std::setfill('0') << std::setw(6) << iRun << "_ls" << std::setfill('0') << std::setw(4) << iLs;
336  ssL1JsnData << "_streamL1Rates_pid" << std::setfill('0') << std::setw(5) << getpid() << ".jsndata";
337 
338  if (iSummary->processed->value().at(0)!=0) {
339  std::ofstream outL1JsnData( monPath + "/" + ssL1JsnData.str() );
340  outL1JsnData<<result;
341  outL1JsnData.close();
342  }
343 
344  //L1 jsn entries
345  StringJ l1JsnFilelist;
346  IntJ l1JsnFilesize = 0;
347  unsigned int l1JsnFileAdler32 = 1;
348  if (iSummary->processed->value().at(0)!=0) {
349  l1JsnFilelist.update(ssL1JsnData.str());
350  l1JsnFilesize = result.size();
351  l1JsnFileAdler32 = cms::Adler32(result.c_str(),result.size());
352  }
353  StringJ l1JsnInputFiles;
354  l1JsnInputFiles.update("");
355 
356  //Create special DAQ JSON file for L1 and HLT rates pseudo-streams
357  //Only three variables are different between the files:
358  //the file list, the file size and the Adler32 value
359  IntJ daqJsnProcessed = iSummary->processed->value().at(0);
360  IntJ daqJsnAccepted = daqJsnProcessed;
361  IntJ daqJsnErrorEvents = 0;
362  IntJ daqJsnRetCodeMask = 0;
363  IntJ daqJsnHLTErrorEvents = 0;
364 
365  //write out L1 metadata jsn
366  Json::Value l1DaqJsn;
367  l1DaqJsn[DataPoint::SOURCE] = sourceHost;
368  l1DaqJsn[DataPoint::DEFINITION] = sOutDef.str();
369 
370  l1DaqJsn[DataPoint::DATA].append((unsigned int)daqJsnProcessed.value());
371  l1DaqJsn[DataPoint::DATA].append((unsigned int)daqJsnAccepted.value());
372  l1DaqJsn[DataPoint::DATA].append((unsigned int)daqJsnErrorEvents.value());
373  l1DaqJsn[DataPoint::DATA].append((unsigned int)daqJsnRetCodeMask.value());
374  l1DaqJsn[DataPoint::DATA].append(l1JsnFilelist.value());
375  l1DaqJsn[DataPoint::DATA].append((unsigned int)l1JsnFilesize.value());
376  l1DaqJsn[DataPoint::DATA].append(l1JsnInputFiles.value());
377  l1DaqJsn[DataPoint::DATA].append(l1JsnFileAdler32);
378  l1DaqJsn[DataPoint::DATA].append(iSummary->streamL1Destination);
379  l1DaqJsn[DataPoint::DATA].append((unsigned int)daqJsnHLTErrorEvents.value());
380 
381  result = writer.write(l1DaqJsn);
382 
383  std::stringstream ssL1DaqJsn;
384  ssL1DaqJsn << "run" << std::setfill('0') << std::setw(6) << iRun << "_ls" << std::setfill('0') << std::setw(4) << iLs;
385  ssL1DaqJsn << "_streamL1Rates_pid" << std::setfill('0') << std::setw(5) << getpid() << ".jsn";
386 
387  std::ofstream outL1DaqJsn( monPath + ssL1DaqJsn.str() );
388  outL1DaqJsn<<result;
389  outL1DaqJsn.close();
390  }
391 
392  //Delete the individual HistoJ pointers
393  delete iSummary->processed;
394 
395  delete iSummary->L1AlgoAccept;
396  delete iSummary->L1AlgoAcceptPhysics;
397  delete iSummary->L1AlgoAcceptCalibration;
398  delete iSummary->L1AlgoAcceptRandom;
399  delete iSummary->L1Global;
400 
401  //Note: Do not delete the iSummary pointer. The framework does something with it later on
402  // and deleting it results in a segmentation fault.
403 
404  //Reninitalize HistoJ pointers to nullptr
405  iSummary->processed = nullptr;
406 
407  iSummary->L1AlgoAccept = nullptr;
408  iSummary->L1AlgoAcceptPhysics = nullptr;
409  iSummary->L1AlgoAcceptCalibration = nullptr;
410  iSummary->L1AlgoAcceptRandom = nullptr;
411  iSummary->L1Global = nullptr;
412 
413 }//End globalEndLuminosityBlockSummary function
414 
415 void
417 
418  std::ofstream outfile( path );
419  outfile << "{" << std::endl;
420  outfile << " \"data\" : [" << std::endl;
421  outfile << " {" ;
422  outfile << " \"name\" : \"Processed\"," ;
423  outfile << " \"type\" : \"integer\"," ;
424  outfile << " \"operation\" : \"histo\"}," << std::endl;
425 
426  outfile << " {" ;
427  outfile << " \"name\" : \"L1-AlgoAccepted\"," ;
428  outfile << " \"type\" : \"integer\"," ;
429  outfile << " \"operation\" : \"histo\"}," << std::endl;
430 
431  outfile << " {" ;
432  outfile << " \"name\" : \"L1-AlgoAccepted-Physics\"," ;
433  outfile << " \"type\" : \"integer\"," ;
434  outfile << " \"operation\" : \"histo\"}," << std::endl;
435 
436  outfile << " {" ;
437  outfile << " \"name\" : \"L1-AlgoAccepted-Calibration\"," ;
438  outfile << " \"type\" : \"integer\"," ;
439  outfile << " \"operation\" : \"histo\"}," << std::endl;
440 
441  outfile << " {" ;
442  outfile << " \"name\" : \"L1-AlgoAccepted-Random\"," ;
443  outfile << " \"type\" : \"integer\"," ;
444  outfile << " \"operation\" : \"histo\"}," << std::endl;
445 
446  outfile << " {" ;
447  outfile << " \"name\" : \"L1-Global\"," ;
448  outfile << " \"type\" : \"integer\"," ;
449  outfile << " \"operation\" : \"histo\"}," << std::endl;
450 
451  outfile << " {" ;
452  outfile << " \"name\" : \"Prescale-Index\"," ;
453  outfile << " \"type\" : \"integer\"," ;
454  outfile << " \"operation\" : \"sample\"}" << std::endl;
455 
456  outfile << " ]" << std::endl;
457  outfile << "}" << std::endl;
458 
459  outfile.close();
460 }//End writeL1DefJson function
461 
#define LogDebug(id)
int i
Definition: DBlmapReader.cc:9
RunNumber_t run() const
Definition: RunBase.h:40
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventIDconst &, edm::Timestampconst & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
void endLuminosityBlockSummary(edm::LuminosityBlock const &, edm::EventSetup const &, l1Json::lumiVars *) const
bool shouldWriteFiles(unsigned int lumi, unsigned int *proc=0)
Value & append(const Value &value)
Append value to array at the end.
void update(std::string const &newStr)
std::vector< unsigned int > L1AlgoAcceptRandom_
Represents a JSON value.
Definition: value.h:111
std::vector< unsigned int > L1AlgoAccept_
void writeL1DefJson(std::string path)
jsoncollector::HistoJ< unsigned int > * L1AlgoAcceptCalibration
virtual Json::Value toJsonValue() const
static const unsigned int maxPhysicsTriggers
Definition: GlobalAlgBlk.h:52
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
LuminosityBlockNumber_t luminosityBlock() const
std::vector< T > & value()
tuple result
Definition: mps_fire.py:95
std::vector< std::string > L1AlgoNames_
LuminosityBlockNumber_t luminosityBlock() const
int iEvent
Definition: GenABIO.cc:230
std::vector< unsigned int > L1Global_
void analyze(edm::Event const &, edm::EventSetup const &)
edm::EDGetTokenT< GlobalAlgBlkBxCollection > level1ResultsToken_
std::vector< std::string > L1GlobalType_
std::vector< unsigned int > L1AlgoAcceptPhysics_
std::vector< unsigned int > L1AlgoAcceptCalibration_
void beginRun(edm::Run const &, edm::EventSetup const &)
RunNumber_t run() const
jsoncollector::HistoJ< unsigned int > * L1Global
void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
virtual std::string write(const Value &root)
Serialize a Value in JSON format.
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
EventAuxiliary const & eventAuxiliary() const
Definition: Event.h:77
jsoncollector::HistoJ< unsigned int > * L1AlgoAccept
jsoncollector::HistoJ< unsigned int > * processed
const T & get() const
Definition: EventSetup.h:56
T const * product() const
Definition: ESHandle.h:86
void add(std::string const &label, ParameterSetDescription const &psetDescription)
const std::map< std::string, L1TUtmAlgorithm > & getAlgorithmMap() const
jsoncollector::HistoJ< unsigned int > * L1AlgoAcceptRandom
L1TriggerJSONMonitoring(const edm::ParameterSet &)
edm::EventAuxiliary::ExperimentType experimentType() const
Definition: EventBase.h:64
static void globalEndLuminosityBlockSummary(edm::LuminosityBlock const &, edm::EventSetup const &, LuminosityBlockContext const *, l1Json::lumiVars *)
jsoncollector::HistoJ< unsigned int > * L1AlgoAcceptPhysics
static std::shared_ptr< l1Json::lumiVars > globalBeginLuminosityBlockSummary(edm::LuminosityBlock const &, edm::EventSetup const &, LuminosityBlockContext const *)
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:65
Definition: Run.h:43
string SOURCE
Definition: runonSM.py:23
array value (ordered list)
Definition: value.h:31