CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/PhysicsTools/FWLite/interface/EventSelectors.h

Go to the documentation of this file.
00001 // these includes are FWLite-safe
00002 #include "DataFormats/FWLite/interface/Handle.h"
00003 #include "DataFormats/FWLite/interface/Event.h"
00004 // these are from ROOT, so they're safe too
00005 #include <TString.h>
00006 #include <TNamed.h>
00007 
00008 #if !defined(__CINT__) && !defined(__MAKECINT__)
00009 #include "PhysicsTools/FWLite/interface/ScannerHelpers.h"
00010 #endif
00011 
00012 namespace fwlite {
00013     class EventSelector : public TNamed {
00014         public:
00015             EventSelector(const char *name="", const char *title="") : TNamed(name,title) {}
00016             virtual ~EventSelector() {}
00017             virtual bool accept(const fwlite::EventBase &ev) const = 0;
00018     };    
00019 
00020     class RunLumiSelector : public EventSelector {
00021         public:
00022             RunLumiSelector(const char *name="", const char *title="") : EventSelector(name,title) {}
00023             RunLumiSelector(int run, int firstLumi=0, int lastLumi = 9999999) :
00024                 EventSelector(TString::Format("run%d_lumi%d_%d", run, firstLumi, lastLumi),
00025                               TString::Format("Run %d, Lumi range [%d, %d]", run, firstLumi, lastLumi))
00026                 { add(run, firstLumi, lastLumi); }
00027 
00028             virtual ~RunLumiSelector() {}
00029             virtual bool accept(const fwlite::EventBase &ev) const {
00030                 return accept(ev.id().run(), ev.luminosityBlock());
00031             }
00032             void add(int run, int firstLumi=0, int lastLumi = 9999999) {
00033                 runs.push_back(run);
00034                 firstLumis.push_back(firstLumi);
00035                 lastLumis.push_back(lastLumi);
00036             }
00037             void clear() {
00038                 runs.clear();
00039                 firstLumis.clear();
00040                 lastLumis.clear();
00041             }
00042             bool accept(int run, int luminosityBlock) const {
00043                 if (runs.empty()) return true;
00044                 for (int i = 0, n = runs.size(); i < n; ++i) {
00045                     if (runs[i] == run) {
00046                         if ((firstLumis[i] <= luminosityBlock) && (luminosityBlock <= lastLumis[i])) return true;
00047                     }
00048                 }
00049                 return false;
00050             }
00051                         
00052         private:
00053             std::vector<int> runs, firstLumis, lastLumis;
00054     };
00055 
00056     template<typename Collection>
00057     class ObjectCountSelector :  public EventSelector {
00058         public:
00059             ObjectCountSelector(const char *label, const char *instance, const char *process,
00060                                 const char *cut,   int minNumber=1, int maxNumber=-1) :
00061                 label_(label), instance_(instance),
00062                 min_(minNumber), max_(maxNumber),
00063                 scanner(new helper::ScannerBase(helper::Parser::elementType(Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo()))))
00064             {
00065                 scanner->setCut(cut);
00066                 scanner->setIgnoreExceptions(true);
00067             }
00068             ~ObjectCountSelector() { delete scanner; }
00069             virtual bool accept(const fwlite::EventBase &ev) const {
00070                 int nfound = 0;
00071                 HandleT handle; // here, not as a datamember, otherwise CINT segfaults (!?)
00072                 handle.getByLabel(ev, label_.c_str(), instance_.c_str(), process_.c_str());
00073                 const Collection & vals = *handle;
00074                 for (size_t j = 0, n = vals.size(); j < n; ++j) {
00075                     if (scanner->test(&vals[j])) nfound++;
00076                 }
00077                 return (nfound >= min_ && (max_ == -1 || nfound <= max_));
00078             }
00079             void setCut(const char *cut) { scanner->setCut(cut); }
00080             void setMin(int minNumber)   { min_ = minNumber; }
00081             void setMax(int maxNumber)   { max_ = maxNumber; }
00082             void setIgnoreExceptions(bool ignoreThem=true) { scanner->setIgnoreExceptions(ignoreThem); }
00083         protected:
00084             typedef fwlite::Handle<Collection> HandleT;
00085             std::string    label_, instance_, process_;
00086             int min_, max_;
00087             helper::ScannerBase *scanner; // has to be a pointer, otherwise CINT segfaults in setCut (!?)
00088             // prevent copy c-tor and assignment
00089             ObjectCountSelector(const fwlite::ObjectCountSelector<Collection> &other) ;
00090             ObjectCountSelector & operator=(const fwlite::ObjectCountSelector<Collection> &other) ;
00091     };
00092 }