CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
25 
26 // system include files
27 #include <memory>
28 
29 // user include files
39 
43 
47 
51 
53 
57 
71 
72 #include <vector>
73 #include <string>
74 #include <iostream>
75 #include <fstream>
76 #include <iomanip>
77 #include <TTree.h>
78 
79 
80 //
81 // class declaration
82 //
83 class CosmicRateAnalyzer : public edm::one::EDAnalyzer<edm::one::WatchRuns,edm::one::SharedResources> {
84  public:
85  explicit CosmicRateAnalyzer(const edm::ParameterSet&);
87 
88  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
89 
90 
91  private:
92  virtual void beginJob() override;
93  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
94  virtual void endJob() override;
95 
96  virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
97  virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
98 
99  static double stampToReal(edm::Timestamp time) {
100  return time.unixTime() + time.microsecondOffset()*1e-6;
101  }
102  void ClearInEventLoop();
103  void ClearInEndRun();
104  // ----------member data ---------------------------
109  double lastruntime;
111 
112  unsigned int DetectorID;
113  TTree* treeEvent;
114  TTree* treeRun;
115  TTree* treeCluster;
116 
117  int events;
118  int track_BPIX ;
119  int track_FPIX ;
120  int track_PIXEL ;
121  int track_TEC ;
122  int track_TECM ;
123  int track_TECP ;
124  int track_TOB ;
125  int track_TIB ;
126  int track_TID ;
127  int track_TIDM ;
128  int track_TIDP ;
129 
130  std::vector<int> v_ntrk;
131  int ntrk;
133 
148  double run_time ;
149  std::vector<double> pt;
150  std::vector<double> charge;
151  std::vector<double> chi2;
152  std::vector<double> chi2_ndof;
153  std::vector<double> eta;
154  std::vector<double> theta;
155  std::vector<double> phi;
156  std::vector<double> p;
157  std::vector<double> d0;
158  std::vector<double> dz;
159  std::vector<double> nvh;
160  std::vector<double> DTtime;
161 
162 };
163 
164 //
165 // constants, enums and typedefs
166 //
167 
168 //
169 // static data member definitions
170 //
171 
172 //
173 // constructors and destructor
174 //
176  trackTags_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracksInputTag"))),
177  clustercollectionToken_(consumes<edmNew::DetSetVector<SiStripCluster> >(iConfig.getParameter<edm::InputTag>("tracksInputTag"))),
178  muonTags_(consumes<reco::MuonCollection>(iConfig.getParameter<edm::InputTag>("muonsInputTag")))
179 {
180  //now do what ever initialization is needed
181  //
183  treeEvent = fs->make<TTree>("Event","");
184  treeRun = fs->make<TTree>("Run","");
185  treeCluster = fs->make<TTree>("Cluster","");
186 
187 }
188 
189 
191 {
192 
193  // do anything here that needs to be done at desctruction time
194  // (e.g. close files, deallocate resources etc.)
195 
196 }
197 
198 
199 //
200 // member functions
201 //
203 
204  pt .clear();
205  charge .clear();
206  chi2 .clear();
207  chi2_ndof .clear();
208  eta .clear();
209  theta .clear();
210  phi .clear();
211  p .clear();
212  d0 .clear();
213  dz .clear();
214  nvh .clear();
215  DTtime .clear();
216 
217 }
218 
219 // ------------ method called for each event ------------
220 void
222 {
223  using namespace edm;
224 
225  using reco::TrackCollection;
227  iEvent.getByToken(trackTags_, tracks);
228 
230  iSetup.get<IdealMagneticFieldRecord>().get(magfield);
231 
232  edm::Timestamp ts_begin = iEvent.getRun().beginTime();
233  double t_begin = stampToReal(ts_begin);
234  edm::Timestamp ts_end = iEvent.getRun().endTime();
235  double t_end = stampToReal(ts_end);
236 
237  lastruntime = t_end - t_begin;
238  lastrunnum = iEvent.getRun().run();
239 
241  iSetup.get<SiStripLatencyRcd>().get(apvlat);
242 
243  if (tracks->size()>0) v_ntrk.push_back(tracks->size());
244 
245  ntrk = 0;
246  for(TrackCollection::const_iterator itTrack1 = tracks->begin(); itTrack1 != tracks->end(); ++itTrack1)
247  {
248  pt .push_back(itTrack1->pt());
249  charge .push_back(itTrack1->charge());
250  chi2 .push_back(itTrack1->chi2());
251  chi2_ndof .push_back(itTrack1->normalizedChi2());
252  eta .push_back(itTrack1->eta());
253  theta .push_back(itTrack1->theta());
254  phi .push_back(itTrack1->phi());
255  p .push_back(itTrack1->p());
256  d0 .push_back(itTrack1->d0());
257  dz .push_back(itTrack1->dz());
258  nvh .push_back(itTrack1->numberOfValidHits());
259 
260  int nhitinBPIX = 0;
261  int nhitinFPIX = 0;
262  int nhitinPIXEL = 0;
263  int nhitinTEC = 0;
264  int nhitinTOB = 0;
265  int nhitinTIB = 0;
266  int nhitinTID = 0;
267  int nhitinTECminus= 0;
268  int nhitinTECplus = 0;
269  int nhitinTIDminus= 0;
270  int nhitinTIDplus = 0;
271  int countHit = 0;
272 
273  for(trackingRecHit_iterator iHit1 = itTrack1->recHitsBegin(); iHit1 != itTrack1->recHitsEnd(); ++iHit1)
274  {
275 
276  const DetId detId1((*iHit1)->geographicalId());
277  const int subdetId1 = detId1.subdetId();
278  if (!(*iHit1)->isValid()) continue; // only real hits count as in itTrack1->numberOfValidHits()
279 
281 // Hit information in PixelBarrel //
283  if (PixelSubdetector::PixelBarrel == subdetId1)
284  {
285  ++nhitinBPIX; ++nhitinPIXEL;
286  PixelBarrelName pxbId1(detId1);
287  }
289 // Hit information in PixelEndcap //
291  else if (PixelSubdetector::PixelEndcap == subdetId1)
292  {
293  ++nhitinFPIX; ++nhitinPIXEL;
294  }
296 // Hit information in TEC //
298  else if (SiStripDetId::TEC == subdetId1)
299  {
300  ++nhitinTEC;
301  TECDetId tecId1(detId1);
302  if (tecId1.isZMinusSide())
303  {
304  ++nhitinTECminus;
305  }
306  else
307  {
308  ++nhitinTECplus;
309  }
310  }
312 // Hit information in TOB //
314  else if (SiStripDetId::TOB == subdetId1)
315  {
316  ++nhitinTOB;
317  TOBDetId tobId1(detId1);
318  }
320 // Hit information in TIB //
322  else if (SiStripDetId::TIB == subdetId1)
323  {
324  ++nhitinTIB;
325  TIBDetId tibId1(detId1);
326  }
328 // Hit information in TID //
330  else if (SiStripDetId::TID == subdetId1)
331  {
332  ++nhitinTID;
333  TIDDetId tidId1(detId1);
334  if (tidId1.isZMinusSide())
335  {
336  ++nhitinTIDminus;
337  }
338  else
339  {
340  ++nhitinTIDplus;
341  }
342  }
343 
344  countHit++;
345  } // for Loop over Hits
346 
347  if (nhitinBPIX > 0 ) {track_BPIX++ ;}
348  if (nhitinFPIX > 0 ) {track_FPIX++ ;}
349  if (nhitinPIXEL > 0 ) {track_PIXEL++ ;}
350  if (nhitinTEC > 0 ) {track_TEC++ ;}
351  if (nhitinTECminus > 0 ) {track_TECM++ ;}
352  if (nhitinTECplus > 0 ) {track_TECP++ ;}
353  if (nhitinTOB > 0 ) {track_TOB++ ;}
354  if (nhitinTIB > 0 ) {track_TIB++ ;}
355  if (nhitinTID > 0 ) {track_TID++ ;}
356  if (nhitinTIDminus > 0 ) {track_TIDM++ ;}
357  if (nhitinTIDplus > 0 ) {track_TIDP++ ;}
358 
359  ntrk++;
360  ntrk_runnum++;
361  } // for Loop over TrackCollection
362  events++;
363 
365  iEvent.getByToken(clustercollectionToken_,cluster);
366 
367  for(edmNew::DetSetVector<SiStripCluster>::const_iterator det = cluster->begin();det!=cluster->end();++det)
368  {
369  DetectorID = (det->detId());
370  treeCluster->Fill();
371  }
372 
374  iEvent.getByToken(muonTags_, muH);
375  const reco::MuonCollection& muonsT0 = *(muH.product());
376  float time = -9999.;
377  for (unsigned int i=0; i<muonsT0.size(); i++)
378  {
379  //DT time
380  reco::MuonTime mt0 = muonsT0[i].time();
381  time = mt0.timeAtIpInOut;
382  DTtime.push_back(time);
383  }
384  treeEvent ->Fill();
386 
387 } //Event Loop
388 
389 // ------------ method called once each job just before starting event loop ------------
390 void
392 {
393  treeEvent ->Branch("pt",&pt);
394  treeEvent ->Branch("charge",&charge);
395  treeEvent ->Branch("chi2",&chi2);
396  treeEvent ->Branch("chi2_ndof",&chi2_ndof);
397  treeEvent ->Branch("eta",&eta);
398  treeEvent ->Branch("theta",&theta);
399  treeEvent ->Branch("phi",&phi);
400  treeEvent ->Branch("p",&p);
401  treeEvent ->Branch("d0",&d0);
402  treeEvent ->Branch("dz",&dz);
403  treeEvent ->Branch("nvh",&nvh);
404  treeEvent ->Branch("ntrk",&ntrk);
405  treeEvent ->Branch("DTtime",&DTtime);
406  treeRun ->Branch("run_time",&run_time);
407  treeRun ->Branch("runnum",&runnum);
408  treeRun ->Branch("number_of_events",&number_of_events);
409  treeRun ->Branch("number_of_tracks",&number_of_tracks);
410  treeRun ->Branch("number_of_tracks_PIX",&number_of_tracks_PIX);
411  treeRun ->Branch("number_of_tracks_FPIX",&number_of_tracks_FPIX);
412  treeRun ->Branch("number_of_tracks_BPIX",&number_of_tracks_BPIX);
413  treeRun ->Branch("number_of_tracks_TID",&number_of_tracks_TID);
414  treeRun ->Branch("number_of_tracks_TIDM",&number_of_tracks_TIDM);
415  treeRun ->Branch("number_of_tracks_TIDP",&number_of_tracks_TIDP);
416  treeRun ->Branch("number_of_tracks_TIB",&number_of_tracks_TIB);
417  treeRun ->Branch("number_of_tracks_TEC",&number_of_tracks_TEC);
418  treeRun ->Branch("number_of_tracks_TECP",&number_of_tracks_TECP);
419  treeRun ->Branch("number_of_tracks_TECM",&number_of_tracks_TECM);
420  treeRun ->Branch("number_of_tracks_TOB",&number_of_tracks_TOB);
421  treeCluster ->Branch("DetID",&DetectorID);
422 
423 }
424 
425 // ------------ method called once each job just after ending the event loop ------------
426 void
428 {
429 }
430 
431 // ------------ method called when starting to processes a run ------------
432 void
434 {
435  lastruntime = 0.0;
436  lastrunnum = 0.0;
437  ntrk_runnum = 0.0;
438  events = 0.0;
439  track_BPIX = 0.0;
440  track_FPIX = 0.0;
441  track_PIXEL = 0.0;
442  track_TEC = 0.0;
443  track_TECM = 0.0;
444  track_TECP = 0.0;
445  track_TOB = 0.0;
446  track_TIB = 0.0;
447  track_TID = 0.0;
448  track_TIDM = 0.0;
449  track_TIDP = 0.0;
450 }
451 
452 
453 // ------------ method called when ending the processing of a run ------------
454 
455 void
457 {
460  runnum = lastrunnum;
473  treeRun ->Fill();
474 }
475 
476 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
477 void
480  desc.setComment("Create tuple with all variables required to calculate cosmic event and track rates.");
481  desc.add<edm::InputTag> ("tracksInputTag",edm::InputTag("ALCARECOTkAlCosmicsCTF0T"));
482  desc.add<edm::InputTag> ("muonsInputTag",edm::InputTag("muons1Leg"));
483  descriptions.add("cosmicRateAnalyzer", desc);
484 }
485 
486 //define this as a plug-in
488 
static const std::string kSharedResource
Definition: TFileService.h:76
std::vector< double > theta
int i
Definition: DBlmapReader.cc:9
std::vector< double > dz
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
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
virtual void endJob() override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< double > charge
tuple magfield
Definition: HLT_ES_cff.py:2311
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:66
unsigned int microsecondOffset() const
Microseconds offset within second.
Definition: Timestamp.h:52
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
CosmicRateAnalyzer(const edm::ParameterSet &)
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
virtual void beginRun(edm::Run const &, edm::EventSetup const &) override
void setComment(std::string const &value)
bool isZMinusSide() const
Definition: TECDetId.h:86
int iEvent
Definition: GenABIO.cc:230
std::vector< double > DTtime
usesResource(TFileService::kSharedResource)
std::vector< double > nvh
edm::EDGetTokenT< reco::MuonCollection > muonTags_
unsigned int unixTime() const
Time in seconds since January 1, 1970.
Definition: Timestamp.h:46
std::vector< double > phi
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
std::vector< double > chi2
std::vector< int > v_ntrk
std::vector< double > p
Definition: DetId.h:18
bool isZMinusSide() const
Definition: TIDDetId.h:81
edm::RunNumber_t runnum
tuple tracks
Definition: testEve_cfg.py:39
const T & get() const
Definition: EventSetup.h:56
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Timestamp const & beginTime() const
Definition: RunBase.h:41
edm::Service< TFileService > fs
virtual void beginJob() override
virtual void endRun(edm::Run const &, edm::EventSetup const &) override
std::vector< double > chi2_ndof
unsigned int RunNumber_t
static double stampToReal(edm::Timestamp time)
float timeAtIpInOut
Definition: MuonTime.h:14
edm::EDGetTokenT< edmNew::DetSetVector< SiStripCluster > > clustercollectionToken_
TrackingRecHitCollection::base::const_iterator trackingRecHit_iterator
iterator over a vector of reference to TrackingRecHit in the same collection
Definition: Run.h:42