CMS 3D CMS Logo

AlCaHBHEMuonProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //#define EDM_ML_DEBUG
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
25 
35 
36 //#define EDM_ML_DEBUG
37 //
38 // class declaration
39 //
40 
42  struct Counters {
43  Counters() : nAll_(0), nGood_(0) {}
44  mutable std::atomic<unsigned int> nAll_, nGood_;
45  };
46 } // namespace alCaHBHEMuonProducer
47 
48 class AlCaHBHEMuonProducer : public edm::stream::EDProducer<edm::GlobalCache<alCaHBHEMuonProducer::Counters> > {
49 public:
51  ~AlCaHBHEMuonProducer() override;
52 
53  static std::unique_ptr<alCaHBHEMuonProducer::Counters> initializeGlobalCache(edm::ParameterSet const&) {
54  return std::make_unique<alCaHBHEMuonProducer::Counters>();
55  }
56 
57  void produce(edm::Event&, const edm::EventSetup&) override;
58  void endStream() override;
60  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
61 
62 private:
63  void beginRun(edm::Run const&, edm::EventSetup const&) override;
64  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_;
71  const double pMuonMin_;
72 
79 };
80 
83  : nRun_(0),
84  nAll_(0),
85  nGood_(0),
86  labelBS_(iConfig.getParameter<edm::InputTag>("BeamSpotLabel")),
87  labelVtx_(iConfig.getParameter<edm::InputTag>("VertexLabel")),
88  labelEB_(iConfig.getParameter<edm::InputTag>("EBRecHitLabel")),
89  labelEE_(iConfig.getParameter<edm::InputTag>("EERecHitLabel")),
90  labelHBHE_(iConfig.getParameter<edm::InputTag>("HBHERecHitLabel")),
91  labelMuon_(iConfig.getParameter<edm::InputTag>("MuonLabel")),
92  pMuonMin_(iConfig.getParameter<double>("MinimumMuonP")) {
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::LogVerbatim("HcalHBHEMuon") << "Parameters read from config file \n"
102  << "\t minP of muon " << pMuonMin_ << "\t input labels " << labelBS_ << " "
103  << labelVtx_ << " " << labelEB_ << " " << labelEE_ << " " << labelHBHE_ << " "
104  << labelMuon_;
105 
106  //saves the following collections
107  produces<reco::BeamSpot>(labelBS_.label());
108  produces<reco::VertexCollection>(labelVtx_.label());
109  produces<EcalRecHitCollection>(labelEB_.instance());
110  produces<EcalRecHitCollection>(labelEE_.instance());
111  produces<HBHERecHitCollection>(labelHBHE_.label());
112  produces<reco::MuonCollection>(labelMuon_.label());
113 }
114 
116 
118  ++nAll_;
119  bool valid(true);
120 #ifdef EDM_ML_DEBUG
121  edm::LogVerbatim("HcalHBHEMuon") << "AlCaHBHEMuonProducer::Run " << iEvent.id().run() << " Event "
122  << iEvent.id().event() << " Luminosity " << iEvent.luminosityBlock() << " Bunch "
123  << iEvent.bunchCrossing();
124 #endif
125 
126  //Step1: Get all the relevant containers
127  auto bmspot = iEvent.getHandle(tok_BS_);
128  if (!bmspot.isValid()) {
129  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelBS_;
130  valid = false;
131  }
132 
133  auto vt = iEvent.getHandle(tok_Vtx_);
134  if (!vt.isValid()) {
135  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelVtx_;
136  valid = false;
137  }
138 
139  auto barrelRecHitsHandle = iEvent.getHandle(tok_EB_);
140  if (!barrelRecHitsHandle.isValid()) {
141  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelEB_;
142  valid = false;
143  }
144 
145  auto endcapRecHitsHandle = iEvent.getHandle(tok_EE_);
146  if (!endcapRecHitsHandle.isValid()) {
147  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelEE_;
148  valid = false;
149  }
150 
151  auto hbhe = iEvent.getHandle(tok_HBHE_);
152  if (!hbhe.isValid()) {
153  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelHBHE_;
154  valid = false;
155  }
156 
157  auto muonhandle = iEvent.getHandle(tok_Muon_);
158  if (!muonhandle.isValid()) {
159  edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelMuon_;
160  valid = false;
161  }
162 
163 #ifdef EDM_ML_DEBUG
164  edm::LogVerbatim("HcalHBHEMuon") << "AlCaHBHEMuonProducer::obtained the collections with validity flag " << valid;
165 #endif
166 
167  //For accepted events
168  auto outputBeamSpot = std::make_unique<reco::BeamSpot>();
169  auto outputVColl = std::make_unique<reco::VertexCollection>();
170  auto outputEBColl = std::make_unique<EBRecHitCollection>();
171  auto outputEEColl = std::make_unique<EERecHitCollection>();
172  auto outputHBHEColl = std::make_unique<HBHERecHitCollection>();
173  auto outputMColl = std::make_unique<reco::MuonCollection>();
174 
175  if (valid) {
176  const reco::BeamSpot beam = *(bmspot.product());
177  outputBeamSpot = std::make_unique<reco::BeamSpot>(
178  beam.position(), beam.sigmaZ(), beam.dxdz(), beam.dydz(), beam.BeamWidthX(), beam.covariance(), beam.type());
179  const reco::VertexCollection vtx = *(vt.product());
180  const EcalRecHitCollection ebcoll = *(barrelRecHitsHandle.product());
181  const EcalRecHitCollection eecoll = *(endcapRecHitsHandle.product());
182  const HBHERecHitCollection hbhecoll = *(hbhe.product());
183  const reco::MuonCollection muons = *(muonhandle.product());
184 
185  bool accept = select(muons);
186 
187  if (accept) {
188  ++nGood_;
189 
190  for (reco::VertexCollection::const_iterator vtr = vtx.begin(); vtr != vtx.end(); ++vtr)
191  outputVColl->push_back(*vtr);
192 
193  for (edm::SortedCollection<EcalRecHit>::const_iterator ehit = ebcoll.begin(); ehit != ebcoll.end(); ++ehit)
194  outputEBColl->push_back(*ehit);
195 
196  for (edm::SortedCollection<EcalRecHit>::const_iterator ehit = eecoll.begin(); ehit != eecoll.end(); ++ehit)
197  outputEEColl->push_back(*ehit);
198 
199  for (std::vector<HBHERecHit>::const_iterator hhit = hbhecoll.begin(); hhit != hbhecoll.end(); ++hhit)
200  outputHBHEColl->push_back(*hhit);
201 
202  for (reco::MuonCollection::const_iterator muon = muons.begin(); muon != muons.end(); ++muon)
203  outputMColl->push_back(*muon);
204  }
205  }
206 
207  iEvent.put(std::move(outputBeamSpot), labelBS_.label());
208  iEvent.put(std::move(outputVColl), labelVtx_.label());
209  iEvent.put(std::move(outputEBColl), labelEB_.instance());
210  iEvent.put(std::move(outputEEColl), labelEE_.instance());
211  iEvent.put(std::move(outputHBHEColl), labelHBHE_.label());
212  iEvent.put(std::move(outputMColl), labelMuon_.label());
213 }
214 
216  globalCache()->nAll_ += nAll_;
217  globalCache()->nGood_ += nGood_;
218 }
219 
221  edm::LogVerbatim("HcalHBHEMuon") << "Finds " << count->nGood_ << " good tracks in " << count->nAll_ << " events";
222 }
223 
225  //The following says we do not know what parameters are allowed so do no validation
226  // Please change this to state exactly what you do use, even if it is no parameters
228  desc.add<edm::InputTag>("BeamSpotLabel", edm::InputTag("offlineBeamSpot"));
229  desc.add<edm::InputTag>("VertexLabel", edm::InputTag("offlinePrimaryVertices"));
230  desc.add<edm::InputTag>("EBRecHitLabel", edm::InputTag("ecalRecHit", "EcalRecHitsEB"));
231  desc.add<edm::InputTag>("EERecHitLabel", edm::InputTag("ecalRecHit", "EcalRecHitsEE"));
232  desc.add<edm::InputTag>("HBHERecHitLabel", edm::InputTag("hbhereco"));
233  desc.add<edm::InputTag>("MuonLabel", edm::InputTag("muons"));
234  desc.add<double>("MinimumMuonP", 5.0);
235  descriptions.add("alcaHBHEMuonProducer", desc);
236 }
237 
238 void AlCaHBHEMuonProducer::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
239  edm::LogVerbatim("HcalHBHEMuon") << "Run[" << nRun_ << "] " << iRun.run();
240 }
241 
243  ++nRun_;
244  edm::LogVerbatim("HcalHBHEMuon") << "endRun[" << nRun_ << "] " << iRun.run();
245 }
246 
248  bool ok(false);
249  for (unsigned int k = 0; k < muons.size(); ++k) {
250  if (muons[k].p() > pMuonMin_) {
251  ok = true;
252  break;
253  }
254  }
255  return ok;
256 }
257 
259 
Log< level::Info, true > LogVerbatim
bool select(const reco::MuonCollection &)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::string const & instance() const
Definition: InputTag.h:37
edm::EDGetTokenT< EcalRecHitCollection > tok_EE_
const edm::InputTag labelEE_
void beginRun(edm::Run const &, edm::EventSetup const &) override
std::vector< T >::const_iterator const_iterator
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
std::string const & label() const
Definition: InputTag.h:36
const edm::InputTag labelHBHE_
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:31
const edm::InputTag labelBS_
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
void endRun(edm::Run const &, edm::EventSetup const &) override
TupleMultiplicity< TrackerTraits > const HitToTuple< TrackerTraits > const cms::cuda::AtomicPairCounter GPUCACellT< TrackerTraits > const *__restrict__ uint32_t const *__restrict__ CellNeighborsVector< TrackerTraits > const CellTracksVector< TrackerTraits > const OuterHitOfCell< TrackerTraits > const int32_t uint32_t Counters * counters
int iEvent
Definition: GenABIO.cc:224
RunNumber_t run() const
Definition: RunBase.h:40
const edm::InputTag labelEB_
edm::EDGetTokenT< HBHERecHitCollection > tok_HBHE_
std::atomic< unsigned int > nAll_
const edm::InputTag labelVtx_
const edm::InputTag labelMuon_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const_iterator begin() const
AlCaHBHEMuonProducer(edm::ParameterSet const &, const alCaHBHEMuonProducer::Counters *count)
const_iterator end() const
std::atomic< unsigned int > nGood_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
edm::EDGetTokenT< EcalRecHitCollection > tok_EB_
edm::EDGetTokenT< reco::BeamSpot > tok_BS_
static std::unique_ptr< alCaHBHEMuonProducer::Counters > initializeGlobalCache(edm::ParameterSet const &)
HLT enums.
void produce(edm::Event &, const edm::EventSetup &) override
edm::EDGetTokenT< reco::VertexCollection > tok_Vtx_
Log< level::Warning, false > LogWarning
static void globalEndJob(const alCaHBHEMuonProducer::Counters *counters)
def move(src, dest)
Definition: eostools.py:511
edm::EDGetTokenT< reco::MuonCollection > tok_Muon_
Definition: Run.h:45