CMS 3D CMS Logo

DQMClientExample.cc
Go to the documentation of this file.
1 /*
2  * \file DQMClientExample.cc
3  * \author M. Zanetti - CERN
4  *
5  * Last Update:
6  *
7  */
8 
9 /* Description: Simple example showing how to access the histograms
10  * already defined and filled by the DQM producer(source)
11  * and how to access the quality test results.
12  */
13 
14 /* Jan 17, 2009: the code has been modified significantly
15  * to steer the client operations
16  * Author: D.Volyanskyy
17  */
18 
23 
24 #include <TF1.h>
25 #include <cmath>
26 #include <cstdio>
27 #include <sstream>
28 
29 using namespace edm;
30 using namespace std;
31 
32 //==================================================================//
33 //================= Constructor and Destructor =====================//
34 //==================================================================//
36  parameters_ = ps;
37  initialize();
38 }
39 
41 
42 //==================================================================//
43 //======================= Initialise ===============================//
44 //==================================================================//
47  counterEvt_ = 0;
48  counterLS_ = 0;
49  counterClientOperation = 0;
50 
53 
55  monitorName_ = parameters_.getUntrackedParameter<string>("monitorName", "YourSubsystemName");
56  cout << "DQMClientExample: Monitor name = " << monitorName_ << endl;
57  if (!monitorName_.empty())
58  monitorName_ = monitorName_ + "/";
59 
61  prescaleLS_ = parameters_.getUntrackedParameter<int>("prescaleLS", -1);
62  cout << "DQMClientExample: DQM lumi section prescale = " << prescaleLS_ << " lumi section(s)" << endl;
63  prescaleEvt_ = parameters_.getUntrackedParameter<int>("prescaleEvt", -1);
64  cout << "DQMClientExample: DQM event prescale = " << prescaleEvt_ << " events(s)" << endl;
65 
66  //-- QTestName that is going to bu run on clienHisto, as defined in XML file
67  QTestName_ = parameters_.getUntrackedParameter<string>("QTestName", "exampleQTest");
68  cout << "DQMClientExample: QTest name to be ran on clientHisto = " << QTestName_ << endl;
69  //-- define where to run the Client
70  clientOnEachEvent = parameters_.getUntrackedParameter<bool>("clientOnEachEvent", false);
71  if (clientOnEachEvent)
72  cout << "DQMClientExample: run Client on each event" << endl;
73  clientOnEndLumi = parameters_.getUntrackedParameter<bool>("clientOnEndLumi", true);
74  if (clientOnEndLumi)
75  cout << "DQMClientExample: run Client at the end of each lumi section" << endl;
76  clientOnEndRun = parameters_.getUntrackedParameter<bool>("clientOnEndRun", false);
77  if (clientOnEndRun)
78  cout << "DQMClientExample: run Client at the end of each run" << endl;
79  clientOnEndJob = parameters_.getUntrackedParameter<bool>("clientOnEndJob", false);
80  if (clientOnEndJob)
81  cout << "DQMClientExample: run Client at the end of the job" << endl;
82 }
83 //==================================================================//
84 //========================= beginJob ===============================//
85 //==================================================================//
89 
91  dbe_->setCurrentFolder(monitorName_ + "DQMclient");
92  clientHisto = dbe_->book1D("clientHisto", "Guassian fit results.", 2, 0, 1);
93 }
94 
95 //==================================================================//
96 //========================= beginRun ===============================//
97 //==================================================================//
98 void DQMClientExample::beginRun(const Run &r, const EventSetup &context) {}
99 
100 //==================================================================//
101 //==================== beginLuminosityBlock ========================//
102 //==================================================================//
104  // optionally reset histograms here
105  // clientHisto->Reset();
106 }
107 
108 //==================================================================//
109 //==================== analyse (takes each event) ==================//
110 //==================================================================//
111 void DQMClientExample::analyze(const Event &e, const EventSetup &context) {
112  counterEvt_++;
113  if (clientOnEachEvent) {
114  if (prescaleEvt_ > 0 && counterEvt_ % prescaleEvt_ == 0)
115  performClient();
116  }
117 }
118 
119 //==================================================================//
120 //========================= endLuminosityBlock =====================//
121 //==================================================================//
123  counterLS_++;
124  if (!clientOnEachEvent && clientOnEndLumi) {
125  if (prescaleLS_ > 0 && counterLS_ % prescaleLS_ == 0)
126  performClient();
127  }
128 }
129 //==================================================================//
130 //============================= endRun =============================//
131 //==================================================================//
132 void DQMClientExample::endRun(const Run &r, const EventSetup &context) {
133  if (clientOnEndRun)
134  performClient();
135 }
136 
137 //==================================================================//
138 //============================= endJob =============================//
139 //==================================================================//
141  std::cout << "DQMSourceClient::endJob()" << std::endl;
142  if (clientOnEndJob)
143  performClient();
144 }
145 
146 //==================================================================//
147 //======================= performClient ============================//
148 //==================================================================//
150  std::cout << "***** run Client operations as defined in: *****" << std::endl;
151  std::cout << "***** DQMClientExample::performClient ********" << std::endl;
152 
153  //----------------------------------------------------------------------------------------------
154  // example how to retrieve a ME created by the DQM producer (in
155  // Examples/src/DQMSourceExample.cc)
156  //---------------------------------------------------------------------------------------------
157  float mean = 0;
158  float rms = 0;
159 
161  string histoName = monitorName_ + "DQMsource/C1/histo2";
162  MonitorElement *meHisto = dbe_->get(histoName);
164  if (meHisto) {
166  if (TH1F *rootHisto = meHisto->getTH1F()) {
167  if (counterClientOperation < 1)
168  std::cout << "=== DQMClientExample::performClient => the histo is "
169  "found: entries="
170  << rootHisto->GetEntries() << "\n"
171  << " mean=" << rootHisto->GetMean() << " rms=" << rootHisto->GetRMS() << std::endl;
172 
174  TF1 *f1 = new TF1("f1", "gaus", 1, 3);
175  rootHisto->Fit(f1);
176  mean = f1->GetParameter(1);
177  rms = f1->GetParameter(2);
178  }
179  clientHisto->setBinContent(1, mean);
180  clientHisto->setBinContent(2, rms);
181  }
182 
183  else { // when it is not found !
184  clientHisto->setBinContent(1, -1);
185  clientHisto->setBinContent(2, -1);
186  if (counterClientOperation < 1)
187  edm::LogError("DQMClientExample") << "--- The following ME :" << histoName << " cannot be found\n"
188  << "--- on the DQM source side !\n";
189  }
190 
191  //----------------------------------------------------------------------------------------------
192  // example how to access the Quality test result for clientHisto
193  //---------------------------------------------------------------------------------------------
194 
195  // qtest to be ran on clientHisto is defined in Examples/test/QualityTests.xml
196  const QReport *theQReport = clientHisto->getQReport(QTestName_);
197  if (theQReport) {
198  if (counterClientOperation < 1)
199  edm::LogWarning("DQMClientExample") << "*** Summary of Quality Test for clientHisto: \n"
200  //<<"--- value ="<< theQReport->getQTresult()<<"\n"
201  << "--- status =" << theQReport->getStatus() << "\n"
202  << "--- message =" << theQReport->getMessage() << "\n";
203 
204  vector<dqm::me_util::Channel> badChannels = theQReport->getBadChannels();
205  for (vector<dqm::me_util::Channel>::iterator channel = badChannels.begin(); channel != badChannels.end();
206  channel++) {
207  if (counterClientOperation < 1)
208  edm::LogError("DQMClientExample")
209  << " Bad channels: " << (*channel).getBin() << " " << (*channel).getContents();
210  }
211  } else {
212  if (counterClientOperation < 1)
213  edm::LogError("DQMClientExample") << "--- No QReport is found for clientHisto!\n"
214  << "--- Please check your XML and config files -> QTestName ( " << QTestName_
215  << " )must be the same!\n";
216  }
217 
218  counterClientOperation++;
219 }
static AlgebraicMatrix initialize()
void endRun(const edm::Run &r, const edm::EventSetup &c) override
const std::vector< DQMChannel > & getBadChannels() const
Definition: QReport.h:33
TH1F * getTH1F() const
void beginRun(const edm::Run &r, const edm::EventSetup &c) override
~DQMClientExample() override
void endLuminosityBlock(const edm::LuminosityBlock &lumiSeg, const edm::EventSetup &c) override
void beginJob() override
const std::string & getMessage() const
get message attached to test
Definition: QReport.h:24
DQMStore * dbe_
void analyze(const edm::Event &e, const edm::EventSetup &c) override
void endJob() override
DQMClientExample(const edm::ParameterSet &ps)
int getStatus() const
get test status (see Core/interface/QTestStatus.h)
Definition: QReport.h:16
HLT enums.
void beginLuminosityBlock(const edm::LuminosityBlock &lumiSeg, const edm::EventSetup &context) override
Definition: Run.h:45