From 850dde02595939ffabc28d9bbad75b1b2ba40b3b Mon Sep 17 00:00:00 2001 From: Ulrich Einhaus Date: Tue, 14 May 2024 18:22:53 +0200 Subject: [PATCH] Add SelectNthEventsProcessor --- source/include/SelectNthEventsProcessor.h | 75 +++++++++++++++++++++++ source/src/SelectNthEventsProcessor.cc | 56 +++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 source/include/SelectNthEventsProcessor.h create mode 100644 source/src/SelectNthEventsProcessor.cc diff --git a/source/include/SelectNthEventsProcessor.h b/source/include/SelectNthEventsProcessor.h new file mode 100644 index 0000000..23529fb --- /dev/null +++ b/source/include/SelectNthEventsProcessor.h @@ -0,0 +1,75 @@ +#ifndef SelectNthEventsProcessor_h +#define SelectNthEventsProcessor_h 1 + +#include "marlin/Processor.h" +#include "lcio.h" +#include + + +using namespace lcio ; +using namespace marlin ; + + +/** Select a number n, every nth event will have the processor ReturnValue set to true, all others to false. + * The selection can be inverted and the starting event can be offset from event 0. + * Use in steering file in execute section via + * + * + * + * + * @param nEventToSelect: Integer number n. Every nth event's ReturnValue is set to true. + * @param SelectionOffset: Sets an offset m, so the Return Value of the mth event (and every nth before and after it) is set to true. + * @param InvertSelection: Inverts the ReturnValue. If true, every nth event's ReturnValue is false, all others are true. + * + * @author U Einhaus, DESY + * @version $Id: SelectNthEventsProcessor.h, v1.0 2024-01-21 $ + */ + +class SelectNthEventsProcessor : public Processor { + + public: + + virtual Processor* newProcessor() { return new SelectNthEventsProcessor ; } + + + SelectNthEventsProcessor() ; + + /** Called at the begin of the job before anything is read. + * Use to initialize the processor, e.g. book histograms. + */ + virtual void init() ; + + /** Called for every run. + */ + virtual void processRunHeader( LCRunHeader* ) {}; + + /** Called for every event - the working horse. + */ + virtual void processEvent( LCEvent* ); + + + virtual void check( LCEvent* ) {}; + + + /** Called after data processing for clean up. + */ + virtual void end() ; + + + protected: + + int _nRun=0; + int _nEvt=0; + int _nEvtProc=0; + int _nEvtSkip=0; + + int _nEventToSelect=1; + int _selectionOffset=0; + bool _invertSelection=false; + +} ; + +#endif + + + diff --git a/source/src/SelectNthEventsProcessor.cc b/source/src/SelectNthEventsProcessor.cc new file mode 100644 index 0000000..9c618e3 --- /dev/null +++ b/source/src/SelectNthEventsProcessor.cc @@ -0,0 +1,56 @@ +#include "SelectNthEventsProcessor.h" +#include + +using namespace lcio ; +using namespace marlin ; + + +SelectNthEventsProcessor aSelectNthEventsProcessor ; + +SelectNthEventsProcessor::SelectNthEventsProcessor() : Processor("SelectNthEventsProcessor"){ + + _description = "SelectNthEventsProcessor Processor sets ReturnValue true for every nth event. The selection can be offset and/or inverted."; + + + registerProcessorParameter( "nEventToSelect", + "Integer number n, every nth event's ReturnValue is set to true; default: 1", + _nEventToSelect, + int(1) ); + + registerProcessorParameter( "SelectionOffset", + "Sets an offset m, so the Return Value of the mth event (and every nth before and after it) is set to true; default: 0", + _selectionOffset, + int(0) ); + + registerProcessorParameter( "InvertSelection", + "Inverts the ReturnValue; if true, every nth event's ReturnValue is false, all others are true; default: false", + _invertSelection, + false ); +} + +void SelectNthEventsProcessor::init(){ + // usually a good idea to + printParameters() ; + + _nRun = 0; + _nEvt = 0; + _nEvtProc = 0; + _nEvtSkip = 0; + +} + +void SelectNthEventsProcessor::processEvent( LCEvent* ){ + + bool skip ( (_nEvt-_selectionOffset)%_nEventToSelect!=0 ); + if( _invertSelection ) skip = !skip; + + setReturnValue(!skip); + + ++_nEvt; + if (skip) ++_nEvtSkip; + else ++_nEvtProc; +} + +void SelectNthEventsProcessor::end(){ + streamlog_out(MESSAGE) << "Total Events: " << _nEvt << ", Events processed: " << _nEvtProc << ", Events skipped: " << _nEvtSkip << std::endl; +}