CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/PhysicsTools/FWLite/src/TH1Store.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
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 // Static Member Data Declaration //
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       } // for iter
00035    } // if destroying pointers
00036 }
00037 
00038 void
00039 TH1Store::add (TH1 *histPtr, const std::string &directory)
00040 {
00041    // Do we have a histogram with this name already?
00042    string name = histPtr->GetName();
00043    if (m_ptrMap.end() != m_ptrMap.find (name))
00044    {
00045       //  D'oh
00046       cerr << "TH1Store::add() Error: '" << name 
00047            << "' already exists.  Aborting." << endl;
00048       assert (0);
00049    } // if already exists
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       //  D'oh
00069       cerr << "TH1Store::hist() Error: '" << name 
00070            << "' does not exists.  Aborting." << endl;
00071       assert (0);
00072    } // doesn't exist
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    // write out all histograms
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          // we want a subdirectory for this one
00107          _createDir (nameDirIter->second, filePtr);
00108       } else {
00109          // we don't need a subdirectory, just save this in the main
00110          // directory.
00111          filePtr->cd();         
00112       }
00113       iter->second->Write();
00114    } // for iter
00115    // Write out command line arguments.  Save information in directory
00116    // called provenance.
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    // do we have this one already
00134    TDirectory *dirPtr = filePtr->GetDirectory (dirName.c_str());
00135    if (dirPtr)
00136    {
00137       dirPtr->cd();
00138       return dirPtr;
00139    }
00140    // if we're here, then this directory doesn't exist.  Is this
00141    // directory a subdirectory?
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       // This is not a subdirectory, so we're golden
00152       parentDir = filePtr;
00153    }
00154    dirPtr = parentDir->mkdir (useName.c_str());
00155    dirPtr->cd();
00156    return dirPtr;
00157 }
00158 
00159 // friends
00160 ostream& operator<< (ostream& o_stream, const TH1Store &rhs)
00161 {
00162    return o_stream;
00163 }