CMS 3D CMS Logo

FilterOutLowPt.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Alignment/CommonAlignment
4 // Class: FilterOutLowPt
5 //
6 //
7 // Original Author: Marco Musich
8 
9 #include <memory>
10 #include <vector>
11 #include <map>
12 #include <set>
13 #include <utility> // std::pair
14 #include "TMath.h"
15 
16 // user include files
17 
29 
31 public:
32  explicit FilterOutLowPt( const edm::ParameterSet & );
33  ~FilterOutLowPt() override;
34 
36 
37 private:
38  virtual void beginJob() ;
39  bool filter ( edm::Event &, const edm::EventSetup&) override;
40  virtual void endJob() ;
41 
42  // ----------member data --------
43  const bool applyfilter;
44  const bool debugOn;
45  const bool runControl_;
46  const double ptmin;
47  const double thresh;
48  const unsigned int numtrack;
49 
50  double trials;
51  double passes;
52 
53  const std::vector<unsigned int> runControlNumbers_;
54  std::map<unsigned int,std::pair<int,int>> eventsInRun_;
55 
58 
59 };
60 
62  applyfilter(iConfig.getUntrackedParameter<bool>("applyfilter")),
63  debugOn(iConfig.getUntrackedParameter<bool>("debugOn")),
64  runControl_(iConfig.getUntrackedParameter<bool>("runControl")),
65  ptmin(iConfig.getUntrackedParameter<double>("ptmin")),
66  thresh(iConfig.getUntrackedParameter<int>("thresh")),
67  numtrack(iConfig.getUntrackedParameter<unsigned int>("numtrack")),
68  runControlNumbers_(iConfig.getUntrackedParameter<std::vector<unsigned int> >("runControlNumber")),
69  theTrackCollectionToken(consumes<reco::TrackCollection>(iConfig.getUntrackedParameter<edm::InputTag>("src")))
70 {
71 }
72 
74 {
75 }
76 
78  trials=0;
79  passes=0;
80 }
81 
83 {
84 
85  bool passesRunControl = false;
86 
87  if(runControl_){
88  if (debugOn){
89  for(unsigned int runControlNumber : runControlNumbers_){
90  edm::LogInfo("FilterOutLowPt")<<"run number:" <<iEvent.id().run()<<" keeping runs:"<<runControlNumber<<std::endl;
91  }
92  }
93 
94  for(unsigned int runControlNumber : runControlNumbers_){
95  if(iEvent.eventAuxiliary().run() == runControlNumber){
96  if (debugOn){
97  edm::LogInfo("FilterOutLowPt")<<"run number"<< runControlNumber << " match!"<<std::endl;
98  }
99  passesRunControl = true;
100  break;
101  }
102  }
103  if (!passesRunControl) return false;
104  }
105 
106  trials++;
107 
108  bool accepted = false;
109  float fraction = 0;
110  // get GeneralTracks collection
111 
113  iEvent.getByToken(theTrackCollectionToken,tkRef);
114  const reco::TrackCollection* tkColl = tkRef.product();
115 
116  int numhighpurity=0;
118 
119  if(tkColl->size()>numtrack){
120  reco::TrackCollection::const_iterator itk = tkColl->begin();
121  reco::TrackCollection::const_iterator itk_e = tkColl->end();
122  for(;itk!=itk_e;++itk){
123  if( itk->quality(_trackQuality) &&
124  (itk->pt() >= ptmin)
125  ) numhighpurity++;
126  }
127  fraction = numhighpurity; //(float)tkColl->size();
128  if(fraction>=thresh) accepted=true;
129  }
130 
131  if (debugOn) {
132  int ievt = iEvent.id().event();
133  int irun = iEvent.id().run();
134  int ils = iEvent.luminosityBlock();
135  int bx = iEvent.bunchCrossing();
136 
137  edm::LogInfo("FilterOutLowPt")<<" Run " << irun << " Event " << ievt << " Lumi Block " << ils << " Bunch Crossing " << bx << " Fraction " << fraction << " NTracks " << tkColl->size() << " Accepted " << accepted << std::endl;
138 
139  }
140 
141  // count the trials and passes
142  unsigned int iRun = iEvent.id().run();
143  if (eventsInRun_.count(iRun)>0){
144  eventsInRun_[iRun].first+=1;
145  if(accepted) eventsInRun_[iRun].second+=1;
146  } else {
147  std::pair<int,int> mypass = std::make_pair(1,0);
148  if(accepted) mypass.second = 1;
149  eventsInRun_[iRun]= mypass;
150  }
151 
152  if (applyfilter){
153  if(accepted) passes++;
154  return accepted;
155  } else
156  return true;
157 
158 }
159 
161 
162  double eff = passes/trials;
163  double eff_err = std::sqrt((eff*(1-eff))/trials);
164 
165  edm::LogVerbatim("FilterOutLowPt")
166  <<"###################################### \n"
167  <<"# FilterOutLowPt::endJob() report \n"
168  <<"# Number of analyzed events: "<<trials<<" \n"
169  <<"# Number of accpeted events: "<<passes<<" \n"
170  <<"# Efficiency: "<< eff*100 << " +/- " << eff_err*100 << " %\n"
171  <<"######################################";
172 
173  edm::LogVerbatim("FilterOutLowPt")<<"###################################### \n"
174  <<"# Filter Summary events accepted by run";
175  for (auto & it : eventsInRun_)
176  edm::LogVerbatim("FilterOutLowPt")<<"# run:" << it.first << " => events tested: " << (it.second).first << " | events passed: " << (it.second).second;
177  edm::LogVerbatim("FilterOutLowPt")<<"###################################### \n";
178 }
179 
180 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
181 void
183 
184  std::vector<unsigned int> defaultRuns;
185  defaultRuns.push_back(0);
186 
188  desc.setComment("Filters out soft events without at least `numtrack` tracks with pT> `ptmin`");
189  desc.addUntracked<bool>("applyfilter",true);
190  desc.addUntracked<bool>("debugOn",false);
191  desc.addUntracked<bool>("runControl",false);
192  desc.addUntracked<unsigned int>("numtrack",0);
193  desc.addUntracked<double>("ptmin",3.);
194  desc.addUntracked<int>("thresh",1);
195  desc.addUntracked<edm::InputTag>("src",edm::InputTag("generalTracks"));
196  desc.addUntracked<std::vector<unsigned int> >("runControlNumber",defaultRuns);
197  descriptions.add("filterOutLowPt", desc);
198 }
199 
200 //define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:39
edm::EDGetTokenT< reco::TrackCollection > theTrackCollectionToken
EventNumber_t event() const
Definition: EventID.h:41
EventAuxiliary const & eventAuxiliary() const override
Definition: Event.h:92
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
const double thresh
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
TrackQuality
track quality
Definition: TrackBase.h:151
const unsigned int numtrack
virtual void beginJob()
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:15
int bunchCrossing() const
Definition: EventBase.h:64
RunNumber_t run() const
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
virtual void endJob()
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
void setComment(std::string const &value)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::map< unsigned int, std::pair< int, int > > eventsInRun_
T sqrt(T t)
Definition: SSEVec.h:18
FilterOutLowPt(const edm::ParameterSet &)
bool filter(edm::Event &, const edm::EventSetup &) override
const double ptmin
reco::TrackBase::TrackQuality _trackQuality
const bool debugOn
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:134
T const * product() const
Definition: Handle.h:74
bool accepted(std::vector< std::string_view > const &, std::string_view)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
const bool applyfilter
edm::EventID id() const
Definition: EventBase.h:59
fixed size matrix
HLT enums.
~FilterOutLowPt() override
const bool runControl_
const std::vector< unsigned int > runControlNumbers_
static void fillDescriptions(edm::ConfigurationDescriptions &)