CMS 3D CMS Logo

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