File indexing completed on 2024-09-08 23:51:37
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #include <memory>
0026 #include <iostream>
0027 #include <fstream>
0028 #include <vector>
0029 #include <sstream>
0030 #include <limits>
0031 #include <cassert>
0032
0033
0034 #include "FWCore/Framework/interface/Frameworkfwd.h"
0035 #include "FWCore/Framework/interface/one/EDFilter.h"
0036
0037 #include "FWCore/Framework/interface/Event.h"
0038 #include "FWCore/Framework/interface/MakerMacros.h"
0039
0040 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0041 #include "FWCore/ParameterSet/interface/FileInPath.h"
0042
0043 #include "DataFormats/Provenance/interface/LuminosityBlockRange.h"
0044
0045
0046 bool orderLuminosityBlockRange(edm::LuminosityBlockRange u, edm::LuminosityBlockRange v) {
0047 return (u.startRun() < v.startRun());
0048 }
0049
0050
0051
0052
0053
0054 class PickEvents : public edm::one::EDFilter<> {
0055 public:
0056 explicit PickEvents(const edm::ParameterSet&);
0057 ~PickEvents() override;
0058
0059 private:
0060 void beginJob() override;
0061 bool filter(edm::Event&, const edm::EventSetup&) override;
0062 void endJob() override;
0063
0064 std::string listrunevents_;
0065 std::string listruneventsinpath_;
0066 bool isRunLsBased_;
0067 std::vector<edm::LuminosityBlockRange> luminositySectionsBlockRanges_;
0068
0069 std::vector<bool> whattodo;
0070 std::vector<edm::RunNumber_t> startrun;
0071 std::vector<edm::RunNumber_t> endrun;
0072 std::vector<edm::EventNumber_t> startevent;
0073 std::vector<edm::EventNumber_t> endevent;
0074
0075 int nEventsAnalyzed;
0076 int nEventsSelected;
0077 };
0078
0079 PickEvents::PickEvents(const edm::ParameterSet& iConfig) {
0080 isRunLsBased_ = iConfig.getParameter<bool>("IsRunLsBased");
0081 luminositySectionsBlockRanges_ =
0082 iConfig.getUntrackedParameter<std::vector<edm::LuminosityBlockRange> >("LuminositySectionsBlockRange");
0083
0084 listruneventsinpath_ = iConfig.getUntrackedParameter<std::string>("RunEventList", "");
0085 edm::FileInPath listruneventstmp(listruneventsinpath_);
0086 listrunevents_ = listruneventstmp.fullPath();
0087
0088
0089 if (isRunLsBased_ && luminositySectionsBlockRanges_.empty()) {
0090 assert("ERROR: selection based on run/Lumisection from json file, but LuminositySectionsBlockRange is emptpy." ==
0091 nullptr);
0092 }
0093 if ((!isRunLsBased_) && !luminositySectionsBlockRanges_.empty()) {
0094 assert("ERROR: selection based on run/event from txt file, but LuminositySectionsBlockRange is not emptpy." ==
0095 nullptr);
0096 }
0097
0098 if (isRunLsBased_) {
0099 std::cout << "Selection based on run/luminositySection; file with run/event list: " << std::endl;
0100 } else {
0101 std::cout << "Selection based on run/event; file with run/event list: " << listrunevents_ << std::endl;
0102 }
0103 }
0104
0105 PickEvents::~PickEvents() {}
0106
0107 bool PickEvents::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0108 using namespace edm;
0109
0110 RunNumber_t kRun = iEvent.id().run();
0111 EventNumber_t kEvent = iEvent.id().event();
0112 LuminosityBlockNumber_t kLumi = iEvent.id().luminosityBlock();
0113
0114 bool selectThisEvent = false;
0115
0116
0117
0118 if (isRunLsBased_) {
0119
0120
0121 for (std::vector<edm::LuminosityBlockRange>::iterator oneLumiRange = luminositySectionsBlockRanges_.begin();
0122 oneLumiRange != luminositySectionsBlockRanges_.end();
0123 ++oneLumiRange) {
0124
0125
0126 if (kRun < (*oneLumiRange).startRun()) {
0127
0128 break;
0129 }
0130
0131
0132 if ((*oneLumiRange).endRun() < kRun)
0133 continue;
0134
0135
0136 if ((*oneLumiRange).startLumi() <= kLumi && kLumi <= (*oneLumiRange).endLumi()) {
0137 selectThisEvent = true;
0138
0139 break;
0140 }
0141 }
0142
0143 }
0144 else {
0145
0146 for (unsigned int cond = 0; cond < whattodo.size(); cond++) {
0147
0148 if (kRun >= startrun[cond] && kRun <= endrun[cond] && kEvent >= startevent[cond] &&
0149 kEvent <= endevent[cond]) {
0150 selectThisEvent = whattodo[cond];
0151 }
0152 }
0153
0154 }
0155
0156 nEventsAnalyzed++;
0157 if (selectThisEvent)
0158 nEventsSelected++;
0159
0160
0161 return selectThisEvent;
0162 }
0163
0164 void PickEvents::beginJob() {
0165 using namespace std;
0166
0167 std::string line;
0168 std::string buf;
0169
0170 std::stringstream ss;
0171 std::vector<std::string> tokens;
0172
0173 nEventsAnalyzed = 0;
0174 nEventsSelected = 0;
0175
0176 if (isRunLsBased_) {
0177
0178 std::sort(luminositySectionsBlockRanges_.begin(), luminositySectionsBlockRanges_.end(), orderLuminosityBlockRange);
0179 }
0180
0181 else {
0182
0183
0184 std::ifstream listfile;
0185 listfile.open(listrunevents_.c_str());
0186 if (listfile.is_open()) {
0187 while (!listfile.eof()) {
0188 getline(listfile, line);
0189 ss.clear();
0190 ss.str(line);
0191 tokens.clear();
0192 while (ss >> buf) {
0193 tokens.push_back(buf);
0194
0195 }
0196
0197 if (tokens.size() < 3) {
0198
0199
0200 continue;
0201 }
0202 if (tokens[0] == "-" || tokens[0] == "+") {
0203
0204 if (tokens[0] == "-")
0205 whattodo.push_back(false);
0206 else
0207 whattodo.push_back(true);
0208
0209
0210 int loc = tokens[1].find(':', 0);
0211
0212 std::string first = tokens[1].substr(0, loc);
0213 startrun.push_back((edm::RunNumber_t)atoi(first.c_str()));
0214
0215 std::string last = tokens[1].substr(loc + 1, tokens[1].size());
0216 if (last == "infty")
0217 endrun.push_back(std::numeric_limits<unsigned int>::max());
0218 else
0219 endrun.push_back((edm::RunNumber_t)atoi(last.c_str()));
0220
0221
0222 loc = tokens[2].find(':', 0);
0223
0224 first = tokens[2].substr(0, loc);
0225 startevent.push_back((edm::EventNumber_t)atoi(first.c_str()));
0226
0227 last = tokens[2].substr(loc + 1, tokens[2].size());
0228 if (last == "infty")
0229 endevent.push_back(edm::EventID::maxEventNumber());
0230
0231 else
0232 endevent.push_back((edm::EventNumber_t)atoi(last.c_str()));
0233 }
0234 }
0235 listfile.close();
0236
0237 std::cout << "Summary from list of run/event number selection" << std::endl;
0238 for (unsigned int cond = 0; cond < whattodo.size(); cond++) {
0239 std::string what;
0240 if (whattodo[cond])
0241 what = "select";
0242 else
0243 what = "reject";
0244 std::cout << what << " ";
0245 std::cout << "from run " << startrun[cond] << " to run " << endrun[cond] << " ";
0246 std::cout << "from eve " << startevent[cond] << " to eve " << endevent[cond] << std::endl;
0247 }
0248 }
0249
0250 else
0251 std::cout << "Unable to open file";
0252
0253 }
0254 }
0255 void PickEvents::endJob() {
0256 using namespace std;
0257 std::cout << "================================================\n"
0258 << " n Events Analyzed ............... " << nEventsAnalyzed << std::endl
0259 << " n Events Selected ............... " << nEventsSelected << std::endl
0260 << "================================================\n\n";
0261 }
0262
0263 DEFINE_FWK_MODULE(PickEvents);