CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HLTriggerJSONMonitoring.cc
Go to the documentation of this file.
1 
11 
14 
18 
20 
21 #include <fstream>
22 using namespace jsoncollector;
23 
25  triggerResults_(ps.getParameter<edm::InputTag>("triggerResults")),
26  triggerResultsToken_(consumes<edm::TriggerResults>(triggerResults_))
27 {
28 
29 
30 }
31 
33 {
34 }
35 
36 void
39  desc.add<edm::InputTag>("triggerResults",edm::InputTag("TriggerResults","","HLT"));
40  descriptions.add("HLTriggerJSONMonitoring", desc);
41 }
42 
43 void
45 {
46 
47  using namespace std;
48  using namespace edm;
49 
50  processed_++;
51 
52  //Get hold of TriggerResults
54  if (not iEvent.getByToken(triggerResultsToken_, HLTR) or not HLTR.isValid()){
55  LogDebug("HLTriggerJSONMonitoring") << "HLT TriggerResults with label ["+triggerResults_.encode()+"] not found!" << std::endl;
56  return;
57  }
58 
59  //Decision for each HLT path
60  const unsigned int n(hltNames_.size());
61  for (unsigned int i=0; i<n; i++) {
62  if (HLTR->wasrun(i)) hltWasRun_[i]++;
63  if (HLTR->accept(i)) hltAccept_[i]++;
64  if (HLTR->wasrun(i) && !HLTR->accept(i)) hltReject_[i]++;
65  if (HLTR->error(i)) hltErrors_[i]++;
66  //Count L1 seeds and Prescales
67  const int index(static_cast<int>(HLTR->index(i)));
68  if (HLTR->accept(i)) {
69  if (index >= posL1s_[i]) hltL1s_[i]++;
70  if (index >= posPre_[i]) hltPre_[i]++;
71  } else {
72  if (index > posL1s_[i]) hltL1s_[i]++;
73  if (index > posPre_[i]) hltPre_[i]++;
74  }
75  }
76 
77  //Decision for each HLT dataset
78  std::vector<bool> acceptedByDS(hltIndex_.size(), false);
79  for (unsigned int ds=0; ds < hltIndex_.size(); ds++) { // ds = index of dataset
80  for (unsigned int p=0; p<hltIndex_[ds].size(); p++) { // p = index of path with dataset ds
81  if (acceptedByDS[ds]>0 || HLTR->accept(hltIndex_[ds][p]) ) {
82  acceptedByDS[ds] = true;
83  }
84  }
85  if (acceptedByDS[ds]) hltDatasets_[ds]++;
86  }
87 
88 }//End analyze function
89 
90 void
92 
93  //Update trigger and dataset names, clear L1 names and counters
94  if (changed){
98  }
99 
100  const unsigned int n = hltNames_.size();
101  const unsigned int d = datasetNames_.size();
102 
103  if (changed) {
104  //Resize per-path counters
105  hltWasRun_.resize(n);
106  hltL1s_.resize(n);
107  hltPre_.resize(n);
108  hltAccept_.resize(n);
109  hltReject_.resize(n);
110  hltErrors_.resize(n);
111 
112  //Resize per-dataset counter
113  hltDatasets_.resize(d);
114  //Resize htlIndex
115  hltIndex_.resize(d);
116  //Set-up hltIndex
117  for (unsigned int ds = 0; ds < d; ds++) {
118  unsigned int size = datasetContents_[ds].size();
119  hltIndex_[ds].reserve(size);
120  for (unsigned int p = 0; p < size; p++) {
121  unsigned int i = hltConfig_.triggerIndex(datasetContents_[ds][p]);
122  if (i<n) {
123  hltIndex_[ds].push_back(i);
124  }
125  }
126  }
127  //Find the positions of seeding and prescaler modules
128  posL1s_.resize(n);
129  posPre_.resize(n);
130  for (unsigned int i = 0; i < n; ++i) {
131  posL1s_[i] = -1;
132  posPre_[i] = -1;
133  const std::vector<std::string> & moduleLabels(hltConfig_.moduleLabels(i));
134  for (unsigned int j = 0; j < moduleLabels.size(); ++j) {
135  const std::string & label = hltConfig_.moduleType(moduleLabels[j]);
136  if (label == "HLTL1TSeed")
137  posL1s_[i] = j;
138  else if (label == "HLTPrescaler")
139  posPre_[i] = j;
140  }
141  }
142  }
143  resetLumi();
144 }//End resetRun function
145 
146 void
148  //Reset total number of events
149  processed_ = 0;
150 
151  //Reset per-path counters
152  for (unsigned int i = 0; i < hltWasRun_.size(); i++) {
153  hltWasRun_[i] = 0;
154  hltL1s_[i] = 0;
155  hltPre_[i] = 0;
156  hltAccept_[i] = 0;
157  hltReject_[i] = 0;
158  hltErrors_[i] = 0;
159  }
160  //Reset per-dataset counter
161  for (unsigned int i = 0; i < hltDatasets_.size(); i++) {
162  hltDatasets_[i] = 0;
163  }
164 
165 }//End resetLumi function
166 
167 void
169 {
170  //Get the run directory from the EvFDaqDirector
172  else baseRunDir_ = ".";
173 
174  std::string monPath = baseRunDir_ + "/";
175 
176  //Initialize hltConfig_
177  bool changed = true;
178  if (hltConfig_.init(iRun, iSetup, triggerResults_.process(), changed)) resetRun(changed);
179  else{
180  LogDebug("HLTriggerJSONMonitoring") << "HLTConfigProvider initialization failed!" << std::endl;
181  return;
182  }
183 
184  //Write the once-per-run files if not already written
185  //Eventually must rewrite this with proper multithreading (i.e. globalBeginRun)
186  bool expected = false;
187  if( runCache()->wroteFiles.compare_exchange_strong(expected, true) ){
188  runCache()->wroteFiles = true;
189 
190  unsigned int nRun = iRun.run();
191 
192  //Create definition file for HLT Rates
193  std::stringstream ssHltJsd;
194  ssHltJsd << "run" << std::setfill('0') << std::setw(6) << nRun << "_ls0000";
195  ssHltJsd << "_streamHLTRates_pid" << std::setfill('0') << std::setw(5) << getpid() << ".jsd";
196  stHltJsd_ = ssHltJsd.str();
197 
199 
200  //Write ini files
201  //HLT
202  Json::Value hltIni;
203  Json::StyledWriter writer;
204 
205  Json::Value hltNamesVal(Json::arrayValue);
206  for (unsigned int ui = 0; ui < hltNames_.size(); ui++){
207  hltNamesVal.append(hltNames_.at(ui));
208  }
209 
210  Json::Value datasetNamesVal(Json::arrayValue);
211  for (unsigned int ui = 0; ui < datasetNames_.size(); ui++){
212  datasetNamesVal.append(datasetNames_.at(ui));
213  }
214 
215  hltIni["Path-Names"] = hltNamesVal;
216  hltIni["Dataset-Names"] = datasetNamesVal;
217 
218  std::string && result = writer.write(hltIni);
219 
220  std::stringstream ssHltIni;
221  ssHltIni << "run" << std::setfill('0') << std::setw(6) << nRun << "_ls0000_streamHLTRates_pid" << std::setfill('0') << std::setw(5) << getpid() << ".ini";
222 
223  std::ofstream outHltIni( monPath + ssHltIni.str() );
224  outHltIni<<result;
225  outHltIni.close();
226  }
227 
228 }//End beginRun function
229 
231 
232 std::shared_ptr<hltJson::lumiVars>
234 {
235  std::shared_ptr<hltJson::lumiVars> iSummary(new hltJson::lumiVars);
236 
237  unsigned int MAXPATHS = 1000;
238 
239  iSummary->processed = new HistoJ<unsigned int>(1, 1);
240 
241  iSummary->hltWasRun = new HistoJ<unsigned int>(1, MAXPATHS);
242  iSummary->hltL1s = new HistoJ<unsigned int>(1, MAXPATHS);
243  iSummary->hltPre = new HistoJ<unsigned int>(1, MAXPATHS);
244  iSummary->hltAccept = new HistoJ<unsigned int>(1, MAXPATHS);
245  iSummary->hltReject = new HistoJ<unsigned int>(1, MAXPATHS);
246  iSummary->hltErrors = new HistoJ<unsigned int>(1, MAXPATHS);
247 
248  iSummary->hltDatasets = new HistoJ<unsigned int>(1, MAXPATHS);
249 
250  iSummary->baseRunDir = "";
251  iSummary->stHltJsd = "";
252  iSummary->streamHLTDestination = "";
253  iSummary->streamHLTMergeType = "";
254 
255  return iSummary;
256 }//End globalBeginLuminosityBlockSummary function
257 
258 void
260 
261  //Whichever stream gets there first does the initialiazation
262  if (iSummary->hltWasRun->value().size() == 0){
263  iSummary->processed->update(processed_);
264 
265  for (unsigned int ui = 0; ui < hltWasRun_.size(); ui++){
266  iSummary->hltWasRun->update(hltWasRun_.at(ui));
267  iSummary->hltL1s ->update(hltL1s_ .at(ui));
268  iSummary->hltPre ->update(hltPre_ .at(ui));
269  iSummary->hltAccept->update(hltAccept_.at(ui));
270  iSummary->hltReject->update(hltReject_.at(ui));
271  iSummary->hltErrors->update(hltErrors_.at(ui));
272  }
273  for (unsigned int ui = 0; ui < hltDatasets_.size(); ui++){
274  iSummary->hltDatasets->update(hltDatasets_.at(ui));
275  }
276 
277  iSummary->stHltJsd = stHltJsd_;
278  iSummary->baseRunDir = baseRunDir_;
279 
280  iSummary->streamHLTDestination = runCache()->streamHLTDestination;
281  iSummary->streamHLTMergeType = runCache()->streamHLTMergeType;
282  }
283 
284  else{
285  iSummary->processed->value().at(0) += processed_;
286 
287  for (unsigned int ui = 0; ui < hltWasRun_.size(); ui++){
288  iSummary->hltWasRun->value().at(ui) += hltWasRun_.at(ui);
289  iSummary->hltL1s ->value().at(ui) += hltL1s_ .at(ui);
290  iSummary->hltPre ->value().at(ui) += hltPre_ .at(ui);
291  iSummary->hltAccept->value().at(ui) += hltAccept_.at(ui);
292  iSummary->hltReject->value().at(ui) += hltReject_.at(ui);
293  iSummary->hltErrors->value().at(ui) += hltErrors_.at(ui);
294  }
295  for (unsigned int ui = 0; ui < hltDatasets_.size(); ui++){
296  iSummary->hltDatasets->value().at(ui) += hltDatasets_.at(ui);
297  }
298  }
299 
300 }//End endLuminosityBlockSummary function
301 
302 
303 void
305 {
306 
307  unsigned int iLs = iLumi.luminosityBlock();
308  unsigned int iRun = iLumi.run();
309 
310  bool writeFiles=true;
311  if (edm::Service<evf::MicroStateService>().isAvailable()) {
313  if (fms) {
314  writeFiles = fms->shouldWriteFiles(iLumi.luminosityBlock());
315  }
316  }
317 
318  if (writeFiles) {
319  Json::StyledWriter writer;
320 
321  char hostname[33];
322  gethostname(hostname,32);
323  std::string sourceHost(hostname);
324 
325  //Get the output directory
326  std::string monPath = iSummary->baseRunDir + "/";
327 
328  std::stringstream sOutDef;
329  sOutDef << monPath << "output_" << getpid() << ".jsd";
330 
331  //Write the .jsndata files which contain the actual rates
332  //HLT .jsndata file
333  Json::Value hltJsnData;
334  hltJsnData[DataPoint::SOURCE] = sourceHost;
335  hltJsnData[DataPoint::DEFINITION] = iSummary->stHltJsd;
336 
337  hltJsnData[DataPoint::DATA].append(iSummary->processed->toJsonValue());
338  hltJsnData[DataPoint::DATA].append(iSummary->hltWasRun->toJsonValue());
339  hltJsnData[DataPoint::DATA].append(iSummary->hltL1s ->toJsonValue());
340  hltJsnData[DataPoint::DATA].append(iSummary->hltPre ->toJsonValue());
341  hltJsnData[DataPoint::DATA].append(iSummary->hltAccept->toJsonValue());
342  hltJsnData[DataPoint::DATA].append(iSummary->hltReject->toJsonValue());
343  hltJsnData[DataPoint::DATA].append(iSummary->hltErrors->toJsonValue());
344 
345  hltJsnData[DataPoint::DATA].append(iSummary->hltDatasets->toJsonValue());
346 
347  std::string && result = writer.write(hltJsnData);
348 
349  std::stringstream ssHltJsnData;
350  ssHltJsnData << "run" << std::setfill('0') << std::setw(6) << iRun << "_ls" << std::setfill('0') << std::setw(4) << iLs;
351  ssHltJsnData << "_streamHLTRates_pid" << std::setfill('0') << std::setw(5) << getpid() << ".jsndata";
352 
353  if (iSummary->processed->value().at(0)!=0) {
354  std::ofstream outHltJsnData( monPath + ssHltJsnData.str() );
355  outHltJsnData<<result;
356  outHltJsnData.close();
357  }
358 
359  //HLT jsn entries
360  StringJ hltJsnFilelist;
361  IntJ hltJsnFilesize = 0;
362  unsigned int hltJsnFileAdler32 = 1;
363  if (iSummary->processed->value().at(0)!=0) {
364  hltJsnFilelist.update(ssHltJsnData.str());
365  hltJsnFilesize = result.size();
366  hltJsnFileAdler32 = cms::Adler32(result.c_str(),result.size());
367  }
368  StringJ hltJsnInputFiles;
369  hltJsnInputFiles.update("");
370 
371  //Create special DAQ JSON file for L1 and HLT rates pseudo-streams
372  //Only three variables are different between the files:
373  //the file list, the file size and the Adler32 value
374  IntJ daqJsnProcessed = iSummary->processed->value().at(0);
375  IntJ daqJsnAccepted = daqJsnProcessed;
376  IntJ daqJsnErrorEvents = 0;
377  IntJ daqJsnRetCodeMask = 0;
378  IntJ daqJsnHLTErrorEvents = 0;
379 
380  //write out HLT metadata jsn
381  Json::Value hltDaqJsn;
382  hltDaqJsn[DataPoint::SOURCE] = sourceHost;
383  hltDaqJsn[DataPoint::DEFINITION] = sOutDef.str();
384 
385  hltDaqJsn[DataPoint::DATA].append((unsigned int)daqJsnProcessed.value());
386  hltDaqJsn[DataPoint::DATA].append((unsigned int)daqJsnAccepted.value());
387  hltDaqJsn[DataPoint::DATA].append((unsigned int)daqJsnErrorEvents.value());
388  hltDaqJsn[DataPoint::DATA].append((unsigned int)daqJsnRetCodeMask.value());
389  hltDaqJsn[DataPoint::DATA].append(hltJsnFilelist.value());
390  hltDaqJsn[DataPoint::DATA].append((unsigned int)hltJsnFilesize.value());
391  hltDaqJsn[DataPoint::DATA].append(hltJsnInputFiles.value());
392  hltDaqJsn[DataPoint::DATA].append(hltJsnFileAdler32);
393  hltDaqJsn[DataPoint::DATA].append(iSummary->streamHLTDestination);
394  hltDaqJsn[DataPoint::DATA].append(iSummary->streamHLTMergeType);
395  hltDaqJsn[DataPoint::DATA].append((unsigned int)daqJsnHLTErrorEvents.value());
396 
397  result = writer.write(hltDaqJsn);
398 
399  std::stringstream ssHltDaqJsn;
400  ssHltDaqJsn << "run" << std::setfill('0') << std::setw(6) << iRun << "_ls" << std::setfill('0') << std::setw(4) << iLs;
401  ssHltDaqJsn << "_streamHLTRates_pid" << std::setfill('0') << std::setw(5) << getpid() << ".jsn";
402 
403  std::ofstream outHltDaqJsn( monPath + ssHltDaqJsn.str() );
404  outHltDaqJsn<<result;
405  outHltDaqJsn.close();
406  }
407 
408  //Delete the individual HistoJ pointers
409  delete iSummary->processed;
410 
411  delete iSummary->hltWasRun;
412  delete iSummary->hltL1s ;
413  delete iSummary->hltPre ;
414  delete iSummary->hltAccept;
415  delete iSummary->hltReject;
416  delete iSummary->hltErrors;
417 
418  delete iSummary->hltDatasets;
419 
420  //Note: Do not delete the iSummary pointer. The framework does something with it later on
421  // and deleting it results in a segmentation fault.
422 
423  //Reninitalize HistoJ pointers to nullptr
424  iSummary->processed = nullptr;
425 
426  iSummary->hltWasRun = nullptr;
427  iSummary->hltL1s = nullptr;
428  iSummary->hltPre = nullptr;
429  iSummary->hltAccept = nullptr;
430  iSummary->hltReject = nullptr;
431  iSummary->hltErrors = nullptr;
432 
433  iSummary->hltDatasets = nullptr;
434 
435 }//End globalEndLuminosityBlockSummary function
436 
437 
438 void
440 
441  std::ofstream outfile( path );
442  outfile << "{" << std::endl;
443  outfile << " \"data\" : [" << std::endl;
444  outfile << " {" ;
445  outfile << " \"name\" : \"Processed\"," ; //***
446  outfile << " \"type\" : \"integer\"," ;
447  outfile << " \"operation\" : \"histo\"}," << std::endl;
448 
449  outfile << " {" ;
450  outfile << " \"name\" : \"Path-WasRun\"," ;
451  outfile << " \"type\" : \"integer\"," ;
452  outfile << " \"operation\" : \"histo\"}," << std::endl;
453 
454  outfile << " {" ;
455  outfile << " \"name\" : \"Path-AfterL1Seed\"," ;
456  outfile << " \"type\" : \"integer\"," ;
457  outfile << " \"operation\" : \"histo\"}," << std::endl;
458 
459  outfile << " {" ;
460  outfile << " \"name\" : \"Path-AfterPrescale\"," ;
461  outfile << " \"type\" : \"integer\"," ;
462  outfile << " \"operation\" : \"histo\"}," << std::endl;
463 
464  outfile << " {" ;
465  outfile << " \"name\" : \"Path-Accepted\"," ;
466  outfile << " \"type\" : \"integer\"," ;
467  outfile << " \"operation\" : \"histo\"}," << std::endl;
468 
469  outfile << " {" ;
470  outfile << " \"name\" : \"Path-Rejected\"," ;
471  outfile << " \"type\" : \"integer\"," ;
472  outfile << " \"operation\" : \"histo\"}," << std::endl;
473 
474  outfile << " {" ;
475  outfile << " \"name\" : \"Path-Errors\"," ;
476  outfile << " \"type\" : \"integer\"," ;
477  outfile << " \"operation\" : \"histo\"}," << std::endl;
478 
479  outfile << " {" ;
480  outfile << " \"name\" : \"Dataset-Accepted\"," ;
481  outfile << " \"type\" : \"integer\"," ;
482  outfile << " \"operation\" : \"histo\"}" << std::endl;
483 
484  outfile << " ]" << std::endl;
485  outfile << "}" << std::endl;
486 
487  outfile.close();
488 }//End writeHLTDefJson function
#define LogDebug(id)
std::vector< std::string > hltNames_
int i
Definition: DBlmapReader.cc:9
jsoncollector::HistoJ< unsigned int > * hltAccept
const std::string moduleType(const std::string &module) const
C++ class name of module.
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
void endLuminosityBlockSummary(edm::LuminosityBlock const &, edm::EventSetup const &, hltJson::lumiVars *) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
std::vector< unsigned int > hltDatasets_
static void globalEndLuminosityBlockSummary(edm::LuminosityBlock const &, edm::EventSetup const &, LuminosityBlockContext const *, hltJson::lumiVars *)
HLTriggerJSONMonitoring(const edm::ParameterSet &)
const std::vector< std::string > & triggerNames() const
names of trigger paths
std::vector< unsigned int > hltPre_
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 > hltAccept_
std::vector< unsigned int > hltReject_
Represents a JSON value.
Definition: value.h:111
jsoncollector::HistoJ< unsigned int > * hltWasRun
virtual Json::Value toJsonValue() const
std::string encode() const
Definition: InputTag.cc:164
static std::shared_ptr< hltJson::lumiVars > globalBeginLuminosityBlockSummary(edm::LuminosityBlock const &, edm::EventSetup const &, LuminosityBlockContext const *)
std::vector< T > & value()
tuple result
Definition: mps_fire.py:84
triggerResultsToken_(consumes< edm::TriggerResults >(edm::InputTag("TriggerResults")))
LuminosityBlockNumber_t luminosityBlock() const
unsigned int triggerIndex(const std::string &triggerName) const
slot position of trigger path in trigger table (0 to size-1)
tuple d
Definition: ztail.py:151
std::vector< std::vector< unsigned int > > hltIndex_
void beginRun(edm::Run const &, edm::EventSetup const &)
int iEvent
Definition: GenABIO.cc:230
jsoncollector::HistoJ< unsigned int > * hltDatasets
void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &)
int j
Definition: DBlmapReader.cc:9
RunNumber_t run() const
std::vector< unsigned int > hltL1s_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
virtual std::string write(const Value &root)
Serialize a Value in JSON format.
const std::vector< std::string > & moduleLabels(unsigned int trigger) const
label(s) of module(s) on a trigger path
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
jsoncollector::HistoJ< unsigned int > * hltPre
edm::EDGetTokenT< edm::TriggerResults > triggerResultsToken_
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
std::vector< std::string > datasetNames_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::vector< unsigned int > hltWasRun_
std::string const & process() const
Definition: InputTag.h:40
const std::vector< std::vector< std::string > > & datasetContents() const
names of trigger paths for all datasets
jsoncollector::HistoJ< unsigned int > * hltErrors
jsoncollector::HistoJ< unsigned int > * processed
void writeHLTDefJson(std::string path)
std::vector< unsigned int > hltErrors_
void analyze(edm::Event const &, edm::EventSetup const &)
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:65
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
tuple size
Write out results.
jsoncollector::HistoJ< unsigned int > * hltL1s
jsoncollector::HistoJ< unsigned int > * hltReject
Definition: Run.h:43
const std::vector< std::string > & datasetNames() const
string SOURCE
Definition: runonSM.py:23
array value (ordered list)
Definition: value.h:31
std::vector< std::vector< std::string > > datasetContents_