DuneEventFilter_module.cc
Go to the documentation of this file.
1 // DuneEventFilter_module.cc
2 //
3 // Module to select or reject events based on event number.
4 //
5 // David Adams
6 // October 2018
7 // June 2019: Update using event range "events" from the job IndexRangeTool.
8 //
9 // Configuration:
10 // LogLevel: 0=quiet, 1=ctor message, 2=status of each event
11 // SelectEvents: If this has entries, the event must be among them
12 // RejectEvents: Event is rejected if it is in this list.
13 // EventBegin, EventEnd: if EventEnd > EventBegin, event must be in (EventBegin, EventEnd].
14 // EventModFreq, EventModVal: keep events for which ievt%EventModFreq == EventModVal
15 // JobIndexRangeTool: Name of the job IndexRangeTool, e.g. jobRanges.
16 // SkipEventTool: Name of a tool (IndexVectorTool) that lists events to be skipped
17 // indexed by run number. Blank means no tool.
18 //
19 // The range of event to process is (EventBegin, EventEnd] if EventEnd > EventBegin.
20 // Otherwise the range is taken from the index range "events" in the job IndexRangeTool.
21 // If that range does not exist or is not valid, all events are processed.
22 
23 #include <iostream>
24 #include <vector>
25 #include <set>
26 
33 
35 
36 public:
37 
38  using Index = unsigned int;
39  using IndexVector = std::vector<Index>;
40  using IndexSet = std::set<Index>;
41  using Name = std::string;
42 
43  explicit DuneEventFilter(fhicl::ParameterSet const & pset);
44  virtual ~DuneEventFilter();
45  virtual bool filter(art::Event& e);
46 
47 private:
48 
49  // Configuration parameters.
59 
60  // Derived from configuration.
66 
67  // Counters.
70 
71 };
72 
73 //*****************************************************************************
74 
76 : EDFilter(pset),
77  m_LogLevel(pset.get<Index>("LogLevel")),
78  m_SelectEventVector(pset.get<IndexVector>("SelectEvents")),
79  m_RejectEventVector(pset.get<IndexVector>("RejectEvents")),
80  m_EventBegin(pset.get<Index>("EventBegin")),
81  m_EventEnd(pset.get<Index>("EventEnd")),
82  m_EventModFreq(pset.get<Index>("EventModFreq")),
83  m_EventModVal(pset.get<Index>("EventModVal")),
84  m_JobIndexRangeTool(pset.get<Name>("JobIndexRangeTool")),
85  m_SkipEventTool(pset.get<Name>("SkipEventTool")),
86  m_beginEvent(0), m_endEvent(0),
87  m_pSkipEventTool(nullptr),
88  m_nproc(0), m_nsel(0) {
89  using std::cout;
90  using std::endl;
91  using std::string;
92  const string myname = "DuneEventFilter::ctor: ";
93  for ( Index ievt : m_SelectEventVector ) m_SelectEvents.insert(ievt);
94  for ( Index ievt : m_RejectEventVector ) m_RejectEvents.insert(ievt);
95  if ( m_LogLevel >= 1 ) {
96  cout << myname << " LogLevel: " << m_LogLevel << endl;
97  cout << myname << " SelectEvents: [";
98  bool first = true;
99  for ( unsigned int ievt : m_SelectEvents ) {
100  if ( first ) first = false;
101  else cout << ", ";
102  cout << ievt;
103  }
104  cout << "]" << endl;
105  cout << myname << " RejectEvents: [";
106  first = true;
107  for ( unsigned int ievt : m_RejectEvents ) {
108  if ( first ) first = false;
109  else cout << ", ";
110  cout << ievt;
111  }
112  cout << "]" << endl;
113  cout << myname << " EventBegin: " << m_EventBegin << endl;
114  cout << myname << " EventEnd: " << m_EventEnd << endl;
115  cout << myname << " EventModVal: " << m_EventModVal << endl;
116  cout << myname << " EventModFreq: " << m_EventModFreq << endl;
117  cout << myname << " JobIndexRangeTool: " << m_JobIndexRangeTool << endl;
118  cout << myname << " SkipEventTool: " << m_SkipEventTool << endl;
119  }
120  if ( m_EventEnd > m_EventBegin ) {
123  } else if ( m_JobIndexRangeTool.size() ) {
126  if ( pjrt == nullptr ) {
127  cout << myname << "ERROR: Job index range tool not found: " << m_JobIndexRangeTool << endl;
128  } else {
129  IndexRange ran = pjrt->get("events");
130  if ( ! ran.isValid() ) {
131  cout << myname << "Event range is unrestricted." << endl;
132  } else {
133  m_beginEvent = ran.begin;
134  m_endEvent = ran.end;
135  }
136  }
137  }
138  if ( m_endEvent > m_beginEvent ) {
139  cout << myname << "Event selection range is [" << m_beginEvent << ", "
140  << m_endEvent << ")." << endl;
141  } else {
142  cout << myname << "No event selection range." << endl;
143  }
144  if ( m_SkipEventTool.size() ) {
147  if ( m_pSkipEventTool == nullptr ) {
148  cout << "WARNING: Unable to find SkipEventTool " << m_SkipEventTool << endl;
149  } else {
150  cout << myname << "Using SkipEventTool @" << m_pSkipEventTool << endl;
151  }
152  }
153 }
154 
155 //*****************************************************************************
156 
158  using std::cout;
159  using std::endl;
160  using std::string;
161  const string myname = "DuneEventFilter::filter: ";
162  Index ievt = evt.event();
163  ++m_nproc;
164  bool keep = true;
165  if ( keep && m_SelectEvents.size() ) keep = m_SelectEvents.count(ievt);
166  if ( keep ) keep = m_RejectEvents.count(ievt) == 0;
167  if ( keep && m_endEvent > m_beginEvent ) keep = ievt >= m_beginEvent && ievt < m_endEvent;
168  if ( keep && m_EventModFreq ) keep = (ievt % m_EventModFreq) == m_EventModVal;
169  if ( keep && m_pSkipEventTool != nullptr ) {
170  const IndexVector& skipEvents = m_pSkipEventTool->get(evt.run());
171  if ( find(skipEvents.begin(), skipEvents.end(), ievt) != skipEvents.end() ) {
172  keep = false;
173  if ( m_LogLevel >= 3 ) {
174  cout << myname << " Event " << ievt << " rejected by SkipEventTool" << endl;
175  }
176  }
177  }
178  if ( m_LogLevel >= 2 ) {
179  cout << myname << (keep ? "Sel" : "Rej") << "ecting event " << ievt << endl;
180  }
181  if ( keep ) ++m_nsel;
182  return keep;
183 }
184 
185 //*****************************************************************************
186 
188  using std::cout;
189  using std::endl;
190  using std::string;
191  const string myname = "DuneEventFilter::dtor: ";
192  if ( m_LogLevel >= 1 ) {
193  float fsel = double(m_nsel)/double(m_nproc);
194  cout << myname << " Events processed: " << m_nproc << endl;
195  cout << myname << " Events selected: " << m_nsel << " (" << fsel << ")" << endl;
196  }
197 }
198 
199 //*****************************************************************************
200 
EventNumber_t event() const
Definition: DataViewImpl.cc:85
std::string string
Definition: nybbler.cc:12
Index begin
Definition: IndexRange.h:34
virtual IndexVector get(Index idx) const =0
virtual bool filter(art::Event &e)
const IndexVectorMapTool * m_pSkipEventTool
bool isValid() const
Definition: IndexRange.h:94
Index end
Definition: IndexRange.h:35
IndexVector m_RejectEventVector
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
IndexVector m_SelectEventVector
std::vector< Index > IndexVector
RunNumber_t run() const
Definition: DataViewImpl.cc:71
EDFilter(fhicl::ParameterSet const &pset)
Definition: EDFilter.h:21
DuneEventFilter(fhicl::ParameterSet const &pset)
TCEvent evt
Definition: DataStructs.cxx:7
std::set< Index > IndexSet
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
static DuneToolManager * instance(std::string fclname="", int dbg=1)
T * getShared(std::string name)
QTextStream & endl(QTextStream &s)
virtual IndexRange get(Name nam) const =0