CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TriggerJSONMonitoring.cc
Go to the documentation of this file.
1 
12 
15 
19 
20 #include <fstream>
21 
23 {
24  if (ps.exists("triggerResults")) triggerResults_ = ps.getParameter<edm::InputTag> ("triggerResults");
25  else triggerResults_ = edm::InputTag("TriggerResults","","HLT");
26 
27  triggerResultsToken_ = consumes<edm::TriggerResults>(triggerResults_);
28 }
29 
31 {
32 }
33 
34 void
37  desc.add<edm::InputTag>("triggerResults",edm::InputTag("TriggerResults","","HLT"));
38  descriptions.add("triggerJSONMonitoring", 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  iEvent.getByToken(triggerResultsToken_, HLTR);
53  if (!HLTR.isValid()) {
54  LogDebug("TriggerJSONMonitoring") << "HLT TriggerResults with label ["+triggerResults_.encode()+"] not found!" << std::endl;
55  return;
56  }
57 
58  //Decision for each HLT path
59  const unsigned int n(hltNames_.size());
60  for (unsigned int i=0; i<n; i++) {
61  if (HLTR->wasrun(i)) hltWasRun_[i]++;
62  if (HLTR->accept(i)) hltAccept_[i]++;
63  if (HLTR->wasrun(i) && !HLTR->accept(i)) hltReject_[i]++;
64  if (HLTR->error(i)) hltErrors_[i]++;
65  //Count L1 seeds and Prescales
66  const int index(static_cast<int>(HLTR->index(i)));
67  if (HLTR->accept(i)) {
68  if (index >= posL1s_[i]) hltL1s_[i]++;
69  if (index >= posPre_[i]) hltPre_[i]++;
70  } else {
71  if (index > posL1s_[i]) hltL1s_[i]++;
72  if (index > posPre_[i]) hltPre_[i]++;
73  }
74  }
75 
76  //Decision for each HLT dataset
77  std::vector<bool> acceptedByDS(hltIndex_.size(), false);
78  for (unsigned int ds=0; ds < hltIndex_.size(); ds++) { // ds = index of dataset
79  for (unsigned int p=0; p<hltIndex_[ds].size(); p++) { // p = index of path with dataset ds
80  if (acceptedByDS[ds]>0 || HLTR->accept(hltIndex_[ds][p]) ) {
81  acceptedByDS[ds] = true;
82  }
83  }
84  if (acceptedByDS[ds]) hltDatasets_[ds]++;
85  }
86 }//End analyze function
87 
88 void
90 
91  //Update trigger and dataset names
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  //Resize per-dataset counter
110  hltDatasets_.resize(d);
111  //Resize htlIndex
112  hltIndex_.resize(d);
113  //Set-up hltIndex
114  for (unsigned int ds = 0; ds < d; ds++) {
115  unsigned int size = datasetContents_[ds].size();
116  hltIndex_[ds].reserve(size);
117  for (unsigned int p = 0; p < size; p++) {
118  unsigned int i = hltConfig_.triggerIndex(datasetContents_[ds][p]);
119  if (i<n) {
120  hltIndex_[ds].push_back(i);
121  }
122  }
123  }
124  //Find the positions of seeding and prescaler modules
125  posL1s_.resize(n);
126  posPre_.resize(n);
127  for (unsigned int i = 0; i < n; ++i) {
128  posL1s_[i] = -1;
129  posPre_[i] = -1;
130  const std::vector<std::string> & moduleLabels(hltConfig_.moduleLabels(i));
131  for (unsigned int j = 0; j < moduleLabels.size(); ++j) {
132  const std::string & label = hltConfig_.moduleType(moduleLabels[j]);
133  if (label == "HLTLevel1GTSeed")
134  posL1s_[i] = j;
135  else if (label == "HLTPrescaler")
136  posPre_[i] = j;
137  }
138  }
139  }
140  resetLumi();
141 }//End resetRun function
142 
143 void
145  //Reset total number of events
146  processed_ = 0;
147 
148  //Reset per-path counters
149  for (unsigned int i = 0; i < hltWasRun_.size(); i++) {
150  hltWasRun_[i] = 0;
151  hltL1s_[i] = 0;
152  hltPre_[i] = 0;
153  hltAccept_[i] = 0;
154  hltReject_[i] = 0;
155  hltErrors_[i] = 0;
156  }
157  //Reset per-dataset counter
158  for (unsigned int i = 0; i < hltDatasets_.size(); i++) {
159  hltDatasets_[i] = 0;
160  }
161 }//End resetLumi function
162 
163 void
165 {
166  //Get the run directory from the EvFDaqDirector
168  else baseRunDir_ = ".";
169 
170  //Create mon directory if it doesn't exist
171  boost::filesystem::path monPath = baseRunDir_ + "/mon";
172  if (not boost::filesystem::is_directory(monPath)) boost::filesystem::create_directories(monPath);
173 
174  //Initialize hltConfig_
175  bool changed = true;
176  if (hltConfig_.init(iRun, iSetup, triggerResults_.process(), changed)) resetRun(changed);
177  else{
178  LogDebug("TriggerJSONMonitoring") << "HLTConfigProvider initialization failed!" << std::endl;
179  return;
180  }
181 
182  unsigned int nRun = iRun.run();
183 
184  //Create definition file for Rates
185  std::stringstream sjsdr;
186  sjsdr << baseRunDir_ + "/mon/run" << nRun << "_ls0000";
187  sjsdr << "_HLTRates_pid" << std::setfill('0') << std::setw(5) << getpid() << ".jsd";
188  jsonRateDefFile_ = sjsdr.str();
189 
191 
192  //Create definition file for Legend
193  std::stringstream sjsdl;
194  sjsdl << baseRunDir_ << "/mon/run" << nRun << "_ls0000";
195  sjsdl << "_HLTRatesLegend_pid" << std::setfill('0') << std::setw(5) << getpid() << ".jsd";
196  jsonLegendDefFile_ = sjsdl.str();
197 
199 
200 }//End beginRun function
201 
203 
204 std::shared_ptr<hltJson::lumiVars>
206 {
207  std::shared_ptr<hltJson::lumiVars> iSummary(new hltJson::lumiVars);
208 
209  unsigned int MAXPATHS = 500;
210 
211  iSummary->processed = new HistoJ<unsigned int>(1, 1);
212 
213  iSummary->hltWasRun = new HistoJ<unsigned int>(1, MAXPATHS);
214  iSummary->hltL1s = new HistoJ<unsigned int>(1, MAXPATHS);
215  iSummary->hltPre = new HistoJ<unsigned int>(1, MAXPATHS);
216  iSummary->hltAccept = new HistoJ<unsigned int>(1, MAXPATHS);
217  iSummary->hltReject = new HistoJ<unsigned int>(1, MAXPATHS);
218  iSummary->hltErrors = new HistoJ<unsigned int>(1, MAXPATHS);
219 
220  iSummary->hltDatasets = new HistoJ<unsigned int>(1, MAXPATHS);
221 
222  iSummary->hltNames = new HistoJ<std::string>(1, MAXPATHS);
223  iSummary->datasetNames = new HistoJ<std::string>(1, MAXPATHS);
224 
225  return iSummary;
226 }//End globalBeginLuminosityBlockSummary function
227 
228 void
230 
231  //Whichever stream gets there first does the initialiazation
232  if (iSummary->hltNames->value().size() == 0){
233  iSummary->processed->update(processed_);
234 
235  for (unsigned int ui = 0; ui < hltNames_.size(); ui++){
236  iSummary->hltWasRun->update(hltWasRun_.at(ui));
237  iSummary->hltL1s ->update(hltL1s_ .at(ui));
238  iSummary->hltPre ->update(hltPre_ .at(ui));
239  iSummary->hltAccept->update(hltAccept_.at(ui));
240  iSummary->hltReject->update(hltReject_.at(ui));
241  iSummary->hltErrors->update(hltErrors_.at(ui));
242 
243  iSummary->hltNames->update(hltNames_.at(ui));
244  }
245 
246  for (unsigned int ui = 0; ui < datasetNames_.size(); ui++){
247  iSummary->hltDatasets->update(hltDatasets_.at(ui));
248 
249  iSummary->datasetNames->update(datasetNames_.at(ui));
250  }
251  iSummary->jsonRateDefFile = jsonRateDefFile_;
253  iSummary->baseRunDir = baseRunDir_;
254  }
255  else{
256  iSummary->processed->value().at(0) += processed_;
257 
258  for (unsigned int ui = 0; ui < hltNames_.size(); ui++){
259  iSummary->hltWasRun->value().at(ui) += hltWasRun_.at(ui);
260  iSummary->hltL1s ->value().at(ui) += hltL1s_ .at(ui);
261  iSummary->hltPre ->value().at(ui) += hltPre_ .at(ui);
262  iSummary->hltAccept->value().at(ui) += hltAccept_.at(ui);
263  iSummary->hltReject->value().at(ui) += hltReject_.at(ui);
264  iSummary->hltErrors->value().at(ui) += hltErrors_.at(ui);
265  }
266  for (unsigned int ui = 0; ui < datasetNames_.size(); ui++){
267  iSummary->hltDatasets->value().at(ui) += hltDatasets_.at(ui);
268  }
269  }
270 }//End endLuminosityBlockSummary function
271 
272 
273 void
275 {
276  Json::StyledWriter writer;
277 
278  char hostname[33];
279  gethostname(hostname,32);
280  std::string sourceHost(hostname);
281 
282  //Get the mon directory
283  std::string monPath = iSummary->baseRunDir + "/mon";
284 
285  unsigned int iLs = iLumi.luminosityBlock();
286  unsigned int iRun = iLumi.run();
287 
288  Json::Value hltRates;
289  hltRates[DataPoint::SOURCE] = sourceHost;
290  hltRates[DataPoint::DEFINITION] = iSummary->jsonRateDefFile;
291 
292  hltRates[DataPoint::DATA].append(iSummary->processed->toString());
293  hltRates[DataPoint::DATA].append(iSummary->hltWasRun->toString());
294  hltRates[DataPoint::DATA].append(iSummary->hltL1s ->toString());
295  hltRates[DataPoint::DATA].append(iSummary->hltPre ->toString());
296  hltRates[DataPoint::DATA].append(iSummary->hltAccept->toString());
297  hltRates[DataPoint::DATA].append(iSummary->hltReject->toString());
298  hltRates[DataPoint::DATA].append(iSummary->hltErrors->toString());
299 
300  hltRates[DataPoint::DATA].append(iSummary->hltDatasets->toString());
301 
302  std::string && result = writer.write(hltRates);
303 
304  std::stringstream sjsnr;
305  sjsnr << monPath << "/run" << iRun << "_ls" << std::setfill('0') << std::setw(4) << iLs;
306  sjsnr << "_HLTRates_pid" << std::setfill('0') << std::setw(5) << getpid() << ".jsn";
307 
308  std::ofstream outJsnFileR( sjsnr.str() );
309  outJsnFileR<<result;
310  outJsnFileR.close();
311 
312  Json::Value hltNames;
313  hltNames[DataPoint::SOURCE] = sourceHost;
314  hltNames[DataPoint::DEFINITION] = iSummary->jsonLegendDefFile;
315 
316  hltNames[DataPoint::DATA].append(iSummary->hltNames->toString());
317  hltNames[DataPoint::DATA].append(iSummary->datasetNames->toString());
318 
319  result = writer.write(hltNames);
320 
321  std::stringstream sjsnl;
322  sjsnl << monPath << "/run" << iRun << "_ls" << std::setfill('0') << std::setw(4) << iLs;
323  sjsnl << "_HLTRatesLegend_pid" << std::setfill('0') << std::setw(5) << getpid() << ".jsn";
324 
325  std::ofstream outJsnFileL( sjsnl.str() );
326  outJsnFileL<<result;
327  outJsnFileL.close();
328 
329  //Delete the individual HistoJ pointers
330  delete iSummary->processed;
331 
332  delete iSummary->hltWasRun;
333  delete iSummary->hltL1s ;
334  delete iSummary->hltPre ;
335  delete iSummary->hltAccept;
336  delete iSummary->hltReject;
337  delete iSummary->hltErrors;
338 
339  delete iSummary->hltDatasets;
340 
341  delete iSummary->hltNames;
342  delete iSummary->datasetNames;
343 
344  //Note: Do not delete the iSummary pointer. The framework does something with it later on
345  // and deleting it results in a segmentation fault.
346 
347 }//End globalEndLuminosityBlockSummary function
348 
349 
350 void
352 
353  std::ofstream outfile( path );
354  outfile << "{" << std::endl;
355  outfile << " \"data\" : [" << std::endl;
356  outfile << " {" ;
357  outfile << " \"name\" : \"Processed\"," ; //***
358  outfile << " type = \"integer\"," ;
359  outfile << " \"operation\" : \"sum\"}," << std::endl;
360 
361  outfile << " {" ;
362  outfile << " \"name\" : \"Path-WasRun\"," ;
363  outfile << " type = \"integer\"," ;
364  outfile << " \"operation\" : \"sum\"}," << std::endl;
365 
366  outfile << " {" ;
367  outfile << " \"name\" : \"Path-AfterL1Seed\"," ;
368  outfile << " type = \"integer\"," ;
369  outfile << " \"operation\" : \"sum\"}," << std::endl;
370 
371  outfile << " {" ;
372  outfile << " \"name\" : \"Path-AfterPrescale\"," ;
373  outfile << " type = \"integer\"," ;
374  outfile << " \"operation\" : \"sum\"}," << std::endl;
375 
376  outfile << " {" ;
377  outfile << " \"name\" : \"Path-Accepted\"," ;
378  outfile << " type = \"integer\"," ;
379  outfile << " \"operation\" : \"sum\"}," << std::endl;
380 
381  outfile << " {" ;
382  outfile << " \"name\" : \"Path-Rejected\"," ;
383  outfile << " type = \"integer\"," ;
384  outfile << " \"operation\" : \"sum\"}," << std::endl;
385 
386  outfile << " {" ;
387  outfile << " \"name\" : \"Path-Errors\"," ;
388  outfile << " type = \"integer\"," ;
389  outfile << " \"operation\" : \"sum\"}," << std::endl;
390 
391  outfile << " {" ;
392  outfile << " \"name\" : \"Dataset-Accepted\"," ;
393  outfile << " type = \"integer\"," ;
394  outfile << " \"operation\" : \"sum\"}" << std::endl;
395 
396  outfile << " ]" << std::endl;
397  outfile << "}" << std::endl;
398 
399  outfile.close();
400 }//End writeDefJson function
401 
402 void
404 
405  std::ofstream legfile(path);
406  legfile << "{" << std::endl;
407  legfile << " \"data\" : [" << std::endl;
408  legfile << " {" ;
409  legfile << " \"name\" : \"Path-Names\"," ;
410  legfile << " type = \"string\"," ;
411  legfile << " \"operation\" : \"cat\"}," << std::endl;
412 
413  legfile << " {" ;
414  legfile << " \"name\" : \"Dataset-Names\"," ;
415  legfile << " type = \"string\"," ;
416  legfile << " \"operation\" : \"cat\"}" << std::endl;
417  legfile << " ]" << std::endl;
418  legfile << "}" << std::endl;
419  legfile.close();
420 }//End writeDefLegJson function
#define LogDebug(id)
HistoJ< std::string > * datasetNames
TriggerJSONMonitoring(const edm::ParameterSet &)
void resetRun(bool changed)
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const std::string moduleType(const std::string &module) const
C++ class name of module.
RunNumber_t run() const
Definition: RunBase.h:42
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
HistoJ< unsigned int > * hltReject
std::vector< unsigned int > hltDatasets_
static std::shared_ptr< hltJson::lumiVars > globalBeginLuminosityBlockSummary(edm::LuminosityBlock const &, edm::EventSetup const &, LuminosityBlockContext const *)
const std::vector< std::string > & triggerNames() const
names of trigger paths
bool exists(std::string const &parameterName) const
checks if a parameter exists
HistoJ< unsigned int > * hltErrors
Value & append(const Value &value)
Append value to array at the end.
std::vector< unsigned int > hltErrors_
static void globalEndLuminosityBlockSummary(edm::LuminosityBlock const &, edm::EventSetup const &, LuminosityBlockContext const *, hltJson::lumiVars *)
void beginRun(edm::Run const &, edm::EventSetup const &)
Represents a JSON value.
Definition: value.h:111
HistoJ< unsigned int > * processed
std::vector< unsigned int > hltL1s_
std::string encode() const
Definition: InputTag.cc:164
std::vector< std::vector< unsigned int > > hltIndex_
std::vector< T > & value()
HistoJ< unsigned int > * hltAccept
std::vector< unsigned int > hltAccept_
LuminosityBlockNumber_t luminosityBlock() const
unsigned int triggerIndex(const std::string &triggerName) const
slot position of trigger path in trigger table (0 to size-1)
int iEvent
Definition: GenABIO.cc:230
tuple path
else: Piece not in the list, fine.
void endLuminosityBlockSummary(edm::LuminosityBlock const &, edm::EventSetup const &, hltJson::lumiVars *) const
std::vector< std::string > hltNames_
HLTConfigProvider hltConfig_
tuple result
Definition: query.py:137
std::vector< std::string > datasetNames_
int j
Definition: DBlmapReader.cc:9
RunNumber_t run() const
std::vector< unsigned int > hltReject_
HistoJ< unsigned int > * hltPre
ParameterDescriptionBase * add(U const &iLabel, T const &value)
virtual std::string write(const Value &root)
Serialize a Value in JSON format.
std::vector< int > posL1s_
edm::EDGetTokenT< edm::TriggerResults > triggerResultsToken_
const std::vector< std::string > & moduleLabels(unsigned int trigger) const
label(s) of module(s) on a trigger path
HistoJ< unsigned int > * hltDatasets
virtual std::string toString() const
void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &)
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HistoJ< std::string > * hltNames
void writeDefJson(std::string path)
std::string const & process() const
Definition: InputTag.h:46
const std::vector< std::vector< std::string > > & datasetContents() const
names of trigger paths for all datasets
std::vector< unsigned int > hltWasRun_
std::vector< std::vector< std::string > > datasetContents_
std::vector< int > posPre_
HistoJ< unsigned int > * hltL1s
void analyze(edm::Event const &, edm::EventSetup const &)
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:65
HistoJ< unsigned int > * hltWasRun
tuple size
Write out results.
void writeDefLegJson(std::string path)
Definition: Run.h:41
const std::vector< std::string > & datasetNames() const
std::vector< unsigned int > hltPre_
string SOURCE
Definition: runonSM.py:23
list at
Definition: asciidump.py:428