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 
21 TH1Store::TH1Store() : m_deleteOnDestruction (false)
22 {
23 }
24 
26 {
28  {
29  for (STH1PtrMapIter iter = m_ptrMap.begin();
30  m_ptrMap.end() != iter;
31  ++iter)
32  {
33  delete iter->second;
34  } // for iter
35  } // if destroying pointers
36 }
37 
38 void
39 TH1Store::add (TH1 *histPtr, const std::string &directory)
40 {
41  // Do we have a histogram with this name already?
42  string name = histPtr->GetName();
43  if (m_ptrMap.end() != m_ptrMap.find (name))
44  {
45  // D'oh
46  cerr << "TH1Store::add() Error: '" << name
47  << "' already exists. Aborting." << endl;
48  assert (0);
49  } // if already exists
50  if (sm_verbose)
51  {
52  cout << "THStore::add() : Adding " << name << endl;
53  }
54  m_ptrMap[name] = histPtr;
55  histPtr->SetDirectory(nullptr);
56  if (directory.length())
57  {
59  }
60 }
61 
62 TH1*
63 TH1Store::hist (const string &name)
64 {
65  STH1PtrMapIter iter = m_ptrMap.find (name);
66  if (m_ptrMap.end() == iter)
67  {
68  // D'oh
69  cerr << "TH1Store::hist() Error: '" << name
70  << "' does not exists. Aborting." << endl;
71  assert (0);
72  } // doesn't exist
73  return iter->second;
74 }
75 
76 void
77 TH1Store::write (const string &filename,
78  const SVec &argsVec,
79  const SVec &inputFilesVec) const
80 {
81  TFile *filePtr = TFile::Open (filename.c_str(), "RECREATE");
82  if ( ! filePtr)
83  {
84  cerr << "TH1Store::write() Error: Can not open '"
85  << filename << "' for output. Aborting." << endl;
86  assert (0);
87  }
88  write (filePtr, argsVec, inputFilesVec);
89  delete filePtr;
90 }
91 
92 void
93 TH1Store::write (TFile *filePtr,
94  const SVec &argsVec,
95  const SVec &inputFilesVec) const
96 {
97  filePtr->cd();
98  // write out all histograms
99  for (STH1PtrMapConstIter iter = m_ptrMap.begin();
100  m_ptrMap.end() != iter;
101  ++iter)
102  {
103  SSMapConstIter nameDirIter = m_nameDirMap.find (iter->first);
104  if (m_nameDirMap.end() != nameDirIter)
105  {
106  // we want a subdirectory for this one
107  _createDir (nameDirIter->second, filePtr);
108  } else {
109  // we don't need a subdirectory, just save this in the main
110  // directory.
111  filePtr->cd();
112  }
113  iter->second->Write();
114  } // for iter
115  // Write out command line arguments. Save information in directory
116  // called provenance.
117  TDirectory *dir = _createDir ("args", filePtr);
118  if (!argsVec.empty())
119  {
120  dir->WriteObject (&argsVec, "argsVec");
121  }
122  if (!inputFilesVec.empty())
123  {
124  dir->WriteObject (&inputFilesVec, "inputFiles");
125  }
126  cout << "TH1Store::write(): Successfully written to '"
127  << filePtr->GetName() << "'." << endl;
128 }
129 
130 TDirectory*
131 TH1Store::_createDir (const string &dirName, TFile *filePtr) const
132 {
133  // do we have this one already
134  TDirectory *dirPtr = filePtr->GetDirectory (dirName.c_str());
135  if (dirPtr)
136  {
137  dirPtr->cd();
138  return dirPtr;
139  }
140  // if we're here, then this directory doesn't exist. Is this
141  // directory a subdirectory?
142  const boost::regex subdirRE ("(.+?)/([^/]+)");
143  boost::smatch matches;
144  TDirectory *parentDir = nullptr;
145  string useName = dirName;
146  if( boost::regex_match (dirName, matches, subdirRE) )
147  {
148  parentDir = _createDir (matches[1], filePtr);
149  useName = matches[2];
150  } else {
151  // This is not a subdirectory, so we're golden
152  parentDir = filePtr;
153  }
154  dirPtr = parentDir->mkdir (useName.c_str());
155  dirPtr->cd();
156  return dirPtr;
157 }
158 
159 // friends
160 ostream& operator<< (ostream& o_stream, const TH1Store &rhs)
161 {
162  return o_stream;
163 }
bool m_deleteOnDestruction
Definition: TH1Store.h:105
SSMap m_nameDirMap
Definition: TH1Store.h:107
void add(TH1 *histPtr, const std::string &directory="")
Definition: TH1Store.cc:39
TDirectory * _createDir(const std::string &dirname, TFile *filePtr) const
Definition: TH1Store.cc:131
static bool sm_verbose
Definition: TH1Store.h:113
~TH1Store()
Definition: TH1Store.cc:25
static const SVec kEmptyVec
Definition: TH1Store.h:30
void write(const std::string &filename, const SVec &argsVec=kEmptyVec, const SVec &inputFilesVec=kEmptyVec) const
STH1PtrMap::const_iterator STH1PtrMapConstIter
Definition: TH1Store.h:28
STH1PtrMap m_ptrMap
Definition: TH1Store.h:106
friend std::ostream & operator<<(std::ostream &o_stream, const TH1Store &rhs)
std::vector< std::string > SVec
Definition: TH1Store.h:23
STH1PtrMap::iterator STH1PtrMapIter
Definition: TH1Store.h:27
TH1Store()
Definition: TH1Store.cc:21
SSMap::const_iterator SSMapConstIter
Definition: TH1Store.h:26
TH1 * hist(const std::string &name)
dbl *** dir
Definition: mlp_gen.cc:35