Go to the documentation of this file.00001
00002 #include <iostream>
00003 #include <fstream>
00004 #include <iomanip>
00005 #include <cassert>
00006
00007 #include "boost/regex.hpp"
00008
00009 #include "PhysicsTools/FWLite/interface/TH1Store.h"
00010
00011 using namespace std;
00012
00014
00016
00017 const TH1Store::SVec TH1Store::kEmptyVec;
00018 bool TH1Store::sm_verbose = false;
00019
00020
00021 TH1Store::TH1Store() : m_deleteOnDestruction (false)
00022 {
00023 }
00024
00025 TH1Store::~TH1Store()
00026 {
00027 if (m_deleteOnDestruction)
00028 {
00029 for (STH1PtrMapIter iter = m_ptrMap.begin();
00030 m_ptrMap.end() != iter;
00031 ++iter)
00032 {
00033 delete iter->second;
00034 }
00035 }
00036 }
00037
00038 void
00039 TH1Store::add (TH1 *histPtr, const std::string &directory)
00040 {
00041
00042 string name = histPtr->GetName();
00043 if (m_ptrMap.end() != m_ptrMap.find (name))
00044 {
00045
00046 cerr << "TH1Store::add() Error: '" << name
00047 << "' already exists. Aborting." << endl;
00048 assert (0);
00049 }
00050 if (sm_verbose)
00051 {
00052 cout << "THStore::add() : Adding " << name << endl;
00053 }
00054 m_ptrMap[name] = histPtr;
00055 histPtr->SetDirectory(0);
00056 if (directory.length())
00057 {
00058 m_nameDirMap[name] = directory;
00059 }
00060 }
00061
00062 TH1*
00063 TH1Store::hist (const string &name)
00064 {
00065 STH1PtrMapIter iter = m_ptrMap.find (name);
00066 if (m_ptrMap.end() == iter)
00067 {
00068
00069 cerr << "TH1Store::hist() Error: '" << name
00070 << "' does not exists. Aborting." << endl;
00071 assert (0);
00072 }
00073 return iter->second;
00074 }
00075
00076 void
00077 TH1Store::write (const string &filename,
00078 const SVec &argsVec,
00079 const SVec &inputFilesVec) const
00080 {
00081 TFile *filePtr = TFile::Open (filename.c_str(), "RECREATE");
00082 if ( ! filePtr)
00083 {
00084 cerr << "TH1Store::write() Error: Can not open '"
00085 << filename << "' for output. Aborting." << endl;
00086 assert (0);
00087 }
00088 write (filePtr, argsVec, inputFilesVec);
00089 delete filePtr;
00090 }
00091
00092 void
00093 TH1Store::write (TFile *filePtr,
00094 const SVec &argsVec,
00095 const SVec &inputFilesVec) const
00096 {
00097 filePtr->cd();
00098
00099 for (STH1PtrMapConstIter iter = m_ptrMap.begin();
00100 m_ptrMap.end() != iter;
00101 ++iter)
00102 {
00103 SSMapConstIter nameDirIter = m_nameDirMap.find (iter->first);
00104 if (m_nameDirMap.end() != nameDirIter)
00105 {
00106
00107 _createDir (nameDirIter->second, filePtr);
00108 } else {
00109
00110
00111 filePtr->cd();
00112 }
00113 iter->second->Write();
00114 }
00115
00116
00117 TDirectory *dir = _createDir ("args", filePtr);
00118 if (argsVec.size())
00119 {
00120 dir->WriteObject (&argsVec, "argsVec");
00121 }
00122 if (inputFilesVec.size())
00123 {
00124 dir->WriteObject (&inputFilesVec, "inputFiles");
00125 }
00126 cout << "TH1Store::write(): Successfully written to '"
00127 << filePtr->GetName() << "'." << endl;
00128 }
00129
00130 TDirectory*
00131 TH1Store::_createDir (const string &dirName, TFile *filePtr) const
00132 {
00133
00134 TDirectory *dirPtr = filePtr->GetDirectory (dirName.c_str());
00135 if (dirPtr)
00136 {
00137 dirPtr->cd();
00138 return dirPtr;
00139 }
00140
00141
00142 const boost::regex subdirRE ("(.+?)/([^/]+)");
00143 boost::smatch matches;
00144 TDirectory *parentDir = 0;
00145 string useName = dirName;
00146 if( boost::regex_match (dirName, matches, subdirRE) )
00147 {
00148 parentDir = _createDir (matches[1], filePtr);
00149 useName = matches[2];
00150 } else {
00151
00152 parentDir = filePtr;
00153 }
00154 dirPtr = parentDir->mkdir (useName.c_str());
00155 dirPtr->cd();
00156 return dirPtr;
00157 }
00158
00159
00160 ostream& operator<< (ostream& o_stream, const TH1Store &rhs)
00161 {
00162 return o_stream;
00163 }