CMS 3D CMS Logo

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