test
CMS 3D CMS Logo

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