CMS 3D CMS Logo

AlCaHBHEMuonProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //#define DebugLog
3 
4 // system include files
5 #include <atomic>
6 #include <memory>
7 #include <string>
8 #include <cmath>
9 #include <iostream>
10 #include <sstream>
11 #include <fstream>
12 #include <vector>
13 #include <boost/regex.hpp>
14 
15 // user include files
26 
36 
37 //
38 // class declaration
39 //
40 
41 namespace AlCaHBHEMuons {
42  struct Counters {
43  Counters() : nAll_(0), nGood_(0) {}
44  mutable std::atomic<unsigned int> nAll_, nGood_;
45  };
46 }
47 
48 class AlCaHBHEMuonProducer : public edm::stream::EDProducer<edm::GlobalCache<AlCaHBHEMuons::Counters> > {
49 public:
52 
53  static std::unique_ptr<AlCaHBHEMuons::Counters> initializeGlobalCache(edm::ParameterSet const&) {
54  return std::make_unique<AlCaHBHEMuons::Counters>();
55  }
56 
57  virtual void produce(edm::Event &, const edm::EventSetup&) override;
58  virtual void endStream() override;
59  static void globalEndJob(const AlCaHBHEMuons::Counters* counters);
60 
61 private:
62 
63  virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
64  virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
65  bool select(const reco::MuonCollection &);
66 
67  // ----------member data ---------------------------
68  unsigned int nRun_, nAll_, nGood_;
70  edm::InputTag labelEB_, labelEE_, labelHBHE_, labelMuon_;
71  double pMuonMin_;
72 
79 };
80 
81 
83  nRun_(0), nAll_(0), nGood_(0) {
84  //Get the run parameters
85  labelBS_ = iConfig.getParameter<edm::InputTag>("BeamSpotLabel");
86  labelVtx_ = iConfig.getParameter<edm::InputTag>("VertexLabel");
87  labelEB_ = iConfig.getParameter<edm::InputTag>("EBRecHitLabel");
88  labelEE_ = iConfig.getParameter<edm::InputTag>("EERecHitLabel");
89  labelHBHE_ = iConfig.getParameter<edm::InputTag>("HBHERecHitLabel");
90  labelMuon_ = iConfig.getParameter<edm::InputTag>("MuonLabel");
91  pMuonMin_ = iConfig.getParameter<double>("MinimumMuonP");
92 
93  // define tokens for access
94  tok_Vtx_ = consumes<reco::VertexCollection>(labelVtx_);
95  tok_BS_ = consumes<reco::BeamSpot>(labelBS_);
96  tok_EB_ = consumes<EcalRecHitCollection>(labelEB_);
97  tok_EE_ = consumes<EcalRecHitCollection>(labelEE_);
98  tok_HBHE_ = consumes<HBHERecHitCollection>(labelHBHE_);
99  tok_Muon_ = consumes<reco::MuonCollection>(labelMuon_);
100 
101  edm::LogInfo("HcalHBHEMuon") << "Parameters read from config file \n"
102  << "\t minP of muon " << pMuonMin_
103  << "\t input labels " << labelBS_ << " "
104  << labelVtx_ <<" " << labelEB_ << " " << labelEE_
105  <<" " << labelHBHE_ << " " << labelMuon_;
106 
107  //saves the following collections
108  produces<reco::BeamSpot>(labelBS_.label());
109  produces<reco::VertexCollection>(labelVtx_.label());
110  produces<EcalRecHitCollection>(labelEB_.instance());
111  produces<EcalRecHitCollection>(labelEE_.instance());
112  produces<HBHERecHitCollection>(labelHBHE_.label());
113  produces<reco::MuonCollection>(labelMuon_.label());
114 }
115 
117 
119 
120  ++nAll_;
121  bool valid(true);
122 #ifdef DebugLog
123  edm::LogInfo("HcalHBHEMuon") << "AlCaHBHEMuonProducer::Run "
124  << iEvent.id().run() << " Event "
125  << iEvent.id().event() << " Luminosity "
126  << iEvent.luminosityBlock() << " Bunch "
127  << iEvent.bunchCrossing();
128 #endif
129 
130  //Step1: Get all the relevant containers
132  iEvent.getByToken(tok_BS_, bmspot);
133  if (!bmspot.isValid()){
134  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelBS_;
135  valid = false;
136  }
137 
139  iEvent.getByToken(tok_Vtx_, vt);
140  if (!vt.isValid()) {
141  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelVtx_;
142  valid = false;
143  }
144 
145  edm::Handle<EcalRecHitCollection> barrelRecHitsHandle;
146  iEvent.getByToken(tok_EB_, barrelRecHitsHandle);
147  if (!barrelRecHitsHandle.isValid()) {
148  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelEB_;
149  valid = false;
150  }
151 
152  edm::Handle<EcalRecHitCollection> endcapRecHitsHandle;
153  iEvent.getByToken(tok_EE_, endcapRecHitsHandle);
154  if (!endcapRecHitsHandle.isValid()) {
155  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelEE_;
156  valid = false;
157  }
158 
160  iEvent.getByToken(tok_HBHE_, hbhe);
161  if (!hbhe.isValid()) {
162  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelHBHE_;
163  valid = false;
164  }
165 
167  iEvent.getByToken(tok_Muon_, muonhandle);
168  if (!muonhandle.isValid()) {
169  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelMuon_;
170  valid = false;
171  }
172 
173 #ifdef DebugLog
174  edm::LogInfo("HcalHBHEMuon") << "AlCaHBHEMuonProducer::obtained the collections with validity flag " << valid;
175 #endif
176 
177  //For accepted events
178  auto outputBeamSpot = std::make_unique<reco::BeamSpot>();
179  auto outputVColl = std::make_unique<reco::VertexCollection>();
180  auto outputEBColl = std::make_unique<EBRecHitCollection>();
181  auto outputEEColl = std::make_unique<EERecHitCollection>();
182  auto outputHBHEColl = std::make_unique<HBHERecHitCollection>();
183  auto outputMColl = std::make_unique<reco::MuonCollection>();
184 
185  if (valid) {
186  const reco::BeamSpot beam = *(bmspot.product());
187  outputBeamSpot = std::make_unique<reco::BeamSpot>(beam.position(),beam.sigmaZ(),
188  beam.dxdz(),beam.dydz(),beam.BeamWidthX(),
189  beam.covariance(),beam.type());
190  const reco::VertexCollection vtx = *(vt.product());
191  const EcalRecHitCollection ebcoll = *(barrelRecHitsHandle.product());
192  const EcalRecHitCollection eecoll = *(endcapRecHitsHandle.product());
193  const HBHERecHitCollection hbhecoll = *(hbhe.product());
194  const reco::MuonCollection muons = *(muonhandle.product());
195 
196  bool accept = select(muons);
197 
198  if (accept) {
199  ++nGood_;
200 
201  for (reco::VertexCollection::const_iterator vtr=vtx.begin(); vtr!=vtx.end(); ++vtr)
202  outputVColl->push_back(*vtr);
203 
204  for (edm::SortedCollection<EcalRecHit>::const_iterator ehit=ebcoll.begin(); ehit!=ebcoll.end(); ++ehit)
205  outputEBColl->push_back(*ehit);
206 
207  for (edm::SortedCollection<EcalRecHit>::const_iterator ehit=eecoll.begin(); ehit!=eecoll.end(); ++ehit)
208  outputEEColl->push_back(*ehit);
209 
210  for (std::vector<HBHERecHit>::const_iterator hhit=hbhecoll.begin(); hhit!=hbhecoll.end(); ++hhit)
211  outputHBHEColl->push_back(*hhit);
212 
213  for (reco::MuonCollection::const_iterator muon=muons.begin(); muon!=muons.end(); ++muon)
214  outputMColl->push_back(*muon);
215  }
216  }
217 
218  iEvent.put(std::move(outputBeamSpot), labelBS_.label());
219  iEvent.put(std::move(outputVColl), labelVtx_.label());
220  iEvent.put(std::move(outputEBColl), labelEB_.instance());
221  iEvent.put(std::move(outputEEColl), labelEE_.instance());
222  iEvent.put(std::move(outputHBHEColl), labelHBHE_.label());
223  iEvent.put(std::move(outputMColl), labelMuon_.label());
224 }
225 
227  globalCache()->nAll_ += nAll_;
228  globalCache()->nGood_ += nGood_;
229 }
230 
232  edm::LogInfo("HcalHBHEMuon") << "Finds " << count->nGood_ <<" good tracks in "
233  << count->nAll_ << " events";
234 }
235 
236 void AlCaHBHEMuonProducer::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
237  edm::LogInfo("HcalHBHEMuon") << "Run[" << nRun_ << "] " << iRun.run();
238 }
239 
241  ++nRun_;
242  edm::LogInfo("HcalHBHEMuon") << "endRun[" << nRun_ << "] " << iRun.run();
243 }
244 
246 
247  bool ok(false);
248  for (unsigned int k=0; k<muons.size(); ++k) {
249  if (muons[k].p() > pMuonMin_) {
250  ok = true; break;
251  }
252  }
253  return ok;
254 }
255 
257 
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
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
bool select(const reco::MuonCollection &)
RunNumber_t run() const
Definition: RunBase.h:40
static std::unique_ptr< AlCaHBHEMuons::Counters > initializeGlobalCache(edm::ParameterSet const &)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< EcalRecHitCollection > tok_EE_
std::atomic< unsigned int > nAll_
virtual void beginRun(edm::Run const &, edm::EventSetup const &) override
std::vector< T >::const_iterator const_iterator
int bunchCrossing() const
Definition: EventBase.h:64
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:30
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
virtual void endRun(edm::Run const &, edm::EventSetup const &) override
AlCaHBHEMuonProducer(edm::ParameterSet const &, const AlCaHBHEMuons::Counters *count)
double dydz() const
dydz slope
Definition: BeamSpot.h:84
int iEvent
Definition: GenABIO.cc:230
edm::EDGetTokenT< HBHERecHitCollection > tok_HBHE_
double BeamWidthX() const
beam width X
Definition: BeamSpot.h:86
bool isValid() const
Definition: HandleBase.h:75
double dxdz() const
dxdz slope
Definition: BeamSpot.h:82
int k[5][pyjets_maxn]
const_iterator end() const
T const * product() const
Definition: Handle.h:81
double sigmaZ() const
sigma z
Definition: BeamSpot.h:80
edm::EDGetTokenT< EcalRecHitCollection > tok_EB_
edm::EDGetTokenT< reco::BeamSpot > tok_BS_
std::string const & label() const
Definition: InputTag.h:36
double covariance(int i, int j) const
(i,j)-th element of error matrix
Definition: BeamSpot.h:112
edm::EventID id() const
Definition: EventBase.h:58
virtual void produce(edm::Event &, const edm::EventSetup &) override
edm::EDGetTokenT< reco::VertexCollection > tok_Vtx_
const Point & position() const
position
Definition: BeamSpot.h:62
static void globalEndJob(const AlCaHBHEMuons::Counters *counters)
std::string const & instance() const
Definition: InputTag.h:37
virtual void endStream() override
def move(src, dest)
Definition: eostools.py:510
edm::EDGetTokenT< reco::MuonCollection > tok_Muon_
const_iterator begin() const
Definition: Run.h:42
BeamType type() const
return beam type
Definition: BeamSpot.h:129