CMS 3D CMS Logo

AlCaHBHEMuonFilter.cc
Go to the documentation of this file.
1 // system include files
2 #include <atomic>
3 #include <memory>
4 #include <cmath>
5 #include <iostream>
6 #include <sstream>
7 #include <fstream>
8 
9 // user include files
21 
28 
31 
36 
37 //#define DebugLog
38 //
39 // class declaration
40 //
41 
42 namespace AlCaHBHEMuons {
43  struct Counters {
44  Counters() : nAll_(0), nGood_(0), nFinal_(0) {}
45  mutable std::atomic<unsigned int> nAll_, nGood_, nFinal_;
46  };
47 } // namespace AlCaHBHEMuons
48 
49 class AlCaHBHEMuonFilter : public edm::stream::EDFilter<edm::GlobalCache<AlCaHBHEMuons::Counters> > {
50 public:
52  ~AlCaHBHEMuonFilter() override;
53 
54  static std::unique_ptr<AlCaHBHEMuons::Counters> initializeGlobalCache(edm::ParameterSet const&) {
55  return std::make_unique<AlCaHBHEMuons::Counters>();
56  }
57 
58  bool filter(edm::Event&, edm::EventSetup const&) override;
59  void endStream() override;
60  static void globalEndJob(const AlCaHBHEMuons::Counters* counters);
61  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
62 
63 private:
64  void beginRun(edm::Run const&, edm::EventSetup const&) override;
65  void endRun(edm::Run const&, edm::EventSetup const&) override;
66 
67  // ----------member data ---------------------------
69  std::vector<std::string> trigNames_, HLTNames_;
71  bool pfCut_;
72  double trackIsoCut_, caloIsoCut_, pfIsoCut_;
73  int preScale_;
74  unsigned int nRun_, nAll_, nGood_, nFinal_;
79 };
80 
81 //
82 // constants, enums and typedefs
83 //
84 
85 //
86 // static data member definitions
87 //
88 
89 //
90 // constructors and destructor
91 //
93  : nRun_(0), nAll_(0), nGood_(0), nFinal_(0) {
94  //now do what ever initialization is needed
95  trigNames_ = iConfig.getParameter<std::vector<std::string> >("Triggers");
96  processName_ = iConfig.getParameter<std::string>("ProcessName");
97  triggerResults_ = iConfig.getParameter<edm::InputTag>("TriggerResultLabel");
98  labelMuon_ = iConfig.getParameter<edm::InputTag>("MuonLabel");
99  pfCut_ = iConfig.getParameter<bool>("PFCut");
100  pfIsoCut_ = iConfig.getParameter<double>("PFIsolationCut");
101  trackIsoCut_ = iConfig.getParameter<double>("TrackIsolationCut");
102  caloIsoCut_ = iConfig.getParameter<double>("CaloIsolationCut");
103  preScale_ = iConfig.getParameter<int>("PreScale");
104  if (preScale_ < 1)
105  preScale_ = 1;
106 
107  // define tokens for access
108  tok_trigRes_ = consumes<edm::TriggerResults>(triggerResults_);
109  tok_Muon_ = consumes<reco::MuonCollection>(labelMuon_);
110  edm::LogInfo("HBHEMuon") << "Parameters read from config file \n"
111  << "Process " << processName_ << " Prescale " << preScale_ << " Isolation Cuts "
112  << trackIsoCut_ << ":" << caloIsoCut_ << "\n";
113  for (unsigned int k = 0; k < trigNames_.size(); ++k)
114  edm::LogInfo("HBHEMuon") << "Trigger[" << k << "] " << trigNames_[k] << "\n";
115 } // AlCaHBHEMuonFilter::AlCaHBHEMuonFilter constructor
116 
118 
119 //
120 // member functions
121 //
122 
123 // ------------ method called on each new Event ------------
125  bool accept(false);
126  ++nAll_;
127 #ifdef DebugLog
128  edm::LogInfo("HBHEMuon") << "AlCaHBHEMuonFilter::Run " << iEvent.id().run() << " Event " << iEvent.id().event()
129  << " Luminosity " << iEvent.luminosityBlock() << " Bunch " << iEvent.bunchCrossing()
130  << std::endl;
131 #endif
132  //Step1: Find if the event passes one of the chosen triggers
135  iEvent.getByToken(tok_trigRes_, triggerResults);
136  if (triggerResults.isValid()) {
137  bool ok(false);
138  std::vector<std::string> modules;
139  const edm::TriggerNames& triggerNames = iEvent.triggerNames(*triggerResults);
140  const std::vector<std::string>& triggerNames_ = triggerNames.triggerNames();
141  for (unsigned int iHLT = 0; iHLT < triggerResults->size(); iHLT++) {
142  int hlt = triggerResults->accept(iHLT);
143  for (unsigned int i = 0; i < trigNames_.size(); ++i) {
144  if (triggerNames_[iHLT].find(trigNames_[i]) != std::string::npos) {
145  if (hlt > 0) {
146  ok = true;
147  }
148 #ifdef DebugLog
149  edm::LogInfo("HBHEMuon") << "AlCaHBHEMuonFilter::Trigger " << triggerNames_[iHLT] << " Flag " << hlt << ":"
150  << ok << std::endl;
151 #endif
152  }
153  }
154  }
155  if (ok) {
156  //Step2: Get geometry/B-field information
157  //Get magnetic field
159  iSetup.get<IdealMagneticFieldRecord>().get(bFieldH);
160  const MagneticField* bField = bFieldH.product();
161  // get handles to calogeometry
163  iSetup.get<CaloGeometryRecord>().get(pG);
164  const CaloGeometry* geo = pG.product();
165 
166  // Relevant blocks from iEvent
168  iEvent.getByToken(tok_Muon_, _Muon);
169 #ifdef DebugLog
170  edm::LogInfo("HBHEMuon") << "AlCaHBHEMuonFilter::Muon Handle " << _Muon.isValid() << std::endl;
171 #endif
172  if (_Muon.isValid()) {
173  for (reco::MuonCollection::const_iterator RecMuon = _Muon->begin(); RecMuon != _Muon->end(); ++RecMuon) {
174 #ifdef DebugLog
175  edm::LogInfo("HBHEMuon") << "AlCaHBHEMuonFilter::Muon:Track " << RecMuon->track().isNonnull()
176  << " innerTrack " << RecMuon->innerTrack().isNonnull() << " outerTrack "
177  << RecMuon->outerTrack().isNonnull() << " globalTrack "
178  << RecMuon->globalTrack().isNonnull() << std::endl;
179 #endif
180  if ((RecMuon->track().isNonnull()) && (RecMuon->innerTrack().isNonnull()) &&
181  (RecMuon->outerTrack().isNonnull()) && (RecMuon->globalTrack().isNonnull())) {
182  const reco::Track* pTrack = (RecMuon->innerTrack()).get();
183  spr::propagatedTrackID trackID = spr::propagateCALO(pTrack, geo, bField, false);
184 #ifdef DebugLog
185  edm::LogInfo("HBHEMuon") << "AlCaHBHEMuonFilter::Propagate: ECAL " << trackID.okECAL << " to HCAL "
186  << trackID.okHCAL << std::endl;
187 #endif
188  double trackIso = RecMuon->isolationR03().sumPt;
189  double caloIso = RecMuon->isolationR03().emEt + RecMuon->isolationR03().hadEt;
190  double isolR04 =
191  ((RecMuon->pfIsolationR04().sumChargedHadronPt +
192  std::max(0.,
193  RecMuon->pfIsolationR04().sumNeutralHadronEt + RecMuon->pfIsolationR04().sumPhotonEt -
194  (0.5 * RecMuon->pfIsolationR04().sumPUPt))) /
195  RecMuon->pt());
196  bool isoCut = (pfCut_) ? (isolR04 < pfIsoCut_) : ((trackIso < trackIsoCut_) && (caloIso < caloIsoCut_));
197  if ((trackID.okECAL) && (trackID.okHCAL) && isoCut) {
198  accept = true;
199  break;
200  }
201  }
202  }
203  }
204  }
205  }
206  // Step 4: Return the acceptance flag
207  if (accept) {
208  ++nGood_;
209  if (((nGood_ - 1) % preScale_) != 0) {
210  accept = false;
211  } else {
212  ++nFinal_;
213  }
214  }
215  return accept;
216 
217 } // AlCaHBHEMuonFilter::filter
218 
219 // ------------ method called once each job just after ending the event loop ------------
221  globalCache()->nAll_ += nAll_;
222  globalCache()->nGood_ += nGood_;
223  globalCache()->nFinal_ += nFinal_;
224 }
225 
227  edm::LogInfo("HBHEMuon") << "Selects " << count->nFinal_ << " out of " << count->nGood_ << " good events out of "
228  << count->nAll_ << " total # of events\n";
229 }
230 
231 // ------------ method called when starting to processes a run ------------
232 void AlCaHBHEMuonFilter::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
233  bool changed(false);
234  bool flag = hltConfig_.init(iRun, iSetup, processName_, changed);
235  edm::LogInfo("HBHEMuon") << "Run[" << nRun_ << "] " << iRun.run() << " hltconfig.init " << flag << std::endl;
236 }
237 
238 // ------------ method called when ending the processing of a run ------------
240  edm::LogInfo("HBHEMuon") << "endRun[" << nRun_ << "] " << iRun.run() << "\n";
241  nRun_++;
242 }
243 
244 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
246  //The following says we do not know what parameters are allowed so do no validation
247  // Please change this to state exactly what you do use, even if it is no parameters
249  desc.setUnknown();
250  descriptions.addDefault(desc);
251 }
252 
253 //define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:39
std::atomic< unsigned int > nGood_
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
std::vector< spr::propagatedTrackID > propagateCALO(edm::Handle< reco::TrackCollection > &trkCollection, const CaloGeometry *geo, const MagneticField *bField, const std::string &theTrackQuality, bool debug=false)
RunNumber_t run() const
Definition: RunBase.h:40
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
void endStream() override
edm::InputTag labelMuon_
bool accept() const
Has at least one path accepted the event?
std::atomic< unsigned int > nAll_
edm::EDGetTokenT< reco::MuonCollection > tok_Muon_
int bunchCrossing() const
Definition: EventBase.h:64
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
void beginRun(edm::Run const &, edm::EventSetup const &) override
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
std::atomic< unsigned int > nFinal_
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:30
Strings const & triggerNames() const
Definition: TriggerNames.cc:20
static void globalEndJob(const AlCaHBHEMuons::Counters *counters)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void addDefault(ParameterSetDescription const &psetDescription)
AlCaHBHEMuonFilter(edm::ParameterSet const &, const AlCaHBHEMuons::Counters *count)
unsigned int size() const
Get number of paths stored.
edm::InputTag triggerResults_
static std::string const triggerResults
Definition: EdmProvDump.cc:45
bool isValid() const
Definition: HandleBase.h:74
int k[5][pyjets_maxn]
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
static std::unique_ptr< AlCaHBHEMuons::Counters > initializeGlobalCache(edm::ParameterSet const &)
void endRun(edm::Run const &, edm::EventSetup const &) override
edm::EventID id() const
Definition: EventBase.h:59
T get() const
Definition: EventSetup.h:71
bool filter(edm::Event &, edm::EventSetup const &) override
std::vector< std::string > trigNames_
HLTConfigProvider hltConfig_
T const * product() const
Definition: ESHandle.h:86
edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const override
Definition: Event.cc:256
edm::EDGetTokenT< edm::TriggerResults > tok_trigRes_
Definition: Run.h:45
edm::EDGetTokenT< trigger::TriggerEvent > tok_trigEvt