Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:23

0001 // -*- C++ -*-
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 // Static Member Data Declaration //
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     }  // for iter
0027   }    // if destroying pointers
0028 }
0029 
0030 void TH1Store::add(TH1 *histPtr, const std::string &directory) {
0031   // Do we have a histogram with this name already?
0032   string name = histPtr->GetName();
0033   if (m_ptrMap.end() != m_ptrMap.find(name)) {
0034     //  D'oh
0035     cerr << "TH1Store::add() Error: '" << name << "' already exists.  Aborting." << endl;
0036     assert(0);
0037   }  // if already exists
0038   if (sm_verbose) {
0039     cout << "THStore::add() : Adding " << name << endl;
0040   }
0041   m_ptrMap[name] = histPtr;
0042   histPtr->SetDirectory(nullptr);
0043   if (directory.length()) {
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     //  D'oh
0052     cerr << "TH1Store::hist() Error: '" << name << "' does not exists.  Aborting." << endl;
0053     assert(0);
0054   }  // doesn't exist
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   // write out all histograms
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       // we want a subdirectory for this one
0075       _createDir(nameDirIter->second, filePtr);
0076     } else {
0077       // we don't need a subdirectory, just save this in the main
0078       // directory.
0079       filePtr->cd();
0080     }
0081     iter->second->Write();
0082   }  // for iter
0083   // Write out command line arguments.  Save information in directory
0084   // called provenance.
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   // do we have this one already
0097   TDirectory *dirPtr = filePtr->GetDirectory(dirName.c_str());
0098   if (dirPtr) {
0099     dirPtr->cd();
0100     return dirPtr;
0101   }
0102   // if we're here, then this directory doesn't exist.  Is this
0103   // directory a subdirectory?
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     // This is not a subdirectory, so we're golden
0113     parentDir = filePtr;
0114   }
0115   dirPtr = parentDir->mkdir(useName.c_str());
0116   dirPtr->cd();
0117   return dirPtr;
0118 }
0119 
0120 // friends
0121 ostream &operator<<(ostream &o_stream, const TH1Store &rhs) { return o_stream; }