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.10 2011/06/22 16:08:13 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 void startEndFile();
186  virtual void finishEndFile();
187  std::string m_fileName;
188  std::string m_logicalFileName;
189  std::auto_ptr<TFile> m_file;
190  std::vector<boost::shared_ptr<TreeHelperBase> > m_treeHelpers;
191 
192  unsigned int m_run;
193  unsigned int m_lumi;
194  unsigned int m_type;
195  unsigned int m_presentHistoryIndex;
196  ULong64_t m_beginTime;
197  ULong64_t m_endTime;
198  ULong64_t m_firstIndex;
199  ULong64_t m_lastIndex;
200 
201  std::string m_fullNameBuffer;
202  std::string* m_fullNameBufferPtr;
203  std::map<unsigned int, unsigned int> m_dqmKindToTypeIndex;
205 
206  std::vector<edm::ProcessHistoryID> m_seenHistories;
208 };
209 
210 //
211 // constants, enums and typedefs
212 //
213 
214 static TreeHelperBase*
215 makeHelper(unsigned int iTypeIndex,
216  TTree* iTree,
217  std::string* iFullNameBufferPtr) {
218  switch(iTypeIndex) {
219  case kIntIndex:
220  return new IntTreeHelper(iTree,iFullNameBufferPtr);
221  case kFloatIndex:
222  return new FloatTreeHelper(iTree,iFullNameBufferPtr);
223  case kStringIndex:
224  return new StringTreeHelper(iTree,iFullNameBufferPtr);
225  case kTH1FIndex:
226  return new TreeHelper<TH1F>(iTree,iFullNameBufferPtr);
227  case kTH1SIndex:
228  return new TreeHelper<TH1S>(iTree,iFullNameBufferPtr);
229  case kTH1DIndex:
230  return new TreeHelper<TH1D>(iTree,iFullNameBufferPtr);
231  case kTH2FIndex:
232  return new TreeHelper<TH2F>(iTree,iFullNameBufferPtr);
233  case kTH2SIndex:
234  return new TreeHelper<TH2S>(iTree,iFullNameBufferPtr);
235  case kTH2DIndex:
236  return new TreeHelper<TH2D>(iTree,iFullNameBufferPtr);
237  case kTH3FIndex:
238  return new TreeHelper<TH3F>(iTree,iFullNameBufferPtr);
239  case kTProfileIndex:
240  return new TreeHelper<TProfile>(iTree,iFullNameBufferPtr);
241  case kTProfile2DIndex:
242  return new TreeHelper<TProfile2D>(iTree,iFullNameBufferPtr);
243  }
244  assert(false);
245  return 0;
246 }
247 
248 //
249 // static data member definitions
250 //
251 
252 //
253 // constructors and destructor
254 //
256 edm::OutputModule(pset),
257 m_fileName(pset.getUntrackedParameter<std::string>("fileName")),
258 m_logicalFileName(pset.getUntrackedParameter<std::string>("logicalFileName","")),
259 m_file(0),
260 m_treeHelpers(kNIndicies,boost::shared_ptr<TreeHelperBase>()),
261 m_presentHistoryIndex(0),
262 m_fullNameBufferPtr(&m_fullNameBuffer),
263 m_indicesTree(0)
264 {
265  //NOTE: I need to also set the I/O performance settings
266 
267  m_file = std::auto_ptr<TFile>(new TFile(m_fileName.c_str(),"CREATE",
268  "1" //This is the file format version number
269  ));
270 
272  cms::Digest branchHash;
275  std::string(),
276  "DQMRootOutputModule",
277  pset.getParameter<std::string>("@module_label"),
278  m_file->GetUUID().AsString(),
279  std::string(),
280  branchHash.digest().toString(),
281  std::vector<std::string>()
282  );
283 
284 
286  m_indicesTree->Branch(kRunBranch,&m_run);
287  m_indicesTree->Branch(kLumiBranch,&m_lumi);
291  m_indicesTree->Branch(kTypeBranch,&m_type);
294  m_indicesTree->SetDirectory(m_file.get());
295 
296  unsigned int i = 0;
297  for(std::vector<boost::shared_ptr<TreeHelperBase> >::iterator it = m_treeHelpers.begin(), itEnd = m_treeHelpers.end();
298  it != itEnd;
299  ++it,++i) {
300  //std::cout <<"making "<<kTypeNames[i]<<std::endl;
301  TTree* tree = new TTree(kTypeNames[i],kTypeNames[i]);
302  *it = boost::shared_ptr<TreeHelperBase>(makeHelper(i,tree,m_fullNameBufferPtr));
303  tree->SetDirectory(m_file.get()); //TFile takes ownership
304  }
305 
318 }
319 
320 // DQMRootOutputModule::DQMRootOutputModule(const DQMRootOutputModule& rhs)
321 // {
322 // // do actual copying here;
323 // }
324 
326 {
327 }
328 
329 //
330 // assignment operators
331 //
332 // const DQMRootOutputModule& DQMRootOutputModule::operator=(const DQMRootOutputModule& rhs)
333 // {
334 // //An exception safe implementation is
335 // DQMRootOutputModule temp(rhs);
336 // swap(rhs);
337 //
338 // return *this;
339 // }
340 
341 //
342 // member functions
343 //
344 void
346 
347 }
348 void
350  edm::Service<DQMStore> dstore;
351  m_run=iLumi.id().run();
352  m_lumi = iLumi.id().value();
353  m_beginTime = iLumi.beginTime().value();
354  m_endTime = iLumi.endTime().value();
355 
356  std::vector<MonitorElement *> items(dstore->getAllContents(""));
357  for(std::vector<MonitorElement*>::iterator it = items.begin(), itEnd=items.end();
358  it!=itEnd;
359  ++it) {
360  if((*it)->getLumiFlag()) {
361  std::map<unsigned int,unsigned int>::iterator itFound = m_dqmKindToTypeIndex.find((*it)->kind());
362  assert(itFound !=m_dqmKindToTypeIndex.end());
363  m_treeHelpers[itFound->second]->fill(*it);
364  }
365  }
366  //Now store the relationship between run/lumi and indices in the other TTrees
367  bool storedLumiIndex = false;
368  unsigned int typeIndex = 0;
369  for(std::vector<boost::shared_ptr<TreeHelperBase> >::iterator it = m_treeHelpers.begin(), itEnd = m_treeHelpers.end();
370  it != itEnd;
371  ++it,++typeIndex) {
372  if((*it)->wasFilled()) {
373  m_type = typeIndex;
374  (*it)->getRangeAndReset(m_firstIndex,m_lastIndex);
375  storedLumiIndex = true;
376  m_indicesTree->Fill();
377  }
378  }
379  if(not storedLumiIndex) {
380  //need to record lumis even if we stored no MonitorElements since some later DQM modules
381  // look to see what lumis were processed
383  m_firstIndex=0;
384  m_lastIndex=0;
385  m_indicesTree->Fill();
386  }
387 
390 }
391 
393  edm::Service<DQMStore> dstore;
394  m_run=iRun.id().run();
395  m_lumi = 0;
396  m_beginTime = iRun.beginTime().value();
397  m_endTime = iRun.endTime().value();
398 
399  std::vector<MonitorElement *> items(dstore->getAllContents(""));
400  for(std::vector<MonitorElement*>::iterator it = items.begin(), itEnd=items.end();
401  it!=itEnd;
402  ++it) {
403  if(not (*it)->getLumiFlag()) {
404  std::map<unsigned int,unsigned int>::iterator itFound = m_dqmKindToTypeIndex.find((*it)->kind());
405  assert (itFound !=m_dqmKindToTypeIndex.end());
406  m_treeHelpers[itFound->second]->fill(*it);
407  }
408  }
409 
410  //Now store the relationship between run/lumi and indices in the other TTrees
411  unsigned int typeIndex = 0;
412  for(std::vector<boost::shared_ptr<TreeHelperBase> >::iterator it = m_treeHelpers.begin(), itEnd = m_treeHelpers.end();
413  it != itEnd;
414  ++it,++typeIndex) {
415  if((*it)->wasFilled()) {
416  m_type = typeIndex;
417  (*it)->getRangeAndReset(m_firstIndex,m_lastIndex);
418  m_indicesTree->Fill();
419  }
420  }
421 
423  jr->reportRunNumber(m_run);
424 }
425 
427  //The ProcessHistory for a lumi must be the same as its Run so we only need to
428  // record it at Run time
429  edm::ProcessHistoryID id = iPrincipal.processHistoryID();
430  std::vector<edm::ProcessHistoryID>::iterator itFind = std::find(m_seenHistories.begin(),m_seenHistories.end(),id);
431  if(itFind == m_seenHistories.end()) {
433  m_seenHistories.push_back(id);
434  } else {
435  m_presentHistoryIndex = itFind - m_seenHistories.begin();
436  }
437 }
438 
440  //fill in the meta data
441  m_file->cd();
442  TDirectory* metaDataDirectory = m_file->mkdir(kMetaDataDirectory);
443 
444 
445  //Write out the Process History
446  TTree* processHistoryTree = new TTree(kProcessHistoryTree,kProcessHistoryTree);
447  processHistoryTree->SetDirectory(metaDataDirectory);
448 
449  unsigned int index = 0;
450  processHistoryTree->Branch(kPHIndexBranch,&index);
451  std::string processName;
452  processHistoryTree->Branch(kProcessConfigurationProcessNameBranch,&processName);
453  std::string parameterSetID;
454  processHistoryTree->Branch(kProcessConfigurationParameterSetIDBranch,&parameterSetID);
455  std::string releaseVersion;
456  processHistoryTree->Branch(kProcessConfigurationReleaseVersion,&releaseVersion);
457  std::string passID;
458  processHistoryTree->Branch(kProcessConfigurationPassID,&passID);
459 
461  assert(0!=phr);
462  for(std::vector<edm::ProcessHistoryID>::iterator it = m_seenHistories.begin(), itEnd = m_seenHistories.end();
463  it !=itEnd;
464  ++it) {
465  const edm::ProcessHistory* history = phr->getMapped(*it);
466  assert(0!=history);
467  index = 0;
468  for(edm::ProcessHistory::collection_type::const_iterator itPC = history->begin(), itPCEnd = history->end();
469  itPC != itPCEnd;
470  ++itPC,++index) {
471  processName = itPC->processName();
472  releaseVersion = itPC->releaseVersion();
473  passID = itPC->passID();
474  parameterSetID = itPC->parameterSetID().compactForm();
475  processHistoryTree->Fill();
476  }
477  }
478 
479  //Store the ParameterSets
480  TTree* parameterSetsTree = new TTree(kParameterSetTree,kParameterSetTree);
481  parameterSetsTree->SetDirectory(metaDataDirectory);
482  std::string blob;
483  parameterSetsTree->Branch(kParameterSetBranch,&blob);
484 
486  assert(0!=psr);
487  for(edm::pset::Registry::const_iterator it = psr->begin(), itEnd = psr->end();
488  it != itEnd;
489  ++it) {
490  blob.clear();
491  it->second.toString(blob);
492  parameterSetsTree->Fill();
493  }
494 
495 }
496 
498  m_file->Write();
499  m_file->Close();
502 }
503 
504 //
505 // const member functions
506 //
507 
508 //
509 // static member functions
510 //
511 void
513  //The following says we do not know what parameters are allowed so do no validation
514  // Please change this to state exactly what you do use, even if it is no parameters
516  desc.setUnknown();
517  descriptions.addDefault(desc);
518 
519  //NOTE: when actually filling this in, do not forget to add a untracked PSet 'dataset'
520  // which is used for bookkeeping by the DMWM
521 }
522 
523 
T getParameter(std::string const &) 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:528
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
Timestamp const & beginTime() const
RunNumber_t run() const
Definition: RunID.h:44
virtual void writeRun(edm::RunPrincipal 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
bool getMapped(key_type const &k, value_type &result) const
static const char *const kLumiBranch
Definition: format.h:43
static const char *const kFullNameBranch
Definition: format.h:35
MD5Result digest() const
Definition: Digest.cc:188
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:567
const uint32_t getTag(void) const
std::vector< MonitorElement * > getAllContents(const std::string &path) const
Definition: DQMStore.cc:1469
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
void addDefault(ParameterSetDescription const &psetDescription)
virtual void finishEndFile()
ProcessHistoryID const & processHistoryID() const
Definition: Principal.h:116
static const char *const kMetaDataDirectory
Definition: format.h:53
Timestamp const & beginTime() const
Definition: RunPrincipal.h:55
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:59
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:44
DQMRootOutputModule(edm::ParameterSet const &pset)
static const char *const kProcessConfigurationPassID
Definition: format.h:60
RunID const & id() const
Definition: RunPrincipal.h:51
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:620
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 ThreadSafeRegistry * instance()
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
long double T
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
virtual void beginRun(edm::RunPrincipal const &r)
static const char *const kValueBranch
Definition: format.h:37
void reportRunNumber(unsigned int run)
Definition: JobReport.cc:630
static const char *const kProcessConfigurationParameterSetIDBranch
Definition: format.h:58
std::vector< edm::ProcessHistoryID > m_seenHistories