Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:22

0001 #ifndef PhysicsTools_FWLite_EventSelectors_h
0002 #define PhysicsTools_FWLite_EventSelectors_h
0003 // these includes are FWLite-safe
0004 #include "DataFormats/FWLite/interface/Handle.h"
0005 #include "DataFormats/FWLite/interface/Event.h"
0006 // these are from ROOT, so they're safe too
0007 #include <TString.h>
0008 #include <TNamed.h>
0009 
0010 #if !defined(__CINT__) && !defined(__MAKECINT__)
0011 #include "PhysicsTools/FWLite/interface/ScannerHelpers.h"
0012 #endif
0013 
0014 namespace fwlite {
0015   class EventSelector : public TNamed {
0016   public:
0017     EventSelector(const char *name = "", const char *title = "") : TNamed(name, title) {}
0018     ~EventSelector() override {}
0019     virtual bool accept(const fwlite::EventBase &ev) const = 0;
0020   };
0021 
0022   class RunLumiSelector : public EventSelector {
0023   public:
0024     RunLumiSelector(const char *name = "", const char *title = "") : EventSelector(name, title) {}
0025     RunLumiSelector(int run, int firstLumi = 0, int lastLumi = 9999999)
0026         : EventSelector(TString::Format("run%d_lumi%d_%d", run, firstLumi, lastLumi),
0027                         TString::Format("Run %d, Lumi range [%d, %d]", run, firstLumi, lastLumi)) {
0028       add(run, firstLumi, lastLumi);
0029     }
0030 
0031     ~RunLumiSelector() override {}
0032     bool accept(const fwlite::EventBase &ev) const override { return accept(ev.id().run(), ev.luminosityBlock()); }
0033     void add(int run, int firstLumi = 0, int lastLumi = 9999999) {
0034       runs.push_back(run);
0035       firstLumis.push_back(firstLumi);
0036       lastLumis.push_back(lastLumi);
0037     }
0038     void clear() {
0039       runs.clear();
0040       firstLumis.clear();
0041       lastLumis.clear();
0042     }
0043     bool accept(int run, int luminosityBlock) const {
0044       if (runs.empty())
0045         return true;
0046       for (int i = 0, n = runs.size(); i < n; ++i) {
0047         if (runs[i] == run) {
0048           if ((firstLumis[i] <= luminosityBlock) && (luminosityBlock <= lastLumis[i]))
0049             return true;
0050         }
0051       }
0052       return false;
0053     }
0054 
0055   private:
0056     std::vector<int> runs, firstLumis, lastLumis;
0057   };
0058 
0059   template <typename Collection>
0060   class ObjectCountSelector : public EventSelector {
0061   public:
0062     ObjectCountSelector(const char *label,
0063                         const char *instance,
0064                         const char *process,
0065                         const char *cut,
0066                         int minNumber = 1,
0067                         int maxNumber = -1)
0068         : label_(label),
0069           instance_(instance),
0070           min_(minNumber),
0071           max_(maxNumber),
0072           scanner(
0073               new helper::ScannerBase(helper::Parser::elementType(edm::TypeWithDict(HandleT::TempWrapT::typeInfo())))) {
0074       scanner->setCut(cut);
0075       scanner->setIgnoreExceptions(true);
0076     }
0077     ~ObjectCountSelector() override { delete scanner; }
0078     bool accept(const fwlite::EventBase &ev) const override {
0079       int nfound = 0;
0080       HandleT handle;  // here, not as a datamember, otherwise CINT segfaults (!?)
0081       handle.getByLabel(ev, label_.c_str(), instance_.c_str(), process_.c_str());
0082       const Collection &vals = *handle;
0083       for (size_t j = 0, n = vals.size(); j < n; ++j) {
0084         if (scanner->test(&vals[j]))
0085           nfound++;
0086       }
0087       return (nfound >= min_ && (max_ == -1 || nfound <= max_));
0088     }
0089     void setCut(const char *cut) { scanner->setCut(cut); }
0090     void setMin(int minNumber) { min_ = minNumber; }
0091     void setMax(int maxNumber) { max_ = maxNumber; }
0092     void setIgnoreExceptions(bool ignoreThem = true) { scanner->setIgnoreExceptions(ignoreThem); }
0093 
0094   protected:
0095     typedef fwlite::Handle<Collection> HandleT;
0096     std::string label_, instance_, process_;
0097     int min_, max_;
0098     helper::ScannerBase *scanner;  // has to be a pointer, otherwise CINT segfaults in setCut (!?)
0099     // prevent copy c-tor and assignment
0100     ObjectCountSelector(const fwlite::ObjectCountSelector<Collection> &other);
0101     ObjectCountSelector &operator=(const fwlite::ObjectCountSelector<Collection> &other);
0102   };
0103 }  // namespace fwlite
0104 #endif  // PhysicsTools_FWLite_EventSelectors_h