CMS 3D CMS Logo

EventContainer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 #include <iostream>
3 #include <fstream>
4 #include <iomanip>
5 #include <cassert>
6 
11 
12 #include "TH1.h"
13 
14 using namespace std;
15 using namespace fwlite;
16 
18 // Static Member Data Declaration //
20 
21 bool EventContainer::sm_autoloaderCalled = false;
22 
23 
24 EventContainer::EventContainer (optutl::CommandLineParser &parser,
25  FuncPtr funcPtr) :
26  m_eventsSeen (0), m_maxWanted (0), m_parserPtr (nullptr)
27 {
28  // get the user-defined tag
29  string tag;
30  if (funcPtr)
31  {
32  (*funcPtr) (tag);
33  }
34 
35  // finish defaultt options and create fwlite::Event
36  parser._finishDefaultOptions (tag);
37 
38  // Call the autoloader if not already called.
39  if (! sm_autoloaderCalled)
40  {
42  sm_autoloaderCalled = true;
43  }
44 
45  const optutl::CommandLineParser::SVec &secondaryInputFiles =
46  parser.stringVector ("secondaryInputFiles");
47  if (!secondaryInputFiles.empty())
48  {
50  new fwlite::MultiChainEvent( parser.stringVector ("inputFiles"),
51  secondaryInputFiles,
52  parser.boolValue("orderedSecondaryFiles") );
53  } else {
55  new fwlite::ChainEvent( parser.stringVector ("inputFiles") );
56  }
57 
58  // get whatever other info you want
59  m_outputName = parser.stringValue ("outputFile");
60  m_maxWanted = parser.integerValue ("maxEvents");
61  m_outputEvery = parser.integerValue ("outputEvery");
62 
63  // remember my parser
65 
66  // TH1::AddDirectory(false);
67 }
68 
70 {
71  // if the pointer is non-zero, then we should run the standard
72  // destructor. If it is zero, then we should do nothing
73  if (! m_eventBasePtr)
74  {
75  return;
76  }
77  // If we're still here, let's get to work.
78  cout << "EventContainer Summary: Processed "
79  << m_eventsSeen << " events." << endl;
82  parser.hasOption("inputFiles"))
83  {
85  parser.argVec(),
86  parser.stringVector ("inputFiles"));
87  } else {
89  parser.argVec());
90  }
91  delete m_eventBasePtr;
92 }
93 
94 void
95 EventContainer::add (TH1 *histPtr, const string &directory)
96 {
97  m_histStore.add (histPtr, directory);
98 }
99 
102 {
103  assert (m_parserPtr);
104  return *m_parserPtr;
105 }
106 
107 TH1*
108 EventContainer::hist (const string &name)
109 {
110  return m_histStore.hist (name);
111 }
112 
113 bool
114 EventContainer::getByLabel (const std::type_info& iInfo,
115  const char* iModuleLabel,
116  const char* iProductInstanceLabel,
117  const char* iProcessLabel,
118  void* oData) const
119 {
120  assert (m_eventBasePtr);
121  return m_eventBasePtr->getByLabel( iInfo,
122  iModuleLabel,
123  iProductInstanceLabel,
124  iProcessLabel,
125  oData );
126 }
127 
128 const std::string
129 EventContainer::getBranchNameFor (const std::type_info& iInfo,
130  const char* iModuleLabel,
131  const char* iProductInstanceLabel,
132  const char* iProcessLabel) const
133 {
134  assert (m_eventBasePtr);
135  return m_eventBasePtr->getBranchNameFor( iInfo,
136  iModuleLabel,
137  iProductInstanceLabel,
138  iProcessLabel );
139 }
140 
141 const EventContainer&
143 {
144  assert (m_eventBasePtr);
145 
146  m_eventBasePtr->operator++();
147  ++m_eventsSeen;
148  if (m_outputEvery && m_eventsSeen % m_outputEvery == 0 )
149  {
150  cout << "Processing Event: " << m_eventsSeen << endl;
151  }
152  return *this;
153 }
154 
155 const EventContainer&
157 {
158  assert (m_eventBasePtr);
159  m_eventsSeen = 0;
161 
162  // If we're going to skip over any events, do it here.
163 
164  // O.k. We should be good to go.
165  return *this;
166 }
167 
168 bool
170 {
171  assert (m_eventBasePtr);
172  // first check to see that we haven't already processed the maxinum
173  // number of events that we asked for.
175  {
176  // we're done
177  return true;
178  }
179 
180  return m_eventBasePtr->atEnd();
181 }
182 
183 
184 // friends
185 ostream& operator<< (ostream& o_stream, const EventContainer &rhs)
186 {
187  return o_stream;
188 }
void _finishDefaultOptions(std::string tag="")
const EventContainer & operator++() override
void add(TH1 *histPtr, const std::string &directory="")
Definition: TH1Store.cc:39
std::string & stringValue(std::string key)
std::vector< std::string > SVec
#define nullptr
const EventContainer & toBegin() override
bool getByLabel(const std::type_info &iInfo, const char *iModuleLabel, const char *iProductInstanceLabel, const char *iProcessLabel, void *oData) const override
static bool sm_autoloaderCalled
virtual std::string const getBranchNameFor(std::type_info const &, char const *, char const *, char const *) const =0
virtual bool getByLabel(std::type_info const &, char const *, char const *, char const *, void *) const =0
void write(const std::string &filename, const SVec &argsVec=kEmptyVec, const SVec &inputFilesVec=kEmptyVec) const
int & integerValue(std::string key)
fwlite::EventBase * m_eventBasePtr
virtual bool atEnd() const =0
static void enable()
enable automatic library loading
void add(TH1 *histPtr, const std::string &directory="")
optutl::CommandLineParser * m_parserPtr
const SVec argVec() const
SVec & stringVector(std::string key)
bool atEnd() const override
const std::string getBranchNameFor(const std::type_info &iInfo, const char *iModuleLabel, const char *iProductInstanceLabel, const char *iProcessLabel) const override
bool & boolValue(std::string key)
TH1 * hist(const std::string &name)
TH1 * hist(const std::string &name)
virtual EventBase const & toBegin()=0
friend std::ostream & operator<<(std::ostream &o_stream, const EventContainer &rhs)
optutl::CommandLineParser & parser()
OptionType hasOption(std::string key)