CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DQMRootOutputModule.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FwkIO
4 // Class : DQMRootOutputModule
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones
10 // Created: Fri Apr 29 13:26:29 CDT 2011
11 // $Id: DQMRootOutputModule.cc,v 1.14 2012/03/03 21:38:40 chrjones Exp $
12 //
13 
14 // system include files
15 #include <iostream>
16 #include <string>
17 #include <map>
18 #include <memory>
19 #include <boost/shared_ptr.hpp>
20 #include "TFile.h"
21 #include "TTree.h"
22 #include "TString.h"
23 #include "TH1.h"
24 #include "TH2.h"
25 #include "TProfile.h"
26 
27 // user include files
37 
41 
42 #include "format.h"
43 
44 namespace {
45  class TreeHelperBase {
46  public:
47  TreeHelperBase(): m_wasFilled(false), m_firstIndex(0),m_lastIndex(0) {}
48  virtual ~TreeHelperBase(){}
49  void fill(MonitorElement* iElement) {
50  doFill(iElement);
51  if(m_wasFilled) {++m_lastIndex;}
52  m_wasFilled = true; }
53  bool wasFilled() const { return m_wasFilled;}
54  void getRangeAndReset(ULong64_t& iFirstIndex, ULong64_t& iLastIndex) {
55  iFirstIndex = m_firstIndex;
56  iLastIndex = m_lastIndex;
57  m_wasFilled = false;
58  m_firstIndex = m_lastIndex +1;
59  m_lastIndex = m_firstIndex;
60  }
61  private:
62  virtual void doFill(MonitorElement*) = 0;
63  bool m_wasFilled;
64  ULong64_t m_firstIndex;
65  ULong64_t m_lastIndex;
66  };
67 
68  template<class T>
69  class TreeHelper : public TreeHelperBase {
70  public:
71  TreeHelper(TTree* iTree, std::string* iFullNameBufferPtr ):
72  m_tree(iTree), m_flagBuffer(0),m_fullNameBufferPtr(iFullNameBufferPtr){ setup();}
73  virtual void doFill(MonitorElement* iElement) {
74  *m_fullNameBufferPtr = iElement->getFullname();
75  m_flagBuffer = iElement->getTag();
76  m_bufferPtr = dynamic_cast<T*>(iElement->getRootObject());
77  assert(0!=m_bufferPtr);
78  //std::cout <<"#entries: "<<m_bufferPtr->GetEntries()<<std::endl;
79  m_tree->Fill();
80  }
81 
82 
83  private:
84  void setup() {
85  m_tree->Branch(kFullNameBranch,&m_fullNameBufferPtr);
86  m_tree->Branch(kFlagBranch,&m_flagBuffer);
87 
88  m_bufferPtr = 0;
89  m_tree->Branch(kValueBranch,&m_bufferPtr,128*1024,0);
90  }
91  TTree* m_tree;
92  uint32_t m_flagBuffer;
93  std::string* m_fullNameBufferPtr;
94  T* m_bufferPtr;
95  };
96 
97  class IntTreeHelper: public TreeHelperBase {
98  public:
99  IntTreeHelper(TTree* iTree, std::string* iFullNameBufferPtr):
100  m_tree(iTree), m_flagBuffer(0),m_fullNameBufferPtr(iFullNameBufferPtr)
101  {setup();}
102 
103  virtual void doFill(MonitorElement* iElement) {
104  *m_fullNameBufferPtr = iElement->getFullname();
105  m_flagBuffer = iElement->getTag();
106  m_buffer = iElement->getIntValue();
107  m_tree->Fill();
108  }
109 
110  private:
111  void setup() {
112  m_tree->Branch(kFullNameBranch,&m_fullNameBufferPtr);
113  m_tree->Branch(kFlagBranch,&m_flagBuffer);
114  m_tree->Branch(kValueBranch,&m_buffer);
115  }
116  TTree* m_tree;
117  uint32_t m_flagBuffer;
118  std::string* m_fullNameBufferPtr;
119  Long64_t m_buffer;
120  };
121 
122  class FloatTreeHelper: public TreeHelperBase {
123  public:
124  FloatTreeHelper(TTree* iTree, std::string* iFullNameBufferPtr):
125  m_tree(iTree), m_flagBuffer(0),m_fullNameBufferPtr(iFullNameBufferPtr)
126  {setup();}
127  virtual void doFill(MonitorElement* iElement) {
128  *m_fullNameBufferPtr = iElement->getFullname();
129  m_flagBuffer = iElement->getTag();
130  m_buffer = iElement->getFloatValue();
131  m_tree->Fill();
132  }
133  private:
134  void setup() {
135  m_tree->Branch(kFullNameBranch,&m_fullNameBufferPtr);
136  m_tree->Branch(kFlagBranch,&m_flagBuffer);
137  m_tree->Branch(kValueBranch,&m_buffer);
138  }
139 
140  TTree* m_tree;
141  uint32_t m_flagBuffer;
142  std::string* m_fullNameBufferPtr;
143  double m_buffer;
144  };
145 
146  class StringTreeHelper: public TreeHelperBase {
147  public:
148  StringTreeHelper(TTree* iTree, std::string* iFullNameBufferPtr):
149  m_tree(iTree), m_flagBuffer(0),m_fullNameBufferPtr(iFullNameBufferPtr), m_bufferPtr(&m_buffer)
150  {setup();}
151  virtual void doFill(MonitorElement* iElement) {
152  *m_fullNameBufferPtr = iElement->getFullname();
153  m_flagBuffer = iElement->getTag();
154  m_buffer = iElement->getStringValue();
155  m_tree->Fill();
156  }
157  private:
158  void setup() {
159  m_tree->Branch(kFullNameBranch,&m_fullNameBufferPtr);
160  m_tree->Branch(kFlagBranch,&m_flagBuffer);
161  m_tree->Branch(kValueBranch,&m_bufferPtr);
162  }
163 
164  TTree* m_tree;
165  uint32_t m_flagBuffer;
166  std::string* m_fullNameBufferPtr;
167  std::string m_buffer;
168  std::string* m_bufferPtr;
169  };
170 
171 }
172 
173 
175 public:
176  explicit DQMRootOutputModule(edm::ParameterSet const& pset);
177  virtual ~DQMRootOutputModule();
178  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
179 
180 private:
181  virtual void write(edm::EventPrincipal const& e);
183  virtual void writeRun(edm::RunPrincipal const&);
184  virtual void beginRun(edm::RunPrincipal const& r);
185  virtual bool isFileOpen() const;
186  virtual void openFile(edm::FileBlock const&);
187 
188 
189  virtual void startEndFile();
190  virtual void finishEndFile();
191  std::string m_fileName;
192  std::string m_logicalFileName;
193  std::auto_ptr<TFile> m_file;
194  std::vector<boost::shared_ptr<TreeHelperBase> > m_treeHelpers;
195 
196  unsigned int m_run;
197  unsigned int m_lumi;
198  unsigned int m_type;
199  unsigned int m_presentHistoryIndex;
200  ULong64_t m_beginTime;
201  ULong64_t m_endTime;
202  ULong64_t m_firstIndex;
203  ULong64_t m_lastIndex;
204  unsigned int m_filterOnRun;
205 
206  std::string m_fullNameBuffer;
207  std::string* m_fullNameBufferPtr;
208  std::map<unsigned int, unsigned int> m_dqmKindToTypeIndex;
210 
211  std::vector<edm::ProcessHistoryID> m_seenHistories;
213 };
214 
215 //
216 // constants, enums and typedefs
217 //
218 
219 static TreeHelperBase*
220 makeHelper(unsigned int iTypeIndex,
221  TTree* iTree,
222  std::string* iFullNameBufferPtr) {
223  switch(iTypeIndex) {
224  case kIntIndex:
225  return new IntTreeHelper(iTree,iFullNameBufferPtr);
226  case kFloatIndex:
227  return new FloatTreeHelper(iTree,iFullNameBufferPtr);
228  case kStringIndex:
229  return new StringTreeHelper(iTree,iFullNameBufferPtr);
230  case kTH1FIndex:
231  return new TreeHelper<TH1F>(iTree,iFullNameBufferPtr);
232  case kTH1SIndex:
233  return new TreeHelper<TH1S>(iTree,iFullNameBufferPtr);
234  case kTH1DIndex:
235  return new TreeHelper<TH1D>(iTree,iFullNameBufferPtr);
236  case kTH2FIndex:
237  return new TreeHelper<TH2F>(iTree,iFullNameBufferPtr);
238  case kTH2SIndex:
239  return new TreeHelper<TH2S>(iTree,iFullNameBufferPtr);
240  case kTH2DIndex:
241  return new TreeHelper<TH2D>(iTree,iFullNameBufferPtr);
242  case kTH3FIndex:
243  return new TreeHelper<TH3F>(iTree,iFullNameBufferPtr);
244  case kTProfileIndex:
245  return new TreeHelper<TProfile>(iTree,iFullNameBufferPtr);
246  case kTProfile2DIndex:
247  return new TreeHelper<TProfile2D>(iTree,iFullNameBufferPtr);
248  }
249  assert(false);
250  return 0;
251 }
252 
253 //
254 // static data member definitions
255 //
256 
257 //
258 // constructors and destructor
259 //
261 edm::OutputModule(pset),
262 m_fileName(pset.getUntrackedParameter<std::string>("fileName")),
263 m_logicalFileName(pset.getUntrackedParameter<std::string>("logicalFileName","")),
264 m_file(0),
265 m_treeHelpers(kNIndicies,boost::shared_ptr<TreeHelperBase>()),
266 m_presentHistoryIndex(0),
267 m_filterOnRun(pset.getUntrackedParameter<unsigned int>("filterOnRun",0)),
268 m_fullNameBufferPtr(&m_fullNameBuffer),
269 m_indicesTree(0)
270 {
271 }
272 
273 // DQMRootOutputModule::DQMRootOutputModule(const DQMRootOutputModule& rhs)
274 // {
275 // // do actual copying here;
276 // }
277 
279 {
280 }
281 
282 //
283 // assignment operators
284 //
285 // const DQMRootOutputModule& DQMRootOutputModule::operator=(const DQMRootOutputModule& rhs)
286 // {
287 // //An exception safe implementation is
288 // DQMRootOutputModule temp(rhs);
289 // swap(rhs);
290 //
291 // return *this;
292 // }
293 
294 //
295 // member functions
296 //
297 bool
299 {
300  return nullptr!=m_file.get();
301 }
302 
303 void
305 {
306  //NOTE: I need to also set the I/O performance settings
307 
308  m_file = std::auto_ptr<TFile>(new TFile(m_fileName.c_str(),"RECREATE",
309  "1" //This is the file format version number
310  ));
311 
313  cms::Digest branchHash;
316  std::string(),
317  "DQMRootOutputModule",
318  description().moduleLabel(),
319  m_file->GetUUID().AsString(),
320  std::string(),
321  branchHash.digest().toString(),
322  std::vector<std::string>()
323  );
324 
325 
327  m_indicesTree->Branch(kRunBranch,&m_run);
328  m_indicesTree->Branch(kLumiBranch,&m_lumi);
332  m_indicesTree->Branch(kTypeBranch,&m_type);
335  m_indicesTree->SetDirectory(m_file.get());
336 
337  unsigned int i = 0;
338  for(std::vector<boost::shared_ptr<TreeHelperBase> >::iterator it = m_treeHelpers.begin(), itEnd = m_treeHelpers.end();
339  it != itEnd;
340  ++it,++i) {
341  //std::cout <<"making "<<kTypeNames[i]<<std::endl;
342  TTree* tree = new TTree(kTypeNames[i],kTypeNames[i]);
343  *it = boost::shared_ptr<TreeHelperBase>(makeHelper(i,tree,m_fullNameBufferPtr));
344  tree->SetDirectory(m_file.get()); //TFile takes ownership
345  }
346 
359 }
360 
361 
362 void
364 
365 }
366 void
368  //std::cout << "DQMRootOutputModule::writeLuminosityBlock"<< std::endl;
369  edm::Service<DQMStore> dstore;
370  m_run=iLumi.id().run();
371  m_lumi = iLumi.id().value();
372  m_beginTime = iLumi.beginTime().value();
373  m_endTime = iLumi.endTime().value();
374  bool shouldWrite = (m_filterOnRun == 0 ||
375  (m_filterOnRun != 0 && m_filterOnRun == m_run));
376 
377  if (! shouldWrite)
378  return;
379  std::vector<MonitorElement *> items(dstore->getAllContents(""));
380  for(std::vector<MonitorElement*>::iterator it = items.begin(), itEnd=items.end();
381  it!=itEnd;
382  ++it) {
383  if((*it)->getLumiFlag()) {
384  std::map<unsigned int,unsigned int>::iterator itFound = m_dqmKindToTypeIndex.find((*it)->kind());
385  assert(itFound !=m_dqmKindToTypeIndex.end());
386  m_treeHelpers[itFound->second]->fill(*it);
387  }
388  }
389  //Now store the relationship between run/lumi and indices in the other TTrees
390  bool storedLumiIndex = false;
391  unsigned int typeIndex = 0;
392  for(std::vector<boost::shared_ptr<TreeHelperBase> >::iterator it = m_treeHelpers.begin(), itEnd = m_treeHelpers.end();
393  it != itEnd;
394  ++it,++typeIndex) {
395  if((*it)->wasFilled()) {
396  m_type = typeIndex;
397  (*it)->getRangeAndReset(m_firstIndex,m_lastIndex);
398  storedLumiIndex = true;
399  m_indicesTree->Fill();
400  }
401  }
402  if(not storedLumiIndex) {
403  //need to record lumis even if we stored no MonitorElements since some later DQM modules
404  // look to see what lumis were processed
406  m_firstIndex=0;
407  m_lastIndex=0;
408  m_indicesTree->Fill();
409  }
410 
413 }
414 
415 
417  //std::cout << "DQMRootOutputModule::writeRun"<< std::endl;
418  edm::Service<DQMStore> dstore;
419  m_run=iRun.id().run();
420  m_lumi = 0;
421  m_beginTime = iRun.beginTime().value();
422  m_endTime = iRun.endTime().value();
423  bool shouldWrite = (m_filterOnRun == 0 ||
424  (m_filterOnRun != 0 && m_filterOnRun == m_run));
425 
426  if (! shouldWrite)
427  return;
428 
429  std::vector<MonitorElement *> items(dstore->getAllContents(""));
430  for(std::vector<MonitorElement*>::iterator it = items.begin(), itEnd=items.end();
431  it!=itEnd;
432  ++it) {
433  if(not (*it)->getLumiFlag()) {
434  std::map<unsigned int,unsigned int>::iterator itFound = m_dqmKindToTypeIndex.find((*it)->kind());
435  assert (itFound !=m_dqmKindToTypeIndex.end());
436  m_treeHelpers[itFound->second]->fill(*it);
437  }
438  }
439 
440  //Now store the relationship between run/lumi and indices in the other TTrees
441  unsigned int typeIndex = 0;
442  for(std::vector<boost::shared_ptr<TreeHelperBase> >::iterator it = m_treeHelpers.begin(), itEnd = m_treeHelpers.end();
443  it != itEnd;
444  ++it,++typeIndex) {
445  if((*it)->wasFilled()) {
446  m_type = typeIndex;
447  (*it)->getRangeAndReset(m_firstIndex,m_lastIndex);
448  m_indicesTree->Fill();
449  }
450  }
451 
453  jr->reportRunNumber(m_run);
454 }
455 
457  //std::cout << "DQMRootOutputModule::beginRun"<< std::endl;
458  //The ProcessHistory for a lumi must be the same as its Run so we only need to
459  // record it at Run time
460  edm::ProcessHistoryID id = iPrincipal.processHistoryID();
461  std::vector<edm::ProcessHistoryID>::iterator itFind = std::find(m_seenHistories.begin(),m_seenHistories.end(),id);
462  if(itFind == m_seenHistories.end()) {
464  m_seenHistories.push_back(id);
465  } else {
466  m_presentHistoryIndex = itFind - m_seenHistories.begin();
467  }
468 }
469 
471  //std::cout << "DQMRootOutputModule::startEndFile"<< std::endl;
472  //fill in the meta data
473  m_file->cd();
474  TDirectory* metaDataDirectory = m_file->mkdir(kMetaDataDirectory);
475 
476 
477  //Write out the Process History
478  TTree* processHistoryTree = new TTree(kProcessHistoryTree,kProcessHistoryTree);
479  processHistoryTree->SetDirectory(metaDataDirectory);
480 
481  unsigned int index = 0;
482  processHistoryTree->Branch(kPHIndexBranch,&index);
483  std::string processName;
484  processHistoryTree->Branch(kProcessConfigurationProcessNameBranch,&processName);
485  std::string parameterSetID;
486  processHistoryTree->Branch(kProcessConfigurationParameterSetIDBranch,&parameterSetID);
487  std::string releaseVersion;
488  processHistoryTree->Branch(kProcessConfigurationReleaseVersion,&releaseVersion);
489  std::string passID;
490  processHistoryTree->Branch(kProcessConfigurationPassID,&passID);
491 
493  assert(0!=phr);
494  for(std::vector<edm::ProcessHistoryID>::iterator it = m_seenHistories.begin(), itEnd = m_seenHistories.end();
495  it !=itEnd;
496  ++it) {
497  const edm::ProcessHistory* history = phr->getMapped(*it);
498  assert(0!=history);
499  index = 0;
500  for(edm::ProcessHistory::collection_type::const_iterator itPC = history->begin(), itPCEnd = history->end();
501  itPC != itPCEnd;
502  ++itPC,++index) {
503  processName = itPC->processName();
504  releaseVersion = itPC->releaseVersion();
505  passID = itPC->passID();
506  parameterSetID = itPC->parameterSetID().compactForm();
507  processHistoryTree->Fill();
508  }
509  }
510 
511  //Store the ParameterSets
512  TTree* parameterSetsTree = new TTree(kParameterSetTree,kParameterSetTree);
513  parameterSetsTree->SetDirectory(metaDataDirectory);
514  std::string blob;
515  parameterSetsTree->Branch(kParameterSetBranch,&blob);
516 
518  assert(0!=psr);
519  for(edm::pset::Registry::const_iterator it = psr->begin(), itEnd = psr->end();
520  it != itEnd;
521  ++it) {
522  blob.clear();
523  it->second.toString(blob);
524  parameterSetsTree->Fill();
525  }
526 
527 }
528 
530  //std::cout << "DQMRootOutputModule::finishEndFile"<< std::endl;
531  m_file->Write();
532  m_file->Close();
535 }
536 
537 //
538 // const member functions
539 //
540 
541 //
542 // static member functions
543 //
544 void
546  //The following says we do not know what parameters are allowed so do no validation
547  // Please change this to state exactly what you do use, even if it is no parameters
549  desc.setUnknown();
550  descriptions.addDefault(desc);
551 
552  //NOTE: when actually filling this in, do not forget to add a untracked PSet 'dataset'
553  // which is used for bookkeeping by the DMWM
554 }
555 
556 
virtual bool isFileOpen() const
const_iterator begin() const
Token outputFileOpened(std::string const &physicalFileName, std::string const &logicalFileName, std::string const &catalog, std::string const &outputModuleClassName, std::string const &moduleLabel, std::string const &guid, std::string const &dataType, std::string const &branchHash, std::vector< std::string > const &branchNames)
Definition: JobReport.cc:561
int i
Definition: DBlmapReader.cc:9
static const char *const kProcessHistoryTree
Definition: format.h:55
static const char *const kRunBranch
Definition: format.h:42
virtual void write(edm::EventPrincipal const &e)
static const char *const kTypeNames[]
Definition: format.h:29
string fill
Definition: lumiContext.py:319
Timestamp const & beginTime() const
RunNumber_t run() const
Definition: RunID.h:44
virtual void writeRun(edm::RunPrincipal const &)
virtual void openFile(edm::FileBlock const &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
static const char *const kIndicesTree
Definition: format.h:41
Timestamp const & endTime() const
unsigned int m_presentHistoryIndex
boost::uint64_t value() const
static TreeHelperBase * makeHelper(unsigned int iTypeIndex, TTree *iTree, std::string *iFullNameBufferPtr)
static const char *const kFirstIndex
Definition: format.h:48
std::string const & processName() const
Definition: OutputModule.h:55
static const char *const kLumiBranch
Definition: format.h:43
static ThreadSafeRegistry * instance()
static const char *const kFullNameBranch
Definition: format.h:35
MD5Result digest() const
Definition: Digest.cc:194
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
void outputFileClosed(Token fileToken)
Definition: JobReport.cc:600
const uint32_t getTag(void) const
std::vector< MonitorElement * > getAllContents(const std::string &path) const
Definition: DQMStore.cc:1672
static const char *const kPHIndexBranch
Definition: format.h:56
static const char *const kParameterSetBranch
Definition: format.h:63
std::map< unsigned int, unsigned int > m_dqmKindToTypeIndex
bool getMapped(key_type const &k, value_type &result) const
void addDefault(ParameterSetDescription const &psetDescription)
virtual void finishEndFile()
ProcessHistoryID const & processHistoryID() const
Definition: Principal.h:126
static const char *const kMetaDataDirectory
Definition: format.h:53
Timestamp const & beginTime() const
Definition: RunPrincipal.h:54
std::auto_ptr< TFile > m_file
collection_type::const_iterator const_iterator
static const char *const kParameterSetTree
Definition: format.h:62
double getFloatValue(void) const
RunNumber_t run() const
Timestamp const & endTime() const
Definition: RunPrincipal.h:58
static const char *const kFlagBranch
Definition: format.h:36
static const char *const kTypeBranch
Definition: format.h:47
static const char *const kProcessHistoryIndexBranch
Definition: format.h:44
static const char *const kEndTimeBranch
Definition: format.h:46
const std::string getFullname(void) const
get full name of ME including Pathname
const std::string & getStringValue(void) const
std::size_t Token
Definition: JobReport.h:43
DQMRootOutputModule(edm::ParameterSet const &pset)
static const char *const kProcessConfigurationPassID
Definition: format.h:60
RunID const & id() const
Definition: RunPrincipal.h:50
TimeValue_t value() const
Definition: Timestamp.cc:72
std::vector< boost::shared_ptr< TreeHelperBase > > m_treeHelpers
std::string * m_fullNameBufferPtr
TObject * getRootObject(void) const
void reportLumiSection(unsigned int run, unsigned int lumiSectId)
Definition: JobReport.cc:653
static const char *const kLastIndex
Definition: format.h:49
int64_t getIntValue(void) const
std::string toString() const
Definition: Digest.cc:87
static const char *const kProcessConfigurationReleaseVersion
Definition: format.h:59
static const char *const kProcessConfigurationProcessNameBranch
Definition: format.h:57
const_iterator end() const
static const char *const kBeginTimeBranch
Definition: format.h:45
virtual void writeLuminosityBlock(edm::LuminosityBlockPrincipal const &)
edm::JobReport::Token m_jrToken
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
long double T
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
ModuleDescription const & description() const
virtual void beginRun(edm::RunPrincipal const &r)
static const char *const kValueBranch
Definition: format.h:37
void reportRunNumber(unsigned int run)
Definition: JobReport.cc:663
static const char *const kProcessConfigurationParameterSetIDBranch
Definition: format.h:58
std::vector< edm::ProcessHistoryID > m_seenHistories