CMS 3D CMS Logo

TH1Store.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 #include <iostream>
3 #include <fstream>
4 #include <iomanip>
5 #include <cassert>
6 
7 #include "boost/regex.hpp"
8 
10 
11 using namespace std;
12 
14 // Static Member Data Declaration //
16 
18 bool TH1Store::sm_verbose = false;
19 
20 TH1Store::TH1Store() : m_deleteOnDestruction(false) {}
21 
24  for (STH1PtrMapIter iter = m_ptrMap.begin(); m_ptrMap.end() != iter; ++iter) {
25  delete iter->second;
26  } // for iter
27  } // if destroying pointers
28 }
29 
30 void TH1Store::add(TH1 *histPtr, const std::string &directory) {
31  // Do we have a histogram with this name already?
32  string name = histPtr->GetName();
33  if (m_ptrMap.end() != m_ptrMap.find(name)) {
34  // D'oh
35  cerr << "TH1Store::add() Error: '" << name << "' already exists. Aborting." << endl;
36  assert(0);
37  } // if already exists
38  if (sm_verbose) {
39  cout << "THStore::add() : Adding " << name << endl;
40  }
41  m_ptrMap[name] = histPtr;
42  histPtr->SetDirectory(nullptr);
43  if (directory.length()) {
45  }
46 }
47 
48 TH1 *TH1Store::hist(const string &name) {
49  STH1PtrMapIter iter = m_ptrMap.find(name);
50  if (m_ptrMap.end() == iter) {
51  // D'oh
52  cerr << "TH1Store::hist() Error: '" << name << "' does not exists. Aborting." << endl;
53  assert(0);
54  } // doesn't exist
55  return iter->second;
56 }
57 
58 void TH1Store::write(const string &filename, const SVec &argsVec, const SVec &inputFilesVec) const {
59  TFile *filePtr = TFile::Open(filename.c_str(), "RECREATE");
60  if (!filePtr) {
61  cerr << "TH1Store::write() Error: Can not open '" << filename << "' for output. Aborting." << endl;
62  assert(0);
63  }
64  write(filePtr, argsVec, inputFilesVec);
65  delete filePtr;
66 }
67 
68 void TH1Store::write(TFile *filePtr, const SVec &argsVec, const SVec &inputFilesVec) const {
69  filePtr->cd();
70  // write out all histograms
71  for (STH1PtrMapConstIter iter = m_ptrMap.begin(); m_ptrMap.end() != iter; ++iter) {
72  SSMapConstIter nameDirIter = m_nameDirMap.find(iter->first);
73  if (m_nameDirMap.end() != nameDirIter) {
74  // we want a subdirectory for this one
75  _createDir(nameDirIter->second, filePtr);
76  } else {
77  // we don't need a subdirectory, just save this in the main
78  // directory.
79  filePtr->cd();
80  }
81  iter->second->Write();
82  } // for iter
83  // Write out command line arguments. Save information in directory
84  // called provenance.
85  TDirectory *dir = _createDir("args", filePtr);
86  if (!argsVec.empty()) {
87  dir->WriteObject(&argsVec, "argsVec");
88  }
89  if (!inputFilesVec.empty()) {
90  dir->WriteObject(&inputFilesVec, "inputFiles");
91  }
92  cout << "TH1Store::write(): Successfully written to '" << filePtr->GetName() << "'." << endl;
93 }
94 
95 TDirectory *TH1Store::_createDir(const string &dirName, TFile *filePtr) const {
96  // do we have this one already
97  TDirectory *dirPtr = filePtr->GetDirectory(dirName.c_str());
98  if (dirPtr) {
99  dirPtr->cd();
100  return dirPtr;
101  }
102  // if we're here, then this directory doesn't exist. Is this
103  // directory a subdirectory?
104  const boost::regex subdirRE("(.+?)/([^/]+)");
105  boost::smatch matches;
106  TDirectory *parentDir = nullptr;
107  string useName = dirName;
108  if (boost::regex_match(dirName, matches, subdirRE)) {
109  parentDir = _createDir(matches[1], filePtr);
110  useName = matches[2];
111  } else {
112  // This is not a subdirectory, so we're golden
113  parentDir = filePtr;
114  }
115  dirPtr = parentDir->mkdir(useName.c_str());
116  dirPtr->cd();
117  return dirPtr;
118 }
119 
120 // friends
121 ostream &operator<<(ostream &o_stream, const TH1Store &rhs) { return o_stream; }
bool m_deleteOnDestruction
Definition: TH1Store.h:93
SSMap m_nameDirMap
Definition: TH1Store.h:95
void add(TH1 *histPtr, const std::string &directory="")
Definition: TH1Store.cc:30
TDirectory * _createDir(const std::string &dirname, TFile *filePtr) const
Definition: TH1Store.cc:95
static bool sm_verbose
Definition: TH1Store.h:101
~TH1Store()
Definition: TH1Store.cc:22
static const SVec kEmptyVec
Definition: TH1Store.h:28
void write(const std::string &filename, const SVec &argsVec=kEmptyVec, const SVec &inputFilesVec=kEmptyVec) const
STH1PtrMap::const_iterator STH1PtrMapConstIter
Definition: TH1Store.h:26
STH1PtrMap m_ptrMap
Definition: TH1Store.h:94
friend std::ostream & operator<<(std::ostream &o_stream, const TH1Store &rhs)
STH1PtrMap::iterator STH1PtrMapIter
Definition: TH1Store.h:25
std::vector< std::string > SVec
Definition: TH1Store.h:21
TH1Store()
Definition: TH1Store.cc:20
SSMap::const_iterator SSMapConstIter
Definition: TH1Store.h:24
TH1 * hist(const std::string &name)