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 "FWCore/FWLite/interface/FWLiteEnabler.h"
0008 #include "PhysicsTools/FWLite/interface/EventContainer.h"
0009 #include "PhysicsTools/FWLite/interface/dout.h"
0010 #include "DataFormats/FWLite/interface/MultiChainEvent.h"
0011 
0012 #include "TH1.h"
0013 
0014 using namespace std;
0015 using namespace fwlite;
0016 
0017 ////////////////////////////////////
0018 // Static Member Data Declaration //
0019 ////////////////////////////////////
0020 
0021 bool EventContainer::sm_autoloaderCalled = false;
0022 
0023 EventContainer::EventContainer(optutl::CommandLineParser& parser, FuncPtr funcPtr)
0024     : m_eventsSeen(0), m_maxWanted(0), m_parserPtr(nullptr) {
0025   // get the user-defined tag
0026   string tag;
0027   if (funcPtr) {
0028     (*funcPtr)(tag);
0029   }
0030 
0031   // finish defaultt options and create fwlite::Event
0032   parser._finishDefaultOptions(tag);
0033 
0034   // Call the autoloader if not already called.
0035   if (!sm_autoloaderCalled) {
0036     FWLiteEnabler::enable();
0037     sm_autoloaderCalled = true;
0038   }
0039 
0040   const optutl::CommandLineParser::SVec& secondaryInputFiles = parser.stringVector("secondaryInputFiles");
0041   if (!secondaryInputFiles.empty()) {
0042     m_eventBasePtr = new fwlite::MultiChainEvent(
0043         parser.stringVector("inputFiles"), secondaryInputFiles, parser.boolValue("orderedSecondaryFiles"));
0044   } else {
0045     m_eventBasePtr = new fwlite::ChainEvent(parser.stringVector("inputFiles"));
0046   }
0047 
0048   // get whatever other info you want
0049   m_outputName = parser.stringValue("outputFile");
0050   m_maxWanted = parser.integerValue("maxEvents");
0051   m_outputEvery = parser.integerValue("outputEvery");
0052 
0053   // remember my parser
0054   m_parserPtr = &parser;
0055 
0056   // TH1::AddDirectory(false);
0057 }
0058 
0059 EventContainer::~EventContainer() {
0060   // if the pointer is non-zero, then we should run the standard
0061   // destructor.  If it is zero, then we should do nothing
0062   if (!m_eventBasePtr) {
0063     return;
0064   }
0065   // If we're still here, let's get to work.
0066   cout << "EventContainer Summary: Processed " << m_eventsSeen << " events." << endl;
0067   optutl::CommandLineParser& parser = this->parser();
0068   if (optutl::CommandLineParser::kStringVector == parser.hasOption("inputFiles")) {
0069     m_histStore.write(m_outputName, parser.argVec(), parser.stringVector("inputFiles"));
0070   } else {
0071     m_histStore.write(m_outputName, parser.argVec());
0072   }
0073   delete m_eventBasePtr;
0074 }
0075 
0076 void EventContainer::add(TH1* histPtr, const string& directory) { m_histStore.add(histPtr, directory); }
0077 
0078 optutl::CommandLineParser& EventContainer::parser() {
0079   assert(m_parserPtr);
0080   return *m_parserPtr;
0081 }
0082 
0083 TH1* EventContainer::hist(const string& name) { return m_histStore.hist(name); }
0084 
0085 bool EventContainer::getByLabel(const std::type_info& iInfo,
0086                                 const char* iModuleLabel,
0087                                 const char* iProductInstanceLabel,
0088                                 const char* iProcessLabel,
0089                                 void* oData) const {
0090   assert(m_eventBasePtr);
0091   return m_eventBasePtr->getByLabel(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel, oData);
0092 }
0093 
0094 const std::string EventContainer::getBranchNameFor(const std::type_info& iInfo,
0095                                                    const char* iModuleLabel,
0096                                                    const char* iProductInstanceLabel,
0097                                                    const char* iProcessLabel) const {
0098   assert(m_eventBasePtr);
0099   return m_eventBasePtr->getBranchNameFor(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel);
0100 }
0101 
0102 const EventContainer& EventContainer::operator++() {
0103   assert(m_eventBasePtr);
0104 
0105   m_eventBasePtr->operator++();
0106   ++m_eventsSeen;
0107   if (m_outputEvery && m_eventsSeen % m_outputEvery == 0) {
0108     cout << "Processing Event: " << m_eventsSeen << endl;
0109   }
0110   return *this;
0111 }
0112 
0113 const EventContainer& EventContainer::toBegin() {
0114   assert(m_eventBasePtr);
0115   m_eventsSeen = 0;
0116   m_eventBasePtr->toBegin();
0117 
0118   // If we're going to skip over any events, do it here.
0119 
0120   // O.k.  We should be good to go.
0121   return *this;
0122 }
0123 
0124 bool EventContainer::atEnd() const {
0125   assert(m_eventBasePtr);
0126   // first check to see that we haven't already processed the maxinum
0127   // number of events that we asked for.
0128   if (m_maxWanted && m_eventsSeen >= m_maxWanted) {
0129     // we're done
0130     return true;
0131   }
0132 
0133   return m_eventBasePtr->atEnd();
0134 }
0135 
0136 // friends
0137 ostream& operator<<(ostream& o_stream, const EventContainer& rhs) { return o_stream; }