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