CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PickEvents.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PickEvents
4 // Class: PickEvents
5 //
13 //
14 // Original Author: Michael Henry Schmitt
15 // Created: Mon Sep 15 19:36:37 CEST 2008
16 // $Id: PickEvents.cc,v 1.4 2010/08/07 14:55:55 wmtan Exp $
17 // Modified: 27/03/2009 Luca Malgeri
18 // reading external file, defining selection syntax
19 // Modified: 30/06/2014 Giovanni Franzoni
20 // reading run-lumisection list from json
21 //
22 //
23 
24 // system include files
25 #include <memory>
26 #include <iostream>
27 #include <fstream>
28 #include <vector>
29 #include <sstream>
30 #include <limits>
31 #include <cassert>
32 
33 // user include files
36 
39 
42 
44 
45 // ordering function to sort LuminosityBlockRange based on the starting run number
47  return (u.startRun() < v.startRun());
48 }
49 
50 //
51 // class declaration
52 //
53 
54 class PickEvents : public edm::one::EDFilter<> {
55 public:
56  explicit PickEvents(const edm::ParameterSet&);
57  ~PickEvents() override;
58 
59 private:
60  void beginJob() override;
61  bool filter(edm::Event&, const edm::EventSetup&) override;
62  void endJob() override;
63 
67  std::vector<edm::LuminosityBlockRange> luminositySectionsBlockRanges_;
68 
69  std::vector<bool> whattodo;
70  std::vector<edm::RunNumber_t> startrun;
71  std::vector<edm::RunNumber_t> endrun;
72  std::vector<edm::EventNumber_t> startevent;
73  std::vector<edm::EventNumber_t> endevent;
74 
77 };
78 
80  isRunLsBased_ = iConfig.getParameter<bool>("IsRunLsBased");
82  iConfig.getUntrackedParameter<std::vector<edm::LuminosityBlockRange> >("LuminositySectionsBlockRange");
83 
84  listruneventsinpath_ = iConfig.getUntrackedParameter<std::string>("RunEventList", "");
85  edm::FileInPath listruneventstmp(listruneventsinpath_);
86  listrunevents_ = listruneventstmp.fullPath();
87 
88  // sanity checks
89  if (isRunLsBased_ && luminositySectionsBlockRanges_.empty()) {
90  assert("ERROR: selection based on run/Lumisection from json file, but LuminositySectionsBlockRange is emptpy." ==
91  nullptr);
92  }
93  if ((!isRunLsBased_) && !luminositySectionsBlockRanges_.empty()) {
94  assert("ERROR: selection based on run/event from txt file, but LuminositySectionsBlockRange is not emptpy." ==
95  nullptr);
96  }
97 
98  if (isRunLsBased_) {
99  std::cout << "Selection based on run/luminositySection; file with run/event list: " << std::endl;
100  } else {
101  std::cout << "Selection based on run/event; file with run/event list: " << listrunevents_ << std::endl;
102  }
103 }
104 
106 
108  using namespace edm;
109 
110  RunNumber_t kRun = iEvent.id().run();
111  EventNumber_t kEvent = iEvent.id().event();
112  LuminosityBlockNumber_t kLumi = iEvent.id().luminosityBlock();
113 
114  bool selectThisEvent = false;
115 
116  // two alternative definition of the filter selection are possible, according to isRunLsBased_
117 
118  if (isRunLsBased_) {
119  // std::cout << "GF DEBUG: kRun is " << kRun << " kLumi is: " << kLumi << std::endl;
120 
121  for (std::vector<edm::LuminosityBlockRange>::iterator oneLumiRange = luminositySectionsBlockRanges_.begin();
122  oneLumiRange != luminositySectionsBlockRanges_.end();
123  ++oneLumiRange) {
124  // luminositySectionsBlockRanges_ is sorted according to startRun()
125  // => if kRun below it, you can stop the loop and return false
126  if (kRun < (*oneLumiRange).startRun()) {
127  // std::cout << "GF DEBUG: LS has NOT PASSED (early bail-out) ! ***" << std::endl;
128  break;
129  }
130 
131  // if endRun() below kRun, go to the next iteration
132  if ((*oneLumiRange).endRun() < kRun)
133  continue;
134 
135  // if the run number and lumi section match => exit from the loop
136  if ((*oneLumiRange).startLumi() <= kLumi && kLumi <= (*oneLumiRange).endLumi()) {
137  selectThisEvent = true;
138  // std::cout << "GF DEBUG: LS HAS PASSED ! ***" << std::endl;
139  break;
140  }
141  }
142 
143  } // end of isRunLsBased_
144  else { // !isRunLsBased_
145 
146  for (unsigned int cond = 0; cond < whattodo.size(); cond++) {
147  // std::string what;
148  if (kRun >= startrun[cond] && kRun <= endrun[cond] && kEvent >= startevent[cond] &&
149  kEvent <= endevent[cond]) { // it's in the range, use
150  selectThisEvent = whattodo[cond];
151  }
152  } // loop on whattodo
153 
154  } // !isRunLsBased_
155 
156  nEventsAnalyzed++;
157  if (selectThisEvent)
158  nEventsSelected++;
159  // if (selectThisEvent) std::cout << "Event selected: " << kRun << " " << kEvent << std::endl;
160 
161  return selectThisEvent;
162 }
163 
165  using namespace std;
166 
169 
170  std::stringstream ss;
171  std::vector<std::string> tokens;
172 
173  nEventsAnalyzed = 0;
174  nEventsSelected = 0;
175 
176  if (isRunLsBased_) {
177  // sorting luminositySectionsBlockRanges_ according to the starting run of the block allows the speedup the search by an average factor 2
179  } // if isRunLsBased_
180 
181  else { // !isRunLsBased_
182 
183  // open file listevent file
184  std::ifstream listfile;
185  listfile.open(listrunevents_.c_str());
186  if (listfile.is_open()) {
187  while (!listfile.eof()) {
188  getline(listfile, line);
189  ss.clear();
190  ss.str(line);
191  tokens.clear();
192  while (ss >> buf) {
193  tokens.push_back(buf);
194  // std::cout << buf << std::endl;
195  }
196  // std::cout << tokens.size() << std::endl;
197  if (tokens.size() < 3) {
198  // std::cout << "strange selection line:" << line << std::endl;
199  // std::cout << "skipping it" << std::endl;
200  continue;
201  }
202  if (tokens[0] == "-" || tokens[0] == "+") {
203  // it's a selection line, use it
204  if (tokens[0] == "-")
205  whattodo.push_back(false);
206  else
207  whattodo.push_back(true);
208 
209  // start with run selecion
210  int loc = tokens[1].find(':', 0);
211 
212  std::string first = tokens[1].substr(0, loc);
213  startrun.push_back((edm::RunNumber_t)atoi(first.c_str()));
214 
215  std::string last = tokens[1].substr(loc + 1, tokens[1].size());
216  if (last == "infty")
218  else
219  endrun.push_back((edm::RunNumber_t)atoi(last.c_str()));
220 
221  // then event number selecion
222  loc = tokens[2].find(':', 0);
223 
224  first = tokens[2].substr(0, loc);
225  startevent.push_back((edm::EventNumber_t)atoi(first.c_str()));
226 
227  last = tokens[2].substr(loc + 1, tokens[2].size());
228  if (last == "infty")
230  // endevent.push_back(std::numeric_limits<long long int>::max());
231  else
232  endevent.push_back((edm::EventNumber_t)atoi(last.c_str()));
233  }
234  }
235  listfile.close();
236  // printout summary
237  std::cout << "Summary from list of run/event number selection" << std::endl;
238  for (unsigned int cond = 0; cond < whattodo.size(); cond++) {
239  std::string what;
240  if (whattodo[cond])
241  what = "select";
242  else
243  what = "reject";
244  std::cout << what << " ";
245  std::cout << "from run " << startrun[cond] << " to run " << endrun[cond] << " ";
246  std::cout << "from eve " << startevent[cond] << " to eve " << endevent[cond] << std::endl;
247  }
248  }
249 
250  else
251  std::cout << "Unable to open file";
252 
253  } // !isRunLsBased_
254 }
256  using namespace std;
257  std::cout << "================================================\n"
258  << " n Events Analyzed ............... " << nEventsAnalyzed << std::endl
259  << " n Events Selected ............... " << nEventsSelected << std::endl
260  << "================================================\n\n";
261 }
262 
RunNumber_t run() const
Definition: EventID.h:38
EventNumber_t event() const
Definition: EventID.h:40
T getUntrackedParameter(std::string const &, T const &) const
~PickEvents() override
Definition: PickEvents.cc:105
std::string listrunevents_
Definition: PickEvents.cc:64
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void beginJob() override
Definition: PickEvents.cc:164
unsigned long long EventNumber_t
bool isRunLsBased_
Definition: PickEvents.cc:66
assert(be >=bs)
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:39
unsigned int LuminosityBlockNumber_t
bool filter(edm::Event &, const edm::EventSetup &) override
Definition: PickEvents.cc:107
int nEventsSelected
Definition: PickEvents.cc:76
std::vector< edm::LuminosityBlockRange > luminositySectionsBlockRanges_
Definition: PickEvents.cc:67
int iEvent
Definition: GenABIO.cc:224
void endJob() override
Definition: PickEvents.cc:255
std::vector< edm::RunNumber_t > startrun
Definition: PickEvents.cc:70
std::vector< edm::EventNumber_t > endevent
Definition: PickEvents.cc:73
bool orderLuminosityBlockRange(edm::LuminosityBlockRange u, edm::LuminosityBlockRange v)
Definition: PickEvents.cc:46
RunNumber_t startRun() const
std::vector< bool > whattodo
Definition: PickEvents.cc:69
std::vector< edm::EventNumber_t > startevent
Definition: PickEvents.cc:72
int nEventsAnalyzed
Definition: PickEvents.cc:75
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
static EventNumber_t maxEventNumber()
Definition: EventID.h:96
edm::EventID id() const
Definition: EventBase.h:59
std::vector< edm::RunNumber_t > endrun
Definition: PickEvents.cc:71
PickEvents(const edm::ParameterSet &)
Definition: PickEvents.cc:79
std::string listruneventsinpath_
Definition: PickEvents.cc:65
tuple cout
Definition: gather_cfg.py:144
unsigned int RunNumber_t
tuple last
Definition: dqmdumpme.py:56
tuple size
Write out results.