Marlin  01.17.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LCIOOutputProcessor.cc
Go to the documentation of this file.
2 #include <iostream>
3 
4 #include "IMPL/LCRunHeaderImpl.h"
5 #include "UTIL/LCTOOLS.h"
6 #include "EVENT/LCCollection.h"
7 #include "IMPL/LCCollectionVec.h"
8 
9 
10 #if LCIO_VERSION_GE(1,7)
11 #include "UTIL/LCSplitWriter.h"
12 #else
13 #include "marlin/LCSplitWriter.h" // provided for compatibility with older LCIO versions
14 #endif
15 
16 #include <algorithm>
17 #include <bitset>
18 
19 namespace marlin{
20 
22 
24  }
25 
27  _lcWrt(NULL),
28  _nRun(-1),
29  _nEvt(-1) {
30  myConstruct() ;
31  }
33  _lcWrt(NULL),
34  _nRun(-1),
35  _nEvt(-1) {
36  myConstruct() ;
37  }
38 
40 
41  _description = "Writes the current event to the specified LCIO outputfile."
42  " Needs to be the last ActiveProcessor." ;
43 
44 
45  registerProcessorParameter( "LCIOOutputFile" ,
46  " name of output file " ,
48  std::string("outputfile.slcio") ) ;
49 
50  registerProcessorParameter( "LCIOWriteMode" ,
51  "write mode for output file: WRITE_APPEND, WRITE_NEW or None" ,
53  std::string("None") ) ;
54 
55 
56  StringVec dropNamesExamples ;
57  dropNamesExamples.push_back("TPCHits");
58  dropNamesExamples.push_back("HCalHits");
59 
60 
61  registerOptionalParameter( "DropCollectionNames" ,
62  "drops the named collections from the event" ,
64  dropNamesExamples ) ;
65 
66 
67  StringVec dropTypesExample ;
68  dropTypesExample.push_back("SimTrackerHit");
69  dropTypesExample.push_back("SimCalorimeterHit");
70 
71  registerOptionalParameter( "DropCollectionTypes" ,
72  "drops all collections of the given type from the event" ,
74  dropTypesExample ) ;
75 
76  StringVec keepNamesExample ;
77  keepNamesExample.push_back("MyPreciousSimTrackerHits");
78 
79  registerOptionalParameter( "KeepCollectionNames" ,
80  "force keep of the named collections - overrules DropCollectionTypes (and DropCollectionNames)" ,
82  keepNamesExample ) ;
83 
84 
85  StringVec fullSubsetExample ;
86  fullSubsetExample.push_back("MCParticlesSkimmed");
87 
88  registerOptionalParameter( "FullSubsetCollections" ,
89  " write complete objects in subset collections to the file (i.e. ignore subset flag)" ,
91  fullSubsetExample ) ;
92 
93 
94  registerOptionalParameter( "SplitFileSizekB" ,
95  "will split output file if size in kB exceeds given value - doesn't work with APPEND and NEW" ,
97  1992294 ) ; // 1.9 GB in kB
98 
99  registerOptionalParameter( "CompressionLevel" ,
100  "The ZLIB compression level on writing. Set it to 0 for no compression" ,
102  _compressionLevel ) ; // 6 by default
103 
104  }
105 
107 
108  printParameters() ;
109 
110  _nRun = 0 ;
111  _nEvt = 0 ;
112 
113  if( parameterSet("SplitFileSizekB") ){
114 
115  _lcWrt = new LCSplitWriter( LCFactory::getInstance()->createLCWriter(), _splitFileSizekB*1024 ) ;
116 
117  } else {
118 
119  _lcWrt = LCFactory::getInstance()->createLCWriter() ;
120  }
121 
122  _lcWrt->setCompressionLevel( _compressionLevel ) ;
123 
124 
125  if( _lcioWriteMode == "WRITE_APPEND" ) {
126 
127  _lcWrt->open( _lcioOutputFile , LCIO::WRITE_APPEND ) ;
128  }
129  else if( _lcioWriteMode == "WRITE_NEW" ) {
130 
131  _lcWrt->open( _lcioOutputFile , LCIO::WRITE_NEW ) ;
132  }
133  else {
134  _lcWrt->open( _lcioOutputFile ) ;
135  }
136 
137 // _lcWrt->writeRunHeader( new LCRunHeaderImpl ) ;
138 // _lcWrt->close() ;
139 // _lcWrt->open( _lcioOutputFile , LCIO::WRITE_APPEND ) ;
140 }
141 
142 
143 
145 
146 // std::cout << "LCIOOutputProcessor::processRun() " << name() <<" this << " << this
147 // << " in run " << run->getRunNumber()
148 // << std::endl ;
149 
150  _lcWrt->writeRunHeader( run ) ;
151 
152  _nRun++ ;
153 }
154 
155  void LCIOOutputProcessor::dropCollections( LCEvent * evt ) {
156 
157 // bool isLCIO_v01_04_01 = false ;
158 // #ifdef LCIO_PATCHVERSION_GE
159 // isLCIO_v01_04_01 = LCIO_PATCHVERSION_GE( 1, 4, 1) ;
160 // #endif
161 // if( ! isLCIO_v01_04_01 ) {
162 // static bool firstCall = true ;
163 // if( firstCall ) {
164 // std::cout << " *** WARNING: LCIOOutputProcessor::dropCollections requires LCIO v01-04-01 or higher "
165 // << std:: endl
166 // << " -> no collections droped from the event !! "
167 // << std:: endl ;
168 // }
169 // firstCall = false ;
170 
171 // return ;
172 // }
173 
174  const StringVec* colNames = evt->getCollectionNames() ;
175 
176 
177  // if all tracker hits are droped we don't store the hit pointers with the tracks below ...
178  bool trackerHitsDroped = false ;
179  bool calorimeterHitsDroped = false ;
180 
181  if( parameterSet("DropCollectionTypes") ){
182 
184  , LCIO::TRACKERHIT ) != _dropCollectionTypes.end() ) {
185 
186  trackerHitsDroped = true ;
187  }
188 
190  , LCIO::CALORIMETERHIT ) != _dropCollectionTypes.end() ) {
191 
192  calorimeterHitsDroped = true ;
193  }
194  }
195 
196  for( StringVec::const_iterator it = colNames->begin();
197  it != colNames->end() ; it++ ){
198 
199  LCCollectionVec* col = dynamic_cast<LCCollectionVec*> (evt->getCollection( *it ) ) ;
200 
201  std::string collectionType = col->getTypeName() ;
202 
203  if( parameterSet("DropCollectionTypes") && std::find( _dropCollectionTypes.begin(),
204  _dropCollectionTypes.end(), collectionType )
205  != _dropCollectionTypes.end() ) {
206 
207  col->setTransient( true ) ;
208  }
209  if( parameterSet("DropCollectionNames") && std::find( _dropCollectionNames.begin(),
210  _dropCollectionNames.end(), *it )
211  != _dropCollectionNames.end() ) {
212 
213  col->setTransient( true ) ;
214  }
215 
216  if( parameterSet("KeepCollectionNames") && std::find( _keepCollectionNames.begin(),
217  _keepCollectionNames.end(), *it )
218  != _keepCollectionNames.end() ) {
219 
220  col->setTransient( false ) ;
221  }
222 
223 
224  if( parameterSet("FullSubsetCollections") && std::find( _fullSubsetCollections.begin(),
225  _fullSubsetCollections.end(), *it )
226  != _fullSubsetCollections.end() ) {
227 
228 
229  if( col->isSubset() ) {
230 
231  col->setSubset( false ) ;
232  _subSets.push_back(col) ;
233  }
234 
235  }
236 
237 
238  // don't store hit pointers if hits are droped
239  if( collectionType == LCIO::TRACK && trackerHitsDroped ){
240 
241  std::bitset<32> flag( col->getFlag() ) ;
242  flag[ LCIO::TRBIT_HITS ] = 0 ;
243  col->setFlag( flag.to_ulong() ) ;
244  }
245  if( collectionType == LCIO::CLUSTER && calorimeterHitsDroped ){
246 
247  std::bitset<32> flag( col->getFlag() ) ;
248  flag[ LCIO::CLBIT_HITS ] = 0 ;
249  col->setFlag( flag.to_ulong() ) ;
250  }
251 
252  }
253 
254  }
255 
256 void LCIOOutputProcessor::processEvent( LCEvent * evt ) {
257 // std::cout << "LCIOOutputProcessor::processEvent() " << name()
258 // << " in event " << evt->getEventNumber() << " (run " << evt->getRunNumber() << ") "
259 // << std::endl ;
260 
261 // std::cout << " writing event : " << std::endl ;
262 // LCTOOLS::dumpEvent( evt ) ;
263 
264 
265  dropCollections( evt ) ;
266 
267  _lcWrt->writeEvent( evt ) ;
268 
269  // revert subset flag - if any
270  for( SubSetVec::iterator sIt = _subSets.begin() ;
271  sIt != _subSets.end() ; ++sIt ) {
272 
273  (*sIt)->setSubset( true ) ;
274  }
275  _subSets.clear() ;
276 
277 
278  _nEvt ++ ;
279 }
280 
282 
283  streamlog_out( MESSAGE4 ) << std::endl
284  << "LCIOOutputProcessor::end() " << name()
285  << ": " << _nEvt << " events in " << _nRun << " runs written to file "
286  << _lcioOutputFile
287  << std::endl
288  << std::endl ;
289 
290  _lcWrt->close() ;
291  delete _lcWrt;
292  _lcWrt = nullptr;
293 
294 }
295 
296 } // namespace marlin{
void registerProcessorParameter(const std::string &parameterName, const std::string &parameterDescription, T &parameter, const T &defaultVal, int setSize=0)
Register a steering variable for this processor - call in constructor of processor.
Definition: Processor.h:254
virtual void processRunHeader(LCRunHeader *run)
Write every run header.
bool parameterSet(const std::string &name)
Tests whether the parameter has been set in the steering file.
Definition: Processor.cc:204
virtual void init()
Open the LCIO outputfile.
virtual const std::string & name() const
Return name of this processor.
Definition: Processor.h:132
T to_ulong(T...args)
T endl(T...args)
T end(T...args)
STL class.
void printParameters()
Print the parameters and their values depending on the given verbosity level.
Definition: Processor.h:156
T push_back(T...args)
virtual void end()
Close outputfile.
virtual ~LCIOOutputProcessor()
D&#39;tor for subclasses.
Default output processor.
void dropCollections(LCEvent *evt)
Drops the collections specified in the steering file parameters DropCollectionNames and DropCollectio...
void registerOptionalParameter(const std::string &parameterName, const std::string &parameterDescription, T &parameter, const T &defaultVal, int setSize=0)
Same as registerProcessorParameter except that the parameter is optional.
Definition: Processor.h:314
T clear(T...args)
T find(T...args)
T begin(T...args)
Base class for Marlin processors.
Definition: Processor.h:64
std::string _description
Describes what the processor does.
Definition: Processor.h:452
virtual void processEvent(LCEvent *evt)
Write every event.
void myConstruct()
Inititalization for constructors.
LCIOOutputProcessor anLCIOOutputProcessor