File indexing completed on 2024-12-20 03:14:00
0001
0002 #include <iostream>
0003 #include <fstream>
0004 #include <iomanip>
0005 #include <cassert>
0006
0007 #include "boost/regex.hpp"
0008
0009 #include "PhysicsTools/FWLite/interface/TH1Store.h"
0010
0011 using namespace std;
0012
0013
0014
0015
0016
0017 const TH1Store::SVec TH1Store::kEmptyVec;
0018 bool TH1Store::sm_verbose = false;
0019
0020 TH1Store::TH1Store() : m_deleteOnDestruction(false) {}
0021
0022 TH1Store::~TH1Store() {
0023 if (m_deleteOnDestruction) {
0024 for (STH1PtrMapIter iter = m_ptrMap.begin(); m_ptrMap.end() != iter; ++iter) {
0025 delete iter->second;
0026 }
0027 }
0028 }
0029
0030 void TH1Store::add(TH1 *histPtr, const std::string &directory) {
0031
0032 string name = histPtr->GetName();
0033 if (m_ptrMap.end() != m_ptrMap.find(name)) {
0034
0035 cerr << "TH1Store::add() Error: '" << name << "' already exists. Aborting." << endl;
0036 assert(0);
0037 }
0038 if (sm_verbose) {
0039 cout << "THStore::add() : Adding " << name << endl;
0040 }
0041 m_ptrMap[name] = histPtr;
0042 histPtr->SetDirectory(nullptr);
0043 if (!directory.empty()) {
0044 m_nameDirMap[name] = directory;
0045 }
0046 }
0047
0048 TH1 *TH1Store::hist(const string &name) {
0049 STH1PtrMapIter iter = m_ptrMap.find(name);
0050 if (m_ptrMap.end() == iter) {
0051
0052 cerr << "TH1Store::hist() Error: '" << name << "' does not exists. Aborting." << endl;
0053 assert(0);
0054 }
0055 return iter->second;
0056 }
0057
0058 void TH1Store::write(const string &filename, const SVec &argsVec, const SVec &inputFilesVec) const {
0059 TFile *filePtr = TFile::Open(filename.c_str(), "RECREATE");
0060 if (!filePtr) {
0061 cerr << "TH1Store::write() Error: Can not open '" << filename << "' for output. Aborting." << endl;
0062 assert(0);
0063 }
0064 write(filePtr, argsVec, inputFilesVec);
0065 delete filePtr;
0066 }
0067
0068 void TH1Store::write(TFile *filePtr, const SVec &argsVec, const SVec &inputFilesVec) const {
0069 filePtr->cd();
0070
0071 for (STH1PtrMapConstIter iter = m_ptrMap.begin(); m_ptrMap.end() != iter; ++iter) {
0072 SSMapConstIter nameDirIter = m_nameDirMap.find(iter->first);
0073 if (m_nameDirMap.end() != nameDirIter) {
0074
0075 _createDir(nameDirIter->second, filePtr);
0076 } else {
0077
0078
0079 filePtr->cd();
0080 }
0081 iter->second->Write();
0082 }
0083
0084
0085 TDirectory *dir = _createDir("args", filePtr);
0086 if (!argsVec.empty()) {
0087 dir->WriteObject(&argsVec, "argsVec");
0088 }
0089 if (!inputFilesVec.empty()) {
0090 dir->WriteObject(&inputFilesVec, "inputFiles");
0091 }
0092 cout << "TH1Store::write(): Successfully written to '" << filePtr->GetName() << "'." << endl;
0093 }
0094
0095 TDirectory *TH1Store::_createDir(const string &dirName, TFile *filePtr) const {
0096
0097 TDirectory *dirPtr = filePtr->GetDirectory(dirName.c_str());
0098 if (dirPtr) {
0099 dirPtr->cd();
0100 return dirPtr;
0101 }
0102
0103
0104 const boost::regex subdirRE("(.+?)/([^/]+)");
0105 boost::smatch matches;
0106 TDirectory *parentDir = nullptr;
0107 string useName = dirName;
0108 if (boost::regex_match(dirName, matches, subdirRE)) {
0109 parentDir = _createDir(matches[1], filePtr);
0110 useName = matches[2];
0111 } else {
0112
0113 parentDir = filePtr;
0114 }
0115 dirPtr = parentDir->mkdir(useName.c_str());
0116 dirPtr->cd();
0117 return dirPtr;
0118 }
0119
0120
0121 ostream &operator<<(ostream &o_stream, const TH1Store &rhs) { return o_stream; }