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; }
TH1Store::TH1Store
TH1Store()
Definition: TH1Store.cc:20
TH1Store::sm_verbose
static bool sm_verbose
Definition: TH1Store.h:101
funct::false
false
Definition: Factorize.h:29
TH1Store::SVec
std::vector< std::string > SVec
Definition: TH1Store.h:21
TH1Store::m_ptrMap
STH1PtrMap m_ptrMap
Definition: TH1Store.h:94
TH1Store.h
operator<<
ostream & operator<<(ostream &o_stream, const TH1Store &rhs)
Definition: TH1Store.cc:121
gather_cfg.cout
cout
Definition: gather_cfg.py:144
oniaPATMuonsWithTrigger_cff.matches
matches
Definition: oniaPATMuonsWithTrigger_cff.py:77
cms::cuda::assert
assert(be >=bs)
TH1Store::hist
TH1 * hist(const std::string &name)
TH1Store::STH1PtrMapConstIter
STH1PtrMap::const_iterator STH1PtrMapConstIter
Definition: TH1Store.h:26
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
TH1Store
Definition: TH1Store.h:15
TH1Store::~TH1Store
~TH1Store()
Definition: TH1Store.cc:22
TH1Store::STH1PtrMapIter
STH1PtrMap::iterator STH1PtrMapIter
Definition: TH1Store.h:25
TH1Store::m_deleteOnDestruction
bool m_deleteOnDestruction
Definition: TH1Store.h:93
TH1Store::_createDir
TDirectory * _createDir(const std::string &dirname, TFile *filePtr) const
Definition: TH1Store.cc:95
TH1Store::m_nameDirMap
SSMap m_nameDirMap
Definition: TH1Store.h:95
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
std
Definition: JetResolutionObject.h:76
TH1Store::kEmptyVec
static const SVec kEmptyVec
Definition: TH1Store.h:28
TH1Store::write
void write(const std::string &filename, const SVec &argsVec=kEmptyVec, const SVec &inputFilesVec=kEmptyVec) const
TH1Store::SSMapConstIter
SSMap::const_iterator SSMapConstIter
Definition: TH1Store.h:24
createBeamHaloJobs.directory
string directory
Definition: createBeamHaloJobs.py:211
TrackerOfflineValidation_Dqm_cff.dirName
dirName
Definition: TrackerOfflineValidation_Dqm_cff.py:55
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
TH1Store::add
void add(TH1 *histPtr, const std::string &directory="")
Definition: TH1Store.cc:30
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23