CMS 3D CMS Logo

DQMRivetClient.cc
Go to the documentation of this file.
1 /*
2  * Class:DQMRivetClient
3  *
4  *
5  *
6  * \author Junghwan Goh - SungKyunKwan University
7  */
8 
10 
15 
16 #include <TH1F.h>
17 #include <TClass.h>
18 #include <TString.h>
19 #include <TPRegexp.h>
20 
21 #include <cmath>
22 #include <boost/tokenizer.hpp>
23 
24 using namespace std;
25 using namespace edm;
26 
28 
30  typedef std::vector<edm::ParameterSet> VPSet;
31  typedef std::vector<std::string> vstring;
32  typedef boost::escaped_list_separator<char> elsc;
33 
34  elsc commonEscapes("\\", " \t", "\'");
35 
36  // Parse Normalization commands
37  vstring normCmds = pset.getUntrackedParameter<vstring>("normalizationToIntegral", vstring());
38  for (vstring::const_iterator normCmd = normCmds.begin(); normCmd != normCmds.end(); ++normCmd) {
39  if (normCmd->empty())
40  continue;
41  boost::tokenizer<elsc> tokens(*normCmd, commonEscapes);
42 
43  vector<string> args;
44  for (boost::tokenizer<elsc>::const_iterator iToken = tokens.begin(); iToken != tokens.end(); ++iToken) {
45  if (iToken->empty())
46  continue;
47  args.push_back(*iToken);
48  }
49 
50  if (args.empty() or args.size() > 2) {
51  LogInfo("DQMRivetClient") << "Wrong input to normCmds\n";
52  continue;
53  }
54 
56  opt.name = args[0];
57  opt.normHistName = args.size() == 2 ? args[1] : args[0];
58 
59  normOptions_.push_back(opt);
60  }
61 
62  VPSet normSets = pset.getUntrackedParameter<VPSet>("normalizationToIntegralSets", VPSet());
63  for (VPSet::const_iterator normSet = normSets.begin(); normSet != normSets.end(); ++normSet) {
65  opt.name = normSet->getUntrackedParameter<string>("name");
66  opt.normHistName = normSet->getUntrackedParameter<string>("normalizedTo", opt.name);
67 
68  normOptions_.push_back(opt);
69  }
70 
71  //normalize to lumi
72  vstring lumiCmds = pset.getUntrackedParameter<vstring>("normalizationToLumi", vstring());
73  for (vstring::const_iterator lumiCmd = lumiCmds.begin(); lumiCmd != lumiCmds.end(); ++lumiCmd) {
74  if (lumiCmd->empty())
75  continue;
76  boost::tokenizer<elsc> tokens(*lumiCmd, commonEscapes);
77 
78  vector<string> args;
79  for (boost::tokenizer<elsc>::const_iterator iToken = tokens.begin(); iToken != tokens.end(); ++iToken) {
80  if (iToken->empty())
81  continue;
82  args.push_back(*iToken);
83  }
84 
85  if (args.size() != 2) {
86  LogInfo("DQMRivetClient") << "Wrong input to lumiCmds\n";
87  continue;
88  }
89 
91  opt.name = args[0];
92  opt.normHistName = args[1];
93  opt.xsection = pset.getUntrackedParameter<double>("xsection", -1.);
94  //opt.xsection = atof(args[2].c_str());
95 
96  //std::cout << opt.name << " " << opt.normHistName << " " << opt.xsection << std::endl;
97  lumiOptions_.push_back(opt);
98  }
99 
100  //multiply by a number
101  vstring scaleCmds = pset.getUntrackedParameter<vstring>("scaleBy", vstring());
102  for (vstring::const_iterator scaleCmd = scaleCmds.begin(); scaleCmd != scaleCmds.end(); ++scaleCmd) {
103  if (scaleCmd->empty())
104  continue;
105  boost::tokenizer<elsc> tokens(*scaleCmd, commonEscapes);
106 
107  vector<string> args;
108  for (boost::tokenizer<elsc>::const_iterator iToken = tokens.begin(); iToken != tokens.end(); ++iToken) {
109  if (iToken->empty())
110  continue;
111  args.push_back(*iToken);
112  }
113 
114  if (args.empty() or args.size() > 2) {
115  LogInfo("DQMRivetClient") << "Wrong input to normCmds\n";
116  continue;
117  }
118 
120  opt.name = args[0];
121  opt.scale = atof(args[1].c_str());
122  scaleOptions_.push_back(opt);
123  }
124 
125  outputFileName_ = pset.getUntrackedParameter<string>("outputFileName", "");
126  subDirs_ = pset.getUntrackedParameter<vstring>("subDirs");
127 }
128 
130  typedef vector<string> vstring;
131 
132  // Update 2009-09-23
133  // Migrated all code from endJob to this function
134  // endJob is not necessarily called in the proper sequence
135  // and does not necessarily book histograms produced in
136  // that step.
137  // It more robust to do the histogram manipulation in
138  // this endRun function
139 
140  theDQM = nullptr;
141  theDQM = Service<DQMStore>().operator->();
142 
143  if (!theDQM) {
144  LogInfo("DQMRivetClient") << "Cannot create DQMStore instance\n";
145  return;
146  }
147 
148  // Process wildcard in the sub-directory
149  set<string> subDirSet;
150 
151  for (vstring::const_iterator iSubDir = subDirs_.begin(); iSubDir != subDirs_.end(); ++iSubDir) {
152  string subDir = *iSubDir;
153 
154  if (subDir[subDir.size() - 1] == '/')
155  subDir.erase(subDir.size() - 1);
156 
157  subDirSet.insert(subDir);
158  }
159 
160  for (set<string>::const_iterator iSubDir = subDirSet.begin(); iSubDir != subDirSet.end(); ++iSubDir) {
161  const string& dirName = *iSubDir;
162  for (vector<NormOption>::const_iterator normOption = normOptions_.begin(); normOption != normOptions_.end();
163  ++normOption) {
164  normalizeToIntegral(dirName, normOption->name, normOption->normHistName);
165  }
166  }
167 
168  for (set<string>::const_iterator iSubDir = subDirSet.begin(); iSubDir != subDirSet.end(); ++iSubDir) {
169  const string& dirName = *iSubDir;
170  for (vector<LumiOption>::const_iterator lumiOption = lumiOptions_.begin(); lumiOption != lumiOptions_.end();
171  ++lumiOption) {
172  normalizeToLumi(dirName, lumiOption->name, lumiOption->normHistName, lumiOption->xsection);
173  }
174  }
175 
176  for (set<string>::const_iterator iSubDir = subDirSet.begin(); iSubDir != subDirSet.end(); ++iSubDir) {
177  const string& dirName = *iSubDir;
178  for (vector<ScaleFactorOption>::const_iterator scaleOption = scaleOptions_.begin();
179  scaleOption != scaleOptions_.end();
180  ++scaleOption) {
181  scaleByFactor(dirName, scaleOption->name, scaleOption->scale);
182  }
183  }
184 
185  if (!outputFileName_.empty())
186  theDQM->save(outputFileName_);
187 }
188 
190  // Update 2009-09-23
191  // Migrated all code from here to endRun
192 
193  LogTrace("DQMRivetClient") << "inside of ::endJob()" << endl;
194 }
195 
197  const std::string& histName,
198  const std::string& normHistName) {
199  if (!theDQM->dirExists(startDir)) {
200  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
201  << "Cannot find sub-directory " << startDir << endl;
202  return;
203  }
204 
205  theDQM->cd();
206 
207  ME* element = theDQM->get(startDir + "/" + histName);
208  ME* normME = theDQM->get(startDir + "/" + normHistName);
209 
210  if (!element) {
211  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
212  << "No such element '" << histName << "' found\n";
213  return;
214  }
215 
216  if (!normME) {
217  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
218  << "No such element '" << normHistName << "' found\n";
219  return;
220  }
221 
222  TH1F* hist = element->getTH1F();
223  if (!hist) {
224  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
225  << "Cannot create TH1F from ME\n";
226  return;
227  }
228 
229  TH1F* normHist = normME->getTH1F();
230  if (!normHist) {
231  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
232  << "Cannot create TH1F from ME\n";
233  return;
234  }
235 
236  const double entries = normHist->Integral();
237  if (entries != 0) {
238  hist->Scale(1. / entries, "width");
239  } else {
240  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
241  << "Zero entries in histogram\n";
242  }
243 
244  return;
245 }
246 
248  const std::string& histName,
249  const std::string& normHistName,
250  double xsection) {
251  normalizeToIntegral(startDir, histName, normHistName);
252  theDQM->cd();
253  ME* element = theDQM->get(startDir + "/" + histName);
254  TH1F* hist = element->getTH1F();
255  if (!hist) {
256  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
257  << "Cannot create TH1F from ME\n";
258  return;
259  }
260  hist->Scale(xsection);
261  return;
262 }
263 
264 void DQMRivetClient::scaleByFactor(const std::string& startDir, const std::string& histName, double factor) {
265  if (!theDQM->dirExists(startDir)) {
266  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
267  << "Cannot find sub-directory " << startDir << endl;
268  return;
269  }
270 
271  theDQM->cd();
272 
273  ME* element = theDQM->get(startDir + "/" + histName);
274 
275  if (!element) {
276  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
277  << "No such element '" << histName << "' found\n";
278  return;
279  }
280 
281  TH1F* hist = element->getTH1F();
282  if (!hist) {
283  LogInfo("DQMRivetClient") << "normalizeToEntries() : "
284  << "Cannot create TH1F from ME\n";
285  return;
286  }
287  hist->Scale(factor);
288 }
289 
291 
vector< string > vstring
Definition: ExoticaDQM.cc:8
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
DQMRivetClient(const edm::ParameterSet &pset)
DQMRivetClient::MonitorElement ME
void scaleByFactor(const std::string &startDir, const std::string &histName, double factor)
#define LogTrace(id)
Definition: ME.h:11
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
void normalizeToIntegral(const std::string &startDir, const std::string &histName, const std::string &normHistName)
__shared__ Hist hist
Log< level::Info, false > LogInfo
HLT enums.
void endJob() override
void endRun(const edm::Run &r, const edm::EventSetup &c) override
EndRun.
void normalizeToLumi(const std::string &startDir, const std::string &histName, const std::string &normHistName, double xsection)
Definition: Run.h:45