CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/PhysicsTools/FWLite/src/EventContainer.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 "FWCore/FWLite/interface/AutoLibraryLoader.h"
00008 #include "PhysicsTools/FWLite/interface/EventContainer.h"
00009 #include "PhysicsTools/FWLite/interface/dout.h"
00010 #include "DataFormats/FWLite/interface/MultiChainEvent.h"
00011 
00012 #include "TH1.h"
00013 
00014 using namespace std;
00015 using namespace fwlite;
00016 
00018 // Static Member Data Declaration //
00020 
00021 bool EventContainer::sm_autoloaderCalled = false;
00022 
00023 
00024 EventContainer::EventContainer (optutl::CommandLineParser &parser, 
00025                                 FuncPtr funcPtr) : 
00026    m_eventsSeen (0), m_maxWanted (0), m_parserPtr (0)
00027 {
00028    // get the user-defined tag
00029    string tag;
00030    if (funcPtr)
00031    {
00032       (*funcPtr) (tag);
00033    }
00034 
00035    // finish defaultt options and create fwlite::Event
00036    parser._finishDefaultOptions (tag);
00037 
00038    // Call the autoloader if not already called.
00039    if (! sm_autoloaderCalled)
00040    {
00041       AutoLibraryLoader::enable();
00042       sm_autoloaderCalled = true;      
00043    }
00044 
00045    const optutl::CommandLineParser::SVec &secondaryInputFiles = 
00046       parser.stringVector ("secondaryInputFiles");
00047    if (secondaryInputFiles.size())
00048    {
00049       m_eventBasePtr = 
00050          new fwlite::MultiChainEvent( parser.stringVector ("inputFiles"), 
00051                                       secondaryInputFiles,
00052                                       parser.boolValue("orderedSecondaryFiles") );
00053    } else {
00054       m_eventBasePtr = 
00055          new fwlite::ChainEvent( parser.stringVector ("inputFiles") );
00056    }
00057 
00058    // get whatever other info you want
00059    m_outputName  = parser.stringValue  ("outputFile");
00060    m_maxWanted   = parser.integerValue ("maxEvents");
00061    m_outputEvery = parser.integerValue ("outputEvery");
00062 
00063    // remember my parser
00064    m_parserPtr = &parser;
00065 
00066    // TH1::AddDirectory(false);
00067 }
00068 
00069 EventContainer::~EventContainer()
00070 {
00071    // if the pointer is non-zero, then we should run the standard
00072    // destructor.  If it is zero, then we should do nothing
00073    if (! m_eventBasePtr)
00074    {
00075       return;
00076    } 
00077    // If we're still here, let's get to work.
00078    cout << "EventContainer Summary: Processed "
00079         << m_eventsSeen << " events." << endl;
00080    optutl::CommandLineParser &parser = this->parser();
00081    if (optutl::CommandLineParser::kStringVector == 
00082        parser.hasOption("inputFiles"))
00083    {
00084       m_histStore.write (m_outputName, 
00085                          parser.argVec(),
00086                          parser.stringVector ("inputFiles"));
00087    } else {
00088       m_histStore.write (m_outputName, 
00089                          parser.argVec());
00090    }
00091    delete m_eventBasePtr;
00092 }
00093 
00094 void
00095 EventContainer::add (TH1 *histPtr, const string &directory)
00096 {
00097    m_histStore.add (histPtr, directory);
00098 }
00099 
00100 optutl::CommandLineParser &
00101 EventContainer::parser()
00102 {
00103    assert (m_parserPtr);
00104    return *m_parserPtr;
00105 }
00106 
00107 TH1*
00108 EventContainer::hist (const string &name)
00109 {
00110    return m_histStore.hist (name);
00111 }
00112 
00113 bool 
00114 EventContainer::getByLabel (const std::type_info& iInfo,
00115                             const char* iModuleLabel,
00116                             const char* iProductInstanceLabel,
00117                             const char* iProcessLabel,
00118                             void* oData) const
00119 {
00120    assert (m_eventBasePtr);
00121    return m_eventBasePtr->getByLabel( iInfo, 
00122                                       iModuleLabel, 
00123                                       iProductInstanceLabel,
00124                                       iProcessLabel, 
00125                                       oData );
00126 }
00127 
00128 const std::string 
00129 EventContainer::getBranchNameFor (const std::type_info& iInfo,
00130                                   const char* iModuleLabel,
00131                                   const char* iProductInstanceLabel,
00132                                   const char* iProcessLabel) const
00133 {
00134    assert (m_eventBasePtr);
00135    return m_eventBasePtr->getBranchNameFor( iInfo,
00136                                             iModuleLabel,
00137                                             iProductInstanceLabel,
00138                                             iProcessLabel );
00139 }
00140 
00141 const EventContainer& 
00142 EventContainer::operator++()
00143 {
00144    assert (m_eventBasePtr);
00145 
00146    m_eventBasePtr->operator++();
00147    ++m_eventsSeen;
00148    if (m_outputEvery && m_eventsSeen % m_outputEvery == 0 ) 
00149    {
00150       cout << "Processing Event: " << m_eventsSeen << endl;
00151    }
00152    return *this;   
00153 }
00154 
00155 const EventContainer& 
00156 EventContainer::toBegin()
00157 {
00158    assert (m_eventBasePtr);
00159    m_eventsSeen = 0;
00160    m_eventBasePtr->toBegin();
00161 
00162    // If we're going to skip over any events, do it here.
00163 
00164    // O.k.  We should be good to go.
00165    return *this;
00166 }
00167 
00168 bool
00169 EventContainer::atEnd() const
00170 {
00171    assert (m_eventBasePtr);
00172    // first check to see that we haven't already processed the maxinum
00173    // number of events that we asked for.
00174    if (m_maxWanted && m_eventsSeen >= m_maxWanted)
00175    {
00176       // we're done
00177       return true;
00178    }
00179 
00180    return m_eventBasePtr->atEnd();
00181 }
00182 
00183 
00184 // friends
00185 ostream& operator<< (ostream& o_stream, const EventContainer &rhs)
00186 {
00187    return o_stream;
00188 }