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.16 2013/02/01 16:38:32 wdd Exp $
12 //
13 
14 // system include files
15 #include <algorithm>
16 #include <iostream>
17 #include <string>
18 #include <map>
19 #include <memory>
20 #include <vector>
21 #include <boost/shared_ptr.hpp>
22 #include "TFile.h"
23 #include "TTree.h"
24 #include "TString.h"
25 #include "TH1.h"
26 #include "TH2.h"
27 #include "TProfile.h"
28 
29 // user include files
40 
45 
46 #include "format.h"
47 
48 namespace {
49  class TreeHelperBase {
50  public:
51  TreeHelperBase(): m_wasFilled(false), m_firstIndex(0),m_lastIndex(0) {}
52  virtual ~TreeHelperBase(){}
53  void fill(MonitorElement* iElement) {
54  doFill(iElement);
55  if(m_wasFilled) {++m_lastIndex;}
56  m_wasFilled = true; }
57  bool wasFilled() const { return m_wasFilled;}
58  void getRangeAndReset(ULong64_t& iFirstIndex, ULong64_t& iLastIndex) {
59  iFirstIndex = m_firstIndex;
60  iLastIndex = m_lastIndex;
61  m_wasFilled = false;
62  m_firstIndex = m_lastIndex +1;
63  m_lastIndex = m_firstIndex;
64  }
65  private:
66  virtual void doFill(MonitorElement*) = 0;
67  bool m_wasFilled;
68  ULong64_t m_firstIndex;
69  ULong64_t m_lastIndex;
70  };
71 
72  template<class T>
73  class TreeHelper : public TreeHelperBase {
74  public:
75  TreeHelper(TTree* iTree, std::string* iFullNameBufferPtr ):
76  m_tree(iTree), m_flagBuffer(0),m_fullNameBufferPtr(iFullNameBufferPtr){ setup();}
77  virtual void doFill(MonitorElement* iElement) {
78  *m_fullNameBufferPtr = iElement->getFullname();
79  m_flagBuffer = iElement->getTag();
80  m_bufferPtr = dynamic_cast<T*>(iElement->getRootObject());
81  assert(0!=m_bufferPtr);
82  //std::cout <<"#entries: "<<m_bufferPtr->GetEntries()<<std::endl;
83  m_tree->Fill();
84  }
85 
86 
87  private:
88  void setup() {
89  m_tree->Branch(kFullNameBranch,&m_fullNameBufferPtr);
90  m_tree->Branch(kFlagBranch,&m_flagBuffer);
91 
92  m_bufferPtr = 0;
93  m_tree->Branch(kValueBranch,&m_bufferPtr,128*1024,0);
94  }
95  TTree* m_tree;
96  uint32_t m_flagBuffer;
97  std::string* m_fullNameBufferPtr;
98  T* m_bufferPtr;
99  };
100 
101  class IntTreeHelper: public TreeHelperBase {
102  public:
103  IntTreeHelper(TTree* iTree, std::string* iFullNameBufferPtr):
104  m_tree(iTree), m_flagBuffer(0),m_fullNameBufferPtr(iFullNameBufferPtr)
105  {setup();}
106 
107  virtual void doFill(MonitorElement* iElement) {
108  *m_fullNameBufferPtr = iElement->getFullname();
109  m_flagBuffer = iElement->getTag();
110  m_buffer = iElement->getIntValue();
111  m_tree->Fill();
112  }
113 
114  private:
115  void setup() {
116  m_tree->Branch(kFullNameBranch,&m_fullNameBufferPtr);
117  m_tree->Branch(kFlagBranch,&m_flagBuffer);
118  m_tree->Branch(kValueBranch,&m_buffer);
119  }
120  TTree* m_tree;
121  uint32_t m_flagBuffer;
122  std::string* m_fullNameBufferPtr;
123  Long64_t m_buffer;
124  };
125 
126  class FloatTreeHelper: public TreeHelperBase {
127  public:
128  FloatTreeHelper(TTree* iTree, std::string* iFullNameBufferPtr):
129  m_tree(iTree), m_flagBuffer(0),m_fullNameBufferPtr(iFullNameBufferPtr)
130  {setup();}
131  virtual void doFill(MonitorElement* iElement) {
132  *m_fullNameBufferPtr = iElement->getFullname();
133  m_flagBuffer = iElement->getTag();
134  m_buffer = iElement->getFloatValue();
135  m_tree->Fill();
136  }
137  private:
138  void setup() {
139  m_tree->Branch(kFullNameBranch,&m_fullNameBufferPtr);
140  m_tree->Branch(kFlagBranch,&m_flagBuffer);
141  m_tree->Branch(kValueBranch,&m_buffer);
142  }
143 
144  TTree* m_tree;
145  uint32_t m_flagBuffer;
146  std::string* m_fullNameBufferPtr;
147  double m_buffer;
148  };
149 
150  class StringTreeHelper: public TreeHelperBase {
151  public:
152  StringTreeHelper(TTree* iTree, std::string* iFullNameBufferPtr):
153  m_tree(iTree), m_flagBuffer(0),m_fullNameBufferPtr(iFullNameBufferPtr), m_bufferPtr(&m_buffer)
154  {setup();}
155  virtual void doFill(MonitorElement* iElement) {
156  *m_fullNameBufferPtr = iElement->getFullname();
157  m_flagBuffer = iElement->getTag();
158  m_buffer = iElement->getStringValue();
159  m_tree->Fill();
160  }
161  private:
162  void setup() {
163  m_tree->Branch(kFullNameBranch,&m_fullNameBufferPtr);
164  m_tree->Branch(kFlagBranch,&m_flagBuffer);
165  m_tree->Branch(kValueBranch,&m_bufferPtr);
166  }
167 
168  TTree* m_tree;
169  uint32_t m_flagBuffer;
170  std::string* m_fullNameBufferPtr;
171  std::string m_buffer;
172  std::string* m_bufferPtr;
173  };
174 
175 }
176 
177 
179 public:
180  explicit DQMRootOutputModule(edm::ParameterSet const& pset);
181  virtual ~DQMRootOutputModule();
182  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
183 
184 private:
185  virtual void write(edm::EventPrincipal const& e);
187  virtual void writeRun(edm::RunPrincipal const&);
188  virtual bool isFileOpen() const;
189  virtual void openFile(edm::FileBlock const&);
190 
191 
192  virtual void startEndFile();
193  virtual void finishEndFile();
196  std::auto_ptr<TFile> m_file;
197  std::vector<boost::shared_ptr<TreeHelperBase> > m_treeHelpers;
198 
199  unsigned int m_run;
200  unsigned int m_lumi;
201  unsigned int m_type;
202  unsigned int m_presentHistoryIndex;
203  ULong64_t m_beginTime;
204  ULong64_t m_endTime;
205  ULong64_t m_firstIndex;
206  ULong64_t m_lastIndex;
207  unsigned int m_filterOnRun;
208 
211  std::map<unsigned int, unsigned int> m_dqmKindToTypeIndex;
213 
214  std::vector<edm::ProcessHistoryID> m_seenHistories;
216 };
217 
218 //
219 // constants, enums and typedefs
220 //
221 
222 static TreeHelperBase*
223 makeHelper(unsigned int iTypeIndex,
224  TTree* iTree,
225  std::string* iFullNameBufferPtr) {
226  switch(iTypeIndex) {
227  case kIntIndex:
228  return new IntTreeHelper(iTree,iFullNameBufferPtr);
229  case kFloatIndex:
230  return new FloatTreeHelper(iTree,iFullNameBufferPtr);
231  case kStringIndex:
232  return new StringTreeHelper(iTree,iFullNameBufferPtr);
233  case kTH1FIndex:
234  return new TreeHelper<TH1F>(iTree,iFullNameBufferPtr);
235  case kTH1SIndex:
236  return new TreeHelper<TH1S>(iTree,iFullNameBufferPtr);
237  case kTH1DIndex:
238  return new TreeHelper<TH1D>(iTree,iFullNameBufferPtr);
239  case kTH2FIndex:
240  return new TreeHelper<TH2F>(iTree,iFullNameBufferPtr);
241  case kTH2SIndex:
242  return new TreeHelper<TH2S>(iTree,iFullNameBufferPtr);
243  case kTH2DIndex:
244  return new TreeHelper<TH2D>(iTree,iFullNameBufferPtr);
245  case kTH3FIndex:
246  return new TreeHelper<TH3F>(iTree,iFullNameBufferPtr);
247  case kTProfileIndex:
248  return new TreeHelper<TProfile>(iTree,iFullNameBufferPtr);
249  case kTProfile2DIndex:
250  return new TreeHelper<TProfile2D>(iTree,iFullNameBufferPtr);
251  }
252  assert(false);
253  return 0;
254 }
255 
256 //
257 // static data member definitions
258 //
259 
260 //
261 // constructors and destructor
262 //
264 edm::OutputModule(pset),
265 m_fileName(pset.getUntrackedParameter<std::string>("fileName")),
266 m_logicalFileName(pset.getUntrackedParameter<std::string>("logicalFileName","")),
267 m_file(0),
268 m_treeHelpers(kNIndicies,boost::shared_ptr<TreeHelperBase>()),
269 m_presentHistoryIndex(0),
270 m_filterOnRun(pset.getUntrackedParameter<unsigned int>("filterOnRun",0)),
271 m_fullNameBufferPtr(&m_fullNameBuffer),
272 m_indicesTree(0)
273 {
274 }
275 
276 // DQMRootOutputModule::DQMRootOutputModule(const DQMRootOutputModule& rhs)
277 // {
278 // // do actual copying here;
279 // }
280 
282 {
283 }
284 
285 //
286 // assignment operators
287 //
288 // const DQMRootOutputModule& DQMRootOutputModule::operator=(const DQMRootOutputModule& rhs)
289 // {
290 // //An exception safe implementation is
291 // DQMRootOutputModule temp(rhs);
292 // swap(rhs);
293 //
294 // return *this;
295 // }
296 
297 //
298 // member functions
299 //
300 bool
302 {
303  return nullptr!=m_file.get();
304 }
305 
306 void
308 {
309  //NOTE: I need to also set the I/O performance settings
310 
311  m_file = std::auto_ptr<TFile>(new TFile(m_fileName.c_str(),"RECREATE",
312  "1" //This is the file format version number
313  ));
314 
316  cms::Digest branchHash;
319  std::string(),
320  "DQMRootOutputModule",
321  description().moduleLabel(),
323  std::string(),
324  branchHash.digest().toString(),
325  std::vector<std::string>()
326  );
327 
328 
330  m_indicesTree->Branch(kRunBranch,&m_run);
331  m_indicesTree->Branch(kLumiBranch,&m_lumi);
335  m_indicesTree->Branch(kTypeBranch,&m_type);
338  m_indicesTree->SetDirectory(m_file.get());
339 
340  unsigned int i = 0;
341  for(std::vector<boost::shared_ptr<TreeHelperBase> >::iterator it = m_treeHelpers.begin(), itEnd = m_treeHelpers.end();
342  it != itEnd;
343  ++it,++i) {
344  //std::cout <<"making "<<kTypeNames[i]<<std::endl;
345  TTree* tree = new TTree(kTypeNames[i],kTypeNames[i]);
346  *it = boost::shared_ptr<TreeHelperBase>(makeHelper(i,tree,m_fullNameBufferPtr));
347  tree->SetDirectory(m_file.get()); //TFile takes ownership
348  }
349 
362 }
363 
364 
365 void
367 
368 }
369 void
371  //std::cout << "DQMRootOutputModule::writeLuminosityBlock"<< std::endl;
372  edm::Service<DQMStore> dstore;
373  m_run=iLumi.id().run();
374  m_lumi = iLumi.id().value();
375  m_beginTime = iLumi.beginTime().value();
376  m_endTime = iLumi.endTime().value();
377  bool shouldWrite = (m_filterOnRun == 0 ||
378  (m_filterOnRun != 0 && m_filterOnRun == m_run));
379 
380  if (! shouldWrite)
381  return;
382  std::vector<MonitorElement *> items(dstore->getAllContents(""));
383  for(std::vector<MonitorElement*>::iterator it = items.begin(), itEnd=items.end();
384  it!=itEnd;
385  ++it) {
386  if((*it)->getLumiFlag()) {
387  std::map<unsigned int,unsigned int>::iterator itFound = m_dqmKindToTypeIndex.find((*it)->kind());
388  assert(itFound !=m_dqmKindToTypeIndex.end());
389  m_treeHelpers[itFound->second]->fill(*it);
390  }
391  }
392 
394  std::vector<edm::ProcessHistoryID>::iterator itFind = std::find(m_seenHistories.begin(),m_seenHistories.end(),id);
395  if(itFind == m_seenHistories.end()) {
397  m_seenHistories.push_back(id);
398  } else {
399  m_presentHistoryIndex = itFind - m_seenHistories.begin();
400  }
401 
402  //Now store the relationship between run/lumi and indices in the other TTrees
403  bool storedLumiIndex = false;
404  unsigned int typeIndex = 0;
405  for(std::vector<boost::shared_ptr<TreeHelperBase> >::iterator it = m_treeHelpers.begin(), itEnd = m_treeHelpers.end();
406  it != itEnd;
407  ++it,++typeIndex) {
408  if((*it)->wasFilled()) {
409  m_type = typeIndex;
410  (*it)->getRangeAndReset(m_firstIndex,m_lastIndex);
411  storedLumiIndex = true;
412  m_indicesTree->Fill();
413  }
414  }
415  if(not storedLumiIndex) {
416  //need to record lumis even if we stored no MonitorElements since some later DQM modules
417  // look to see what lumis were processed
419  m_firstIndex=0;
420  m_lastIndex=0;
421  m_indicesTree->Fill();
422  }
423 
426 }
427 
428 
430  //std::cout << "DQMRootOutputModule::writeRun"<< std::endl;
431  edm::Service<DQMStore> dstore;
432  m_run=iRun.id().run();
433  m_lumi = 0;
434  m_beginTime = iRun.beginTime().value();
435  m_endTime = iRun.endTime().value();
436  bool shouldWrite = (m_filterOnRun == 0 ||
437  (m_filterOnRun != 0 && m_filterOnRun == m_run));
438 
439  if (! shouldWrite)
440  return;
441 
442  std::vector<MonitorElement *> items(dstore->getAllContents(""));
443  for(std::vector<MonitorElement*>::iterator it = items.begin(), itEnd=items.end();
444  it!=itEnd;
445  ++it) {
446  if(not (*it)->getLumiFlag()) {
447  std::map<unsigned int,unsigned int>::iterator itFound = m_dqmKindToTypeIndex.find((*it)->kind());
448  assert (itFound !=m_dqmKindToTypeIndex.end());
449  m_treeHelpers[itFound->second]->fill(*it);
450  }
451  }
452 
454  std::vector<edm::ProcessHistoryID>::iterator itFind = std::find(m_seenHistories.begin(),m_seenHistories.end(),id);
455  if(itFind == m_seenHistories.end()) {
457  m_seenHistories.push_back(id);
458  } else {
459  m_presentHistoryIndex = itFind - m_seenHistories.begin();
460  }
461 
462  //Now store the relationship between run/lumi and indices in the other TTrees
463  unsigned int typeIndex = 0;
464  for(std::vector<boost::shared_ptr<TreeHelperBase> >::iterator it = m_treeHelpers.begin(), itEnd = m_treeHelpers.end();
465  it != itEnd;
466  ++it,++typeIndex) {
467  if((*it)->wasFilled()) {
468  m_type = typeIndex;
469  (*it)->getRangeAndReset(m_firstIndex,m_lastIndex);
470  m_indicesTree->Fill();
471  }
472  }
473 
475  jr->reportRunNumber(m_run);
476 }
477 
479  //std::cout << "DQMRootOutputModule::startEndFile"<< std::endl;
480  //fill in the meta data
481  m_file->cd();
482  TDirectory* metaDataDirectory = m_file->mkdir(kMetaDataDirectory);
483 
484 
485  //Write out the Process History
486  TTree* processHistoryTree = new TTree(kProcessHistoryTree,kProcessHistoryTree);
487  processHistoryTree->SetDirectory(metaDataDirectory);
488 
489  unsigned int index = 0;
490  processHistoryTree->Branch(kPHIndexBranch,&index);
492  processHistoryTree->Branch(kProcessConfigurationProcessNameBranch,&processName);
493  std::string parameterSetID;
494  processHistoryTree->Branch(kProcessConfigurationParameterSetIDBranch,&parameterSetID);
495  std::string releaseVersion;
496  processHistoryTree->Branch(kProcessConfigurationReleaseVersion,&releaseVersion);
497  std::string passID;
498  processHistoryTree->Branch(kProcessConfigurationPassID,&passID);
499 
501  assert(0!=phr);
502  for(std::vector<edm::ProcessHistoryID>::iterator it = m_seenHistories.begin(), itEnd = m_seenHistories.end();
503  it !=itEnd;
504  ++it) {
505  const edm::ProcessHistory* history = phr->getMapped(*it);
506  assert(0!=history);
507  index = 0;
508  for(edm::ProcessHistory::collection_type::const_iterator itPC = history->begin(), itPCEnd = history->end();
509  itPC != itPCEnd;
510  ++itPC,++index) {
511  processName = itPC->processName();
512  releaseVersion = itPC->releaseVersion();
513  passID = itPC->passID();
514  parameterSetID = itPC->parameterSetID().compactForm();
515  processHistoryTree->Fill();
516  }
517  }
518 
519  //Store the ParameterSets
520  TTree* parameterSetsTree = new TTree(kParameterSetTree,kParameterSetTree);
521  parameterSetsTree->SetDirectory(metaDataDirectory);
522  std::string blob;
523  parameterSetsTree->Branch(kParameterSetBranch,&blob);
524 
526  assert(0!=psr);
527  for(edm::pset::Registry::const_iterator it = psr->begin(), itEnd = psr->end();
528  it != itEnd;
529  ++it) {
530  blob.clear();
531  it->second.toString(blob);
532  parameterSetsTree->Fill();
533  }
534 
535 }
536 
538  //std::cout << "DQMRootOutputModule::finishEndFile"<< std::endl;
539  m_file->Write();
540  m_file->Close();
543 }
544 
545 //
546 // const member functions
547 //
548 
549 //
550 // static member functions
551 //
552 void
554  //The following says we do not know what parameters are allowed so do no validation
555  // Please change this to state exactly what you do use, even if it is no parameters
557  desc.setUnknown();
558  descriptions.addDefault(desc);
559 
560  //NOTE: when actually filling this in, do not forget to add a untracked PSet 'dataset'
561  // which is used for bookkeeping by the DMWM
562 }
563 
564 
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 &)
static ThreadSafeRegistry * instance()
#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:60
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: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:1677
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:129
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
doFill
Definition: cuy.py:574
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
std::string createGlobalIdentifier()
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