MarlinMT  0.1.0
LCIOOutputProcessor.cc
Go to the documentation of this file.
1 
2 // -- marlinmt headers
3 #include <marlinmt/Processor.h>
6 
7 // -- lcio headers
8 #include <lcio.h>
9 #include <MT/LCWriter.h>
10 #include <IMPL/LCRunHeaderImpl.h>
11 #include <UTIL/LCTOOLS.h>
12 #include <EVENT/LCCollection.h>
13 #include <IMPL/LCCollectionVec.h>
14 
15 // -- std headers
16 #include <iostream>
17 #include <algorithm>
18 #include <bitset>
19 
20 namespace marlinmt {
21 
53  class LCIOOutputProcessor : public Processor {
54  private:
55  typedef std::vector< IMPL::LCCollectionVec* > SubSetVec ;
56  typedef std::shared_ptr<MT::LCWriter> Writer ;
57 
58  public:
60  LCIOOutputProcessor(const LCIOOutputProcessor&) = delete ;
62  ~LCIOOutputProcessor() = default ;
63 
66  void init() ;
67 
70  void processRunHeader( RunHeader* run) ;
71 
74  void processEvent( EventStore * evt ) ;
75 
78  void end() ;
79 
80  private:
81  std::set<std::string> getWriteCollections( EVENT::LCEvent * evt ) const ;
82 
83  private:
84  StringParameter _lcioOutputFile {*this, "LCIOOutputFile",
85  "Name of the LCIO output file", "outputfile.slcio" } ;
86 
87  StringParameter _lcioWriteMode {*this, "LCIOWriteMode",
88  "Write mode for output file: WRITE_APPEND, WRITE_NEW or None", "None" } ;
89 
90  StringVectorParameter _dropCollectionNames {*this, "DropCollectionNames" ,
91  "drops the named collections from the event", {"TPCHits", "HCalHits"} } ;
92 
93  StringVectorParameter _dropCollectionTypes {*this, "DropCollectionTypes" ,
94  "drops all collections of the given type from the event", {"SimTrackerHit", "SimCalorimeterHit"} } ;
95 
96  StringVectorParameter _keepCollectionNames {*this, "KeepCollectionNames" ,
97  "force keep of the named collections - overrules DropCollectionTypes (and DropCollectionNames)", {"MyPreciousSimTrackerHits"} } ;
98 
99  // OptionalProperty<int> _splitFileSizekB {this, "SplitFileSizekB" ,
100  // "will split output file if size in kB exceeds given value - doesn't work with APPEND and NEW", 1992294 } ;
101 
102  // runtime members
103  Writer _writer {nullptr} ;
104  std::atomic<int> _nRuns {0} ;
105  std::atomic<int> _nEvents {0} ;
106  };
107 
108  //--------------------------------------------------------------------------
109  //--------------------------------------------------------------------------
110 
112  Processor("LCIOOutputProcessor") {
113  setDescription( "Writes the current event to the specified LCIO outputfile." ) ;
114  // no lock, the writer implementation is thread safe
116  // don't duplicate opening/writing of output file
118  }
119 
120  //--------------------------------------------------------------------------
121 
123  printParameters() ;
124  _writer = std::make_shared<Writer::element_type>() ;
125  if ( _lcioWriteMode == "WRITE_APPEND" ) {
126  _writer->open( _lcioOutputFile , EVENT::LCIO::WRITE_APPEND ) ;
127  }
128  else if ( _lcioWriteMode == "WRITE_NEW" ) {
129  _writer->open( _lcioOutputFile , EVENT::LCIO::WRITE_NEW ) ;
130  }
131  else {
132  _writer->open( _lcioOutputFile ) ;
133  }
134  }
135 
136  //--------------------------------------------------------------------------
137 
139  auto rhdr = std::make_unique<IMPL::LCRunHeaderImpl>() ;
140  rhdr->setRunNumber( run->runNumber() ) ;
141  rhdr->setDetectorName( run->detectorName() ) ;
142  rhdr->setDescription( run->description() ) ;
143  auto activeSubdets = run->parameter<std::vector<std::string>>( "ActiveSubdetectors" ).value() ;
144  for( auto &det : activeSubdets ) {
145  rhdr->addActiveSubdetector( det ) ;
146  }
147  auto intKeys = run->parameter<std::vector<std::string>>( "LCIntKeys" ).value() ;
148  auto floatKeys = run->parameter<std::vector<std::string>>( "LCFloatKeys" ).value() ;
149  auto strKeys = run->parameter<std::vector<std::string>>( "LCStrKeys" ).value() ;
150  auto keys = details::keys( run->parameters() ) ;
151  for( auto &key : keys ) {
152  if( key == "ActiveSubdetectors" ) {
153  continue ;
154  }
155  if( std::find( intKeys.begin(), intKeys.end(), key ) != intKeys.end() ) {
156  auto values = run->parameter<std::vector<int>>( key ).value() ;
157  rhdr->parameters().setValues( key, values ) ;
158  continue ;
159  }
160  if( std::find( floatKeys.begin(), floatKeys.end(), key ) != floatKeys.end() ) {
161  auto values = run->parameter<std::vector<float>>( key ).value() ;
162  rhdr->parameters().setValues( key, values ) ;
163  continue ;
164  }
165  if( std::find( strKeys.begin(), strKeys.end(), key ) != strKeys.end() ) {
166  auto values = run->parameter<std::vector<std::string>>( key ).value() ;
167  rhdr->parameters().setValues( key, values ) ;
168  continue ;
169  }
170  }
171 
172  _writer->writeRunHeader( rhdr.release() ) ;
173  _nRuns++ ;
174  }
175 
176  //--------------------------------------------------------------------------
177 
178  std::set<std::string> LCIOOutputProcessor::getWriteCollections( EVENT::LCEvent * evt ) const {
179  auto colNames = evt->getCollectionNames() ;
180  std::set<std::string> writeCollections {} ;
181  // loop over collections and collect the ones to actually write
182  for( auto colName : *colNames ) {
183  auto col = evt->getCollection( colName ) ;
184  std::string collectionType = col->getTypeName() ;
185  auto typeIter = std::find( _dropCollectionTypes.get().begin(), _dropCollectionTypes.get().end(), collectionType ) ;
186  auto nameIter = std::find( _dropCollectionNames.get().begin(), _dropCollectionNames.get().end(), colName ) ;
187  auto keepIter = std::find( _keepCollectionNames.get().begin(), _keepCollectionNames.get().end(), colName ) ;
188  if ( _dropCollectionTypes.isSet() && typeIter != _dropCollectionTypes.get().end() ) {
189  continue ;
190  }
191  if ( _dropCollectionNames.isSet() && nameIter != _dropCollectionNames.get().end() ) {
192  continue ;
193  }
194  if( _keepCollectionNames.isSet() && keepIter != _keepCollectionNames.get().end() ) {
195  continue ;
196  }
197  writeCollections.insert( colName ) ;
198  }
199  return writeCollections ;
200  }
201 
202  //--------------------------------------------------------------------------
203 
205  auto lcevent = evt->event<EVENT::LCEvent>() ;
206  if( nullptr == lcevent ) {
207  ProcessorApi::abort( this, "Event is not an LCEvent" ) ;
208  }
209  auto writeCols = getWriteCollections( lcevent.get() ) ;
210  _writer->writeEvent( lcevent.get(), writeCols ) ;
211  _nEvents ++ ;
212  }
213 
214  //--------------------------------------------------------------------------
215 
217  log<MESSAGE4>() << std::endl
218  << "LCIOOutputProcessor::end() " << name()
219  << ": " << _nEvents.load() << " events in " << _nRuns.load() << " runs written to file "
220  << _lcioOutputFile
221  << std::endl
222  << std::endl ;
223  _writer->close() ;
224  _writer = nullptr ;
225  }
226 
228 }
const ParameterMap & parameters() const
Get the run parameters.
Definition: RunHeader.h:162
StringVectorParameter _dropCollectionNames
const std::string & name() const
Get the component name.
Definition: Component.cc:23
std::shared_ptr< T > event() const
Get the underlying event to a specific type.
Definition: EventStore.h:130
void init()
Open the LCIO outputfile.
void setRuntimeOption(ERuntimeOption option, bool value)
Force the runtime option to a given boolean value.
Definition: Processor.cc:37
constexpr unsigned long long value(const Flag_t &flag)
Definition: Flags.h:106
StringVectorParameter _dropCollectionTypes
void processEvent(EventStore *evt)
Write every event.
LCIOOutputProcessor & operator=(const LCIOOutputProcessor &)=delete
std::vector< IMPL::LCCollectionVec *> SubSetVec
#define MARLINMT_DECLARE_PROCESSOR(Class)
Definition: PluginManager.h:34
void setDescription(const std::string &desc)
Set the component description.
Definition: Component.cc:41
std::set< std::string > getWriteCollections(EVENT::LCEvent *evt) const
static void abort(const Processor *const proc, const std::string &reason)
Abort program execution properly.
const std::string & detectorName() const
Get the detector name.
Definition: RunHeader.h:149
void end()
Close outputfile.
std::optional< T > parameter(const std::string &name) const
Get a parameter by name.
Definition: RunHeader.h:175
Whether the processor has to be executed in a critical section.
Default output processor.
T get() const
Get the parameter value.
Definition: Parameter.h:556
void processRunHeader(RunHeader *run)
Write every run header.
RunHeader class.
Definition: RunHeader.h:22
StringVectorParameter _keepCollectionNames
Processor class.
Definition: Processor.h:43
void printParameters() const
Print the component parameters.
Definition: Component.cc:118
const std::string & description() const
Get the run description.
Definition: RunHeader.h:136
int runNumber() const
Get the run number.
Definition: RunHeader.h:123
std::vector< K > keys(const std::map< K, V > &m)
Definition: Utils.h:539
bool isSet() const
Whether the parameter has been set.
Definition: Parameter.h:506
std::shared_ptr< MT::LCWriter > Writer
EventStore class.
Definition: EventStore.h:17