CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/DPGAnalysis/Skims/src/PickEvents.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    PickEvents
00004 // Class:      PickEvents
00005 // 
00013 //
00014 // Original Author:  Michael Henry Schmitt
00015 //         Created:  Mon Sep 15 19:36:37 CEST 2008
00016 // $Id: PickEvents.cc,v 1.4 2010/08/07 14:55:55 wmtan Exp $
00017 //         Modified: 27/03/2009 Luca Malgeri
00018 //                   reading external file, defining selection syntax
00019 //
00020 //
00021 
00022 
00023 // system include files
00024 #include <memory>
00025 #include <iostream>
00026 #include <fstream>
00027 #include <vector>
00028 #include <sstream>
00029 #include <limits>
00030 
00031 // user include files
00032 #include "FWCore/Framework/interface/Frameworkfwd.h"
00033 #include "FWCore/Framework/interface/EDFilter.h"
00034 
00035 #include "FWCore/Framework/interface/Event.h"
00036 #include "FWCore/Framework/interface/MakerMacros.h"
00037 
00038 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00039 #include "FWCore/ParameterSet/interface/FileInPath.h"
00040 
00041 
00042 //
00043 // class declaration
00044 //
00045 
00046 class PickEvents : public edm::EDFilter {
00047    public:
00048       explicit PickEvents(const edm::ParameterSet&);
00049       ~PickEvents();
00050 
00051    private:
00052       virtual void beginJob() ;
00053       virtual bool filter(edm::Event&, const edm::EventSetup&);
00054       virtual void endJob() ;
00055 
00056   std::string listrunevents_; 
00057   std::string listruneventsinpath_; 
00058   
00059   std::vector<bool> whattodo;
00060   std::vector<edm::RunNumber_t> startrun;
00061   std::vector<edm::RunNumber_t> endrun;
00062   std::vector<edm::EventNumber_t> startevent;
00063   std::vector<edm::EventNumber_t> endevent;
00064   
00065   int nEventsAnalyzed;
00066   int nEventsSelected;
00067       
00068 };
00069 
00070 
00071 
00072 PickEvents::PickEvents(const edm::ParameterSet& iConfig)
00073 {
00074 
00075   listruneventsinpath_=iConfig.getUntrackedParameter<std::string> ("RunEventList","");
00076   edm::FileInPath listruneventstmp("DPGAnalysis/Skims/data/"+listruneventsinpath_);
00077 
00078   listrunevents_=listruneventstmp.fullPath();
00079 
00080   std::cout <<"File with run/event list:"<< listrunevents_<<std::endl;
00081 
00082 }
00083 
00084 
00085 PickEvents::~PickEvents()
00086 {
00087 }
00088 
00089 bool
00090 PickEvents::filter(edm::Event& iEvent, const edm::EventSetup& iSetup)
00091 {
00092    using namespace edm;
00093 
00094    RunNumber_t kRun   = iEvent.id().run();
00095    EventNumber_t kEvent = iEvent.id().event();
00096 
00097    bool selectThisEvent = false;
00098 
00099    for (unsigned int cond=0; cond<whattodo.size();cond++)
00100      {
00101        //       std::string what;
00102        if ( kRun>=startrun[cond] && 
00103             kRun<=endrun[cond]   &&
00104             kEvent>=startevent[cond] && 
00105             kEvent<=endevent[cond] ) 
00106          { // it's in the range, use
00107            selectThisEvent=whattodo[cond];
00108          }
00109      }
00110                
00111    nEventsAnalyzed++;
00112    if (selectThisEvent) nEventsSelected++;
00113    //   if (selectThisEvent) std::cout << "Event selected: " << kRun << " " << kEvent << std::endl;
00114 
00115    return selectThisEvent;
00116 }
00117 
00118 void 
00119 PickEvents::beginJob()
00120 {
00121   using namespace std;
00122 
00123   std::string line;
00124   std::string buf;
00125 
00126   std::stringstream ss;
00127   std::vector<std::string> tokens;
00128 
00129   nEventsAnalyzed = 0;
00130   nEventsSelected = 0;
00131 
00132   // open file listevent file
00133   ifstream listfile;
00134   listfile.open(listrunevents_.c_str());
00135   if (listfile.is_open())
00136     {
00137       while (! listfile.eof() )
00138         {
00139           getline (listfile,line);
00140           ss.clear();
00141           ss.str(line);
00142           tokens.clear();
00143           while (ss>>buf)
00144             {
00145               tokens.push_back(buf);
00146               //              std::cout << buf << std::endl;
00147             }         
00148           // std::cout << tokens.size() << std::endl;
00149           if (tokens.size()<3)
00150             {
00151               //              std::cout << "strange selection line:" << line << std::endl;
00152               //              std::cout << "skipping it" << std::endl;
00153               continue;
00154             }
00155           if(tokens[0]=="-" || tokens[0]=="+")
00156             {
00157               // it's a selection line, use it
00158               if(tokens[0]=="-") whattodo.push_back(false);
00159               else whattodo.push_back(true);
00160 
00161               // start with run selecion
00162               int loc=tokens[1].find(":",0);
00163 
00164               std::string first=tokens[1].substr(0,loc);
00165               startrun.push_back((edm::RunNumber_t)atoi(first.c_str()));
00166 
00167               std::string last=tokens[1].substr(loc+1,tokens[1].size());
00168               if (last=="infty")
00169                 endrun.push_back(std::numeric_limits<unsigned int>::max());
00170               else
00171                 endrun.push_back((edm::RunNumber_t) atoi(last.c_str()));
00172 
00173               // then event number selecion
00174               loc=tokens[2].find(":",0);
00175 
00176               first=tokens[2].substr(0,loc);
00177               startevent.push_back((edm::EventNumber_t)atoi(first.c_str()));
00178 
00179               last=tokens[2].substr(loc+1,tokens[2].size());
00180               if (last=="infty")
00181                 endevent.push_back(edm::EventID::maxEventNumber());
00182               //                endevent.push_back(std::numeric_limits<long long int>::max());
00183               else
00184                 endevent.push_back((edm::EventNumber_t)atoi(last.c_str()));
00185 
00186             }
00187         }
00188       listfile.close();
00189       // printout summary
00190       std::cout << "Summary from list of run/event number selection" << std::endl;
00191       for (unsigned int cond=0; cond<whattodo.size();cond++)
00192         {
00193           std::string what;
00194           if(whattodo[cond]) what="select";
00195           else what="reject";
00196           std::cout << what << " "; 
00197           std::cout << "from run " << startrun[cond] << " to run " << endrun[cond] << " ";
00198           std::cout << "from eve " << startevent[cond] << " to eve " << endevent[cond] << std::endl; 
00199         }
00200     }
00201 
00202   else std::cout << "Unable to open file"; 
00203 
00204 }
00205 void 
00206 PickEvents::endJob() {
00207   using namespace std;
00208   std::cout << "================================================\n"
00209        << "  n Events Analyzed ............... " << nEventsAnalyzed << std::endl
00210        << "  n Events Selected ............... " << nEventsSelected<< std::endl
00211        << "================================================\n\n" ;
00212 }
00213 
00214 
00215 DEFINE_FWK_MODULE(PickEvents);