Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-07 22:41:50

0001 // -*- C++ -*-
0002 //
0003 // Package:    PickEvents
0004 // Class:      PickEvents
0005 //
0006 /**\class PickEvents PickEvents.cc DPGAnalysis/PickEvents/src/PickEvents.cc
0007 
0008  Description: <one line class summary>
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Michael Henry Schmitt
0015 //         Created:  Mon Sep 15 19:36:37 CEST 2008
0016 // $Id: PickEvents.cc,v 1.4 2010/08/07 14:55:55 wmtan Exp $
0017 //         Modified: 27/03/2009 Luca Malgeri
0018 //                   reading external file, defining selection syntax
0019 //         Modified: 30/06/2014 Giovanni Franzoni
0020 //                   reading run-lumisection list from json
0021 //
0022 //
0023 
0024 // system include files
0025 #include <memory>
0026 #include <iostream>
0027 #include <fstream>
0028 #include <vector>
0029 #include <sstream>
0030 #include <limits>
0031 #include <cassert>
0032 
0033 // user include files
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 // ordering function to sort LuminosityBlockRange based on the starting run number
0046 bool orderLuminosityBlockRange(edm::LuminosityBlockRange u, edm::LuminosityBlockRange v) {
0047   return (u.startRun() < v.startRun());
0048 }
0049 
0050 //
0051 // class declaration
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   // sanity checks
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   // two alternative definition of the filter selection are possible, according to isRunLsBased_
0117 
0118   if (isRunLsBased_) {
0119     // std::cout << "GF DEBUG: kRun is " << kRun << " kLumi is: " << kLumi << std::endl;
0120 
0121     for (std::vector<edm::LuminosityBlockRange>::iterator oneLumiRange = luminositySectionsBlockRanges_.begin();
0122          oneLumiRange != luminositySectionsBlockRanges_.end();
0123          ++oneLumiRange) {
0124       // luminositySectionsBlockRanges_ is sorted according to startRun()
0125       // => if kRun below it, you can stop the loop and return false
0126       if (kRun < (*oneLumiRange).startRun()) {
0127         // std::cout << "GF DEBUG: LS has NOT PASSED (early bail-out) ! ***" << std::endl;
0128         break;
0129       }
0130 
0131       // if endRun() below kRun, go to the next iteration
0132       if ((*oneLumiRange).endRun() < kRun)
0133         continue;
0134 
0135       // if the run number and lumi section match => exit from the loop
0136       if ((*oneLumiRange).startLumi() <= kLumi && kLumi <= (*oneLumiRange).endLumi()) {
0137         selectThisEvent = true;
0138         // std::cout << "GF DEBUG: LS HAS PASSED ! ***" << std::endl;
0139         break;
0140       }
0141     }
0142 
0143   }       // end of isRunLsBased_
0144   else {  // !isRunLsBased_
0145 
0146     for (unsigned int cond = 0; cond < whattodo.size(); cond++) {
0147       //       std::string what;
0148       if (kRun >= startrun[cond] && kRun <= endrun[cond] && kEvent >= startevent[cond] &&
0149           kEvent <= endevent[cond]) {  // it's in the range, use
0150         selectThisEvent = whattodo[cond];
0151       }
0152     }  // loop on whattodo
0153 
0154   }  // !isRunLsBased_
0155 
0156   nEventsAnalyzed++;
0157   if (selectThisEvent)
0158     nEventsSelected++;
0159   //   if (selectThisEvent) std::cout << "Event selected: " << kRun << " " << kEvent << std::endl;
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     // sorting luminositySectionsBlockRanges_ according to the starting run of the block allows the speedup the search by an average factor 2
0178     std::sort(luminositySectionsBlockRanges_.begin(), luminositySectionsBlockRanges_.end(), orderLuminosityBlockRange);
0179   }  // if isRunLsBased_
0180 
0181   else {  // !isRunLsBased_
0182 
0183     // open file listevent file
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           //          std::cout << buf << std::endl;
0195         }
0196         // std::cout << tokens.size() << std::endl;
0197         if (tokens.size() < 3) {
0198           //          std::cout << "strange selection line:" << line << std::endl;
0199           //          std::cout << "skipping it" << std::endl;
0200           continue;
0201         }
0202         if (tokens[0] == "-" || tokens[0] == "+") {
0203           // it's a selection line, use it
0204           if (tokens[0] == "-")
0205             whattodo.push_back(false);
0206           else
0207             whattodo.push_back(true);
0208 
0209           // start with run selecion
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           // then event number selecion
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           //        endevent.push_back(std::numeric_limits<long long int>::max());
0231           else
0232             endevent.push_back((edm::EventNumber_t)atoi(last.c_str()));
0233         }
0234       }
0235       listfile.close();
0236       // printout summary
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   }  // !isRunLsBased_
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);