CMS 3D CMS Logo

CosmicRateAnalyzer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CosmicTrackTool/CosmicRateAnalyzer
4 // Class: CosmicRateAnalyzer
5 //
17 // Originally created: Justyna Magdalena Tomaszewska,,,
18 // Revisited by: Ashutosh Bhardwaj and Kirti Ranjan
19 // Further Developed by: Sumit Keshri (sumit.keshri@cern.ch)
20 //
21 // Created: Sat, 30 May 2015 20:14:35 GMT
22 //
23 
24 // system include files
25 #include <memory>
26 
27 // user include files
38 
42 
46 
51 
53 
57 
65 
66 #include <vector>
67 #include <string>
68 #include <iostream>
69 #include <fstream>
70 #include <iomanip>
71 #include <TTree.h>
72 
73 //
74 // class declaration
75 //
76 class CosmicRateAnalyzer : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
77 public:
78  explicit CosmicRateAnalyzer(const edm::ParameterSet&);
79  ~CosmicRateAnalyzer() override;
80 
81  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
82 
83 private:
84  void beginJob() override;
85  void analyze(const edm::Event&, const edm::EventSetup&) override;
86  void endJob() override;
87 
88  void beginRun(edm::Run const&, edm::EventSetup const&) override;
89  void endRun(edm::Run const&, edm::EventSetup const&) override;
90 
91  static double stampToReal(edm::Timestamp time) { return time.unixTime() + time.microsecondOffset() * 1e-6; }
92  void ClearInEventLoop();
93  void ClearInEndRun();
94  // ----------member data ---------------------------
99  double lastruntime;
101 
102  unsigned int DetectorID;
103  TTree* treeEvent;
104  TTree* treeRun;
105  TTree* treeCluster;
106 
107  int events;
119 
120  std::vector<int> v_ntrk;
121  int ntrk;
123 
138  double run_time;
139  std::vector<double> pt;
140  std::vector<double> charge;
141  std::vector<double> chi2;
142  std::vector<double> chi2_ndof;
143  std::vector<double> eta;
144  std::vector<double> theta;
145  std::vector<double> phi;
146  std::vector<double> p;
147  std::vector<double> d0;
148  std::vector<double> dz;
149  std::vector<double> nvh;
150  std::vector<double> DTtime;
151 };
152 
153 //
154 // constants, enums and typedefs
155 //
156 
157 //
158 // static data member definitions
159 //
160 
161 //
162 // constructors and destructor
163 //
165  : trackTags_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracksInputTag"))),
167  consumes<edmNew::DetSetVector<SiStripCluster> >(iConfig.getParameter<edm::InputTag>("tracksInputTag"))),
168  muonTags_(consumes<reco::MuonCollection>(iConfig.getParameter<edm::InputTag>("muonsInputTag"))) {
169  //now do what ever initialization is needed
170  //
171  usesResource(TFileService::kSharedResource);
172  treeEvent = fs->make<TTree>("Event", "");
173  treeRun = fs->make<TTree>("Run", "");
174  treeCluster = fs->make<TTree>("Cluster", "");
175 }
176 
178  // do anything here that needs to be done at desctruction time
179  // (e.g. close files, deallocate resources etc.)
180 }
181 
182 //
183 // member functions
184 //
186  pt.clear();
187  charge.clear();
188  chi2.clear();
189  chi2_ndof.clear();
190  eta.clear();
191  theta.clear();
192  phi.clear();
193  p.clear();
194  d0.clear();
195  dz.clear();
196  nvh.clear();
197  DTtime.clear();
198 }
199 
200 // ------------ method called for each event ------------
202  using namespace edm;
203 
204  using reco::TrackCollection;
206  iEvent.getByToken(trackTags_, tracks);
207 
209  iSetup.get<IdealMagneticFieldRecord>().get(magfield);
210 
212  iSetup.get<TrackerTopologyRcd>().get(tTopo);
213 
214  edm::Timestamp ts_begin = iEvent.getRun().beginTime();
215  double t_begin = stampToReal(ts_begin);
216  edm::Timestamp ts_end = iEvent.getRun().endTime();
217  double t_end = stampToReal(ts_end);
218 
219  lastruntime = t_end - t_begin;
220  lastrunnum = iEvent.getRun().run();
221 
223  iSetup.get<SiStripLatencyRcd>().get(apvlat);
224 
225  if (!tracks->empty())
226  v_ntrk.push_back(tracks->size());
227 
228  ntrk = 0;
229  for (TrackCollection::const_iterator itTrack1 = tracks->begin(); itTrack1 != tracks->end(); ++itTrack1) {
230  pt.push_back(itTrack1->pt());
231  charge.push_back(itTrack1->charge());
232  chi2.push_back(itTrack1->chi2());
233  chi2_ndof.push_back(itTrack1->normalizedChi2());
234  eta.push_back(itTrack1->eta());
235  theta.push_back(itTrack1->theta());
236  phi.push_back(itTrack1->phi());
237  p.push_back(itTrack1->p());
238  d0.push_back(itTrack1->d0());
239  dz.push_back(itTrack1->dz());
240  nvh.push_back(itTrack1->numberOfValidHits());
241 
242  int nhitinBPIX = 0;
243  int nhitinFPIX = 0;
244  int nhitinPIXEL = 0;
245  int nhitinTEC = 0;
246  int nhitinTOB = 0;
247  int nhitinTIB = 0;
248  int nhitinTID = 0;
249  int nhitinTECminus = 0;
250  int nhitinTECplus = 0;
251  int nhitinTIDminus = 0;
252  int nhitinTIDplus = 0;
253  int countHit = 0;
254 
255  for (auto const& hit1 : itTrack1->recHits()) {
256  const DetId detId1(hit1->geographicalId());
257  const int subdetId1 = detId1.subdetId();
258  if (!hit1->isValid())
259  continue; // only real hits count as in itTrack1->numberOfValidHits()
260 
262  // Hit information in PixelBarrel //
264  if (PixelSubdetector::PixelBarrel == subdetId1) {
265  ++nhitinBPIX;
266  ++nhitinPIXEL;
267  PixelBarrelName pxbId1(detId1);
268  }
270  // Hit information in PixelEndcap //
272  else if (PixelSubdetector::PixelEndcap == subdetId1) {
273  ++nhitinFPIX;
274  ++nhitinPIXEL;
275  }
277  // Hit information in TEC //
279  else if (SiStripDetId::TEC == subdetId1) {
280  ++nhitinTEC;
281 
282  if (tTopo->tecIsZMinusSide(detId1)) {
283  ++nhitinTECminus;
284  } else {
285  ++nhitinTECplus;
286  }
287  }
289  // Hit information in TOB //
291  else if (SiStripDetId::TOB == subdetId1) {
292  ++nhitinTOB;
293  }
295  // Hit information in TIB //
297  else if (SiStripDetId::TIB == subdetId1) {
298  ++nhitinTIB;
299  }
301  // Hit information in TID //
303  else if (SiStripDetId::TID == subdetId1) {
304  ++nhitinTID;
305 
306  if (tTopo->tidIsZMinusSide(detId1)) {
307  ++nhitinTIDminus;
308  } else {
309  ++nhitinTIDplus;
310  }
311  }
312 
313  countHit++;
314  } // for Loop over Hits
315 
316  if (nhitinBPIX > 0) {
317  track_BPIX++;
318  }
319  if (nhitinFPIX > 0) {
320  track_FPIX++;
321  }
322  if (nhitinPIXEL > 0) {
323  track_PIXEL++;
324  }
325  if (nhitinTEC > 0) {
326  track_TEC++;
327  }
328  if (nhitinTECminus > 0) {
329  track_TECM++;
330  }
331  if (nhitinTECplus > 0) {
332  track_TECP++;
333  }
334  if (nhitinTOB > 0) {
335  track_TOB++;
336  }
337  if (nhitinTIB > 0) {
338  track_TIB++;
339  }
340  if (nhitinTID > 0) {
341  track_TID++;
342  }
343  if (nhitinTIDminus > 0) {
344  track_TIDM++;
345  }
346  if (nhitinTIDplus > 0) {
347  track_TIDP++;
348  }
349 
350  ntrk++;
351  ntrk_runnum++;
352  } // for Loop over TrackCollection
353  events++;
354 
356  iEvent.getByToken(clustercollectionToken_, cluster);
357 
358  for (edmNew::DetSetVector<SiStripCluster>::const_iterator det = cluster->begin(); det != cluster->end(); ++det) {
359  DetectorID = (det->detId());
360  treeCluster->Fill();
361  }
362 
364  iEvent.getByToken(muonTags_, muH);
365  const reco::MuonCollection& muonsT0 = *(muH.product());
366  float time = -9999.;
367  for (unsigned int i = 0; i < muonsT0.size(); i++) {
368  //DT time
369  reco::MuonTime mt0 = muonsT0[i].time();
370  time = mt0.timeAtIpInOut;
371  DTtime.push_back(time);
372  }
373  treeEvent->Fill();
375 
376 } //Event Loop
377 
378 // ------------ method called once each job just before starting event loop ------------
380  treeEvent->Branch("pt", &pt);
381  treeEvent->Branch("charge", &charge);
382  treeEvent->Branch("chi2", &chi2);
383  treeEvent->Branch("chi2_ndof", &chi2_ndof);
384  treeEvent->Branch("eta", &eta);
385  treeEvent->Branch("theta", &theta);
386  treeEvent->Branch("phi", &phi);
387  treeEvent->Branch("p", &p);
388  treeEvent->Branch("d0", &d0);
389  treeEvent->Branch("dz", &dz);
390  treeEvent->Branch("nvh", &nvh);
391  treeEvent->Branch("ntrk", &ntrk);
392  treeEvent->Branch("DTtime", &DTtime);
393  treeRun->Branch("run_time", &run_time);
394  treeRun->Branch("runnum", &runnum);
395  treeRun->Branch("number_of_events", &number_of_events);
396  treeRun->Branch("number_of_tracks", &number_of_tracks);
397  treeRun->Branch("number_of_tracks_PIX", &number_of_tracks_PIX);
398  treeRun->Branch("number_of_tracks_FPIX", &number_of_tracks_FPIX);
399  treeRun->Branch("number_of_tracks_BPIX", &number_of_tracks_BPIX);
400  treeRun->Branch("number_of_tracks_TID", &number_of_tracks_TID);
401  treeRun->Branch("number_of_tracks_TIDM", &number_of_tracks_TIDM);
402  treeRun->Branch("number_of_tracks_TIDP", &number_of_tracks_TIDP);
403  treeRun->Branch("number_of_tracks_TIB", &number_of_tracks_TIB);
404  treeRun->Branch("number_of_tracks_TEC", &number_of_tracks_TEC);
405  treeRun->Branch("number_of_tracks_TECP", &number_of_tracks_TECP);
406  treeRun->Branch("number_of_tracks_TECM", &number_of_tracks_TECM);
407  treeRun->Branch("number_of_tracks_TOB", &number_of_tracks_TOB);
408  treeCluster->Branch("DetID", &DetectorID);
409 }
410 
411 // ------------ method called once each job just after ending the event loop ------------
413 
414 // ------------ method called when starting to processes a run ------------
416  lastruntime = 0.0;
417  lastrunnum = 0.0;
418  ntrk_runnum = 0.0;
419  events = 0.0;
420  track_BPIX = 0.0;
421  track_FPIX = 0.0;
422  track_PIXEL = 0.0;
423  track_TEC = 0.0;
424  track_TECM = 0.0;
425  track_TECP = 0.0;
426  track_TOB = 0.0;
427  track_TIB = 0.0;
428  track_TID = 0.0;
429  track_TIDM = 0.0;
430  track_TIDP = 0.0;
431 }
432 
433 // ------------ method called when ending the processing of a run ------------
434 
438  runnum = lastrunnum;
451  treeRun->Fill();
452 }
453 
454 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
457  desc.setComment("Create tuple with all variables required to calculate cosmic event and track rates.");
458  desc.add<edm::InputTag>("tracksInputTag", edm::InputTag("ALCARECOTkAlCosmicsCTF0T"));
459  desc.add<edm::InputTag>("muonsInputTag", edm::InputTag("muons1Leg"));
460  descriptions.add("cosmicRateAnalyzer", desc);
461 }
462 
463 //define this as a plug-in
static const std::string kSharedResource
Definition: TFileService.h:76
std::vector< double > theta
std::vector< double > dz
Timestamp const & endTime() const
Definition: RunBase.h:42
RunNumber_t run() const
Definition: RunBase.h:40
edm::EDGetTokenT< reco::TrackCollection > trackTags_
std::vector< double > pt
std::vector< double > d0
std::vector< double > eta
void endJob() override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
static constexpr auto TID
Definition: SiStripDetId.h:38
std::vector< double > charge
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
edm::RunNumber_t lastrunnum
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
Run const & getRun() const
Definition: Event.cc:108
unsigned int microsecondOffset() const
Microseconds offset within second.
Definition: Timestamp.h:43
void analyze(const edm::Event &, const edm::EventSetup &) override
CosmicRateAnalyzer(const edm::ParameterSet &)
bool tecIsZMinusSide(const DetId &id) const
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
void beginRun(edm::Run const &, edm::EventSetup const &) override
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
bool tidIsZMinusSide(const DetId &id) const
void setComment(std::string const &value)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< double > DTtime
std::vector< double > nvh
edm::EDGetTokenT< reco::MuonCollection > muonTags_
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
unsigned int unixTime() const
Time in seconds since January 1, 1970.
Definition: Timestamp.h:40
std::vector< double > phi
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
static constexpr auto TOB
Definition: SiStripDetId.h:39
std::vector< double > chi2
std::vector< int > v_ntrk
std::vector< double > p
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
Definition: DetId.h:17
edm::RunNumber_t runnum
T const * product() const
Definition: Handle.h:69
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static constexpr auto TIB
Definition: SiStripDetId.h:37
Timestamp const & beginTime() const
Definition: RunBase.h:41
edm::Service< TFileService > fs
void beginJob() override
fixed size matrix
HLT enums.
void endRun(edm::Run const &, edm::EventSetup const &) override
std::vector< double > chi2_ndof
T get() const
Definition: EventSetup.h:73
unsigned int RunNumber_t
static double stampToReal(edm::Timestamp time)
float timeAtIpInOut
Definition: MuonTime.h:13
edm::EDGetTokenT< edmNew::DetSetVector< SiStripCluster > > clustercollectionToken_
static constexpr auto TEC
Definition: SiStripDetId.h:40
Definition: Run.h:45