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.5 2013/02/27 20:17:14 wmtan Exp $
17 // Modified: 27/03/2009 Luca Malgeri
18 // reading external file, defining selection syntax
19 //
20 //
21 
22 
23 // system include files
24 #include <memory>
25 #include <iostream>
26 #include <fstream>
27 #include <vector>
28 #include <sstream>
29 #include <limits>
30 
31 // user include files
34 
37 
40 
41 
42 //
43 // class declaration
44 //
45 
46 class PickEvents : public edm::EDFilter {
47  public:
48  explicit PickEvents(const edm::ParameterSet&);
49  ~PickEvents();
50 
51  private:
52  virtual void beginJob() ;
53  virtual bool filter(edm::Event&, const edm::EventSetup&) override;
54  virtual void endJob() ;
55 
58 
59  std::vector<bool> whattodo;
60  std::vector<edm::RunNumber_t> startrun;
61  std::vector<edm::RunNumber_t> endrun;
62  std::vector<edm::EventNumber_t> startevent;
63  std::vector<edm::EventNumber_t> endevent;
64 
67 
68 };
69 
70 
71 
73 {
74 
75  listruneventsinpath_=iConfig.getUntrackedParameter<std::string> ("RunEventList","");
76  edm::FileInPath listruneventstmp("DPGAnalysis/Skims/data/"+listruneventsinpath_);
77 
78  listrunevents_=listruneventstmp.fullPath();
79 
80  std::cout <<"File with run/event list:"<< listrunevents_<<std::endl;
81 
82 }
83 
84 
86 {
87 }
88 
89 bool
91 {
92  using namespace edm;
93 
94  RunNumber_t kRun = iEvent.id().run();
95  EventNumber_t kEvent = iEvent.id().event();
96 
97  bool selectThisEvent = false;
98 
99  for (unsigned int cond=0; cond<whattodo.size();cond++)
100  {
101  // std::string what;
102  if ( kRun>=startrun[cond] &&
103  kRun<=endrun[cond] &&
104  kEvent>=startevent[cond] &&
105  kEvent<=endevent[cond] )
106  { // it's in the range, use
107  selectThisEvent=whattodo[cond];
108  }
109  }
110 
111  nEventsAnalyzed++;
112  if (selectThisEvent) nEventsSelected++;
113  // if (selectThisEvent) std::cout << "Event selected: " << kRun << " " << kEvent << std::endl;
114 
115  return selectThisEvent;
116 }
117 
118 void
120 {
121  using namespace std;
122 
124  std::string buf;
125 
126  std::stringstream ss;
127  std::vector<std::string> tokens;
128 
129  nEventsAnalyzed = 0;
130  nEventsSelected = 0;
131 
132  // open file listevent file
133  ifstream listfile;
134  listfile.open(listrunevents_.c_str());
135  if (listfile.is_open())
136  {
137  while (! listfile.eof() )
138  {
139  getline (listfile,line);
140  ss.clear();
141  ss.str(line);
142  tokens.clear();
143  while (ss>>buf)
144  {
145  tokens.push_back(buf);
146  // std::cout << buf << std::endl;
147  }
148  // std::cout << tokens.size() << std::endl;
149  if (tokens.size()<3)
150  {
151  // std::cout << "strange selection line:" << line << std::endl;
152  // std::cout << "skipping it" << std::endl;
153  continue;
154  }
155  if(tokens[0]=="-" || tokens[0]=="+")
156  {
157  // it's a selection line, use it
158  if(tokens[0]=="-") whattodo.push_back(false);
159  else whattodo.push_back(true);
160 
161  // start with run selecion
162  int loc=tokens[1].find(":",0);
163 
164  std::string first=tokens[1].substr(0,loc);
165  startrun.push_back((edm::RunNumber_t)atoi(first.c_str()));
166 
167  std::string last=tokens[1].substr(loc+1,tokens[1].size());
168  if (last=="infty")
170  else
171  endrun.push_back((edm::RunNumber_t) atoi(last.c_str()));
172 
173  // then event number selecion
174  loc=tokens[2].find(":",0);
175 
176  first=tokens[2].substr(0,loc);
177  startevent.push_back((edm::EventNumber_t)atoi(first.c_str()));
178 
179  last=tokens[2].substr(loc+1,tokens[2].size());
180  if (last=="infty")
182  // endevent.push_back(std::numeric_limits<long long int>::max());
183  else
184  endevent.push_back((edm::EventNumber_t)atoi(last.c_str()));
185 
186  }
187  }
188  listfile.close();
189  // printout summary
190  std::cout << "Summary from list of run/event number selection" << std::endl;
191  for (unsigned int cond=0; cond<whattodo.size();cond++)
192  {
193  std::string what;
194  if(whattodo[cond]) what="select";
195  else what="reject";
196  std::cout << what << " ";
197  std::cout << "from run " << startrun[cond] << " to run " << endrun[cond] << " ";
198  std::cout << "from eve " << startevent[cond] << " to eve " << endevent[cond] << std::endl;
199  }
200  }
201 
202  else std::cout << "Unable to open file";
203 
204 }
205 void
207  using namespace std;
208  std::cout << "================================================\n"
209  << " n Events Analyzed ............... " << nEventsAnalyzed << std::endl
210  << " n Events Selected ............... " << nEventsSelected<< std::endl
211  << "================================================\n\n" ;
212 }
213 
214 
RunNumber_t run() const
Definition: EventID.h:42
EventNumber_t event() const
Definition: EventID.h:44
T getUntrackedParameter(std::string const &, T const &) const
std::string listrunevents_
Definition: PickEvents.cc:56
unsigned int EventNumber_t
Definition: EventID.h:30
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual bool filter(edm::Event &, const edm::EventSetup &) override
Definition: PickEvents.cc:90
int nEventsSelected
Definition: PickEvents.cc:66
int iEvent
Definition: GenABIO.cc:243
const T & max(const T &a, const T &b)
virtual void endJob()
Definition: PickEvents.cc:206
std::vector< edm::RunNumber_t > startrun
Definition: PickEvents.cc:60
std::vector< edm::EventNumber_t > endevent
Definition: PickEvents.cc:63
bool first
Definition: L1TdeRCT.cc:94
virtual void beginJob()
Definition: PickEvents.cc:119
std::vector< bool > whattodo
Definition: PickEvents.cc:59
std::vector< edm::EventNumber_t > startevent
Definition: PickEvents.cc:62
int nEventsAnalyzed
Definition: PickEvents.cc:65
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:61
PickEvents(const edm::ParameterSet &)
Definition: PickEvents.cc:72
std::string listruneventsinpath_
Definition: PickEvents.cc:57
tuple cout
Definition: gather_cfg.py:121
unsigned int RunNumber_t
Definition: EventRange.h:32
tuple size
Write out results.