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