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 
25 
26 // system include files
27 #include <memory>
28 
29 // user include files
40 
44 
48 
53 
55 
59 
67 
68 #include <vector>
69 #include <string>
70 #include <iostream>
71 #include <fstream>
72 #include <iomanip>
73 #include <TTree.h>
74 
75 
76 //
77 // class declaration
78 //
79 class CosmicRateAnalyzer : public edm::one::EDAnalyzer<edm::one::WatchRuns,edm::one::SharedResources> {
80  public:
81  explicit CosmicRateAnalyzer(const edm::ParameterSet&);
82  ~CosmicRateAnalyzer() override;
83 
84  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
85 
86 
87  private:
88  void beginJob() override;
89  void analyze(const edm::Event&, const edm::EventSetup&) override;
90  void endJob() override;
91 
92  void beginRun(edm::Run const&, edm::EventSetup const&) override;
93  void endRun(edm::Run const&, edm::EventSetup const&) override;
94 
95  static double stampToReal(edm::Timestamp time) {
96  return time.unixTime() + time.microsecondOffset()*1e-6;
97  }
98  void ClearInEventLoop();
99  void ClearInEndRun();
100  // ----------member data ---------------------------
105  double lastruntime;
107 
108  unsigned int DetectorID;
109  TTree* treeEvent;
110  TTree* treeRun;
111  TTree* treeCluster;
112 
113  int events;
114  int track_BPIX ;
115  int track_FPIX ;
116  int track_PIXEL ;
117  int track_TEC ;
118  int track_TECM ;
119  int track_TECP ;
120  int track_TOB ;
121  int track_TIB ;
122  int track_TID ;
123  int track_TIDM ;
124  int track_TIDP ;
125 
126  std::vector<int> v_ntrk;
127  int ntrk;
129 
144  double run_time ;
145  std::vector<double> pt;
146  std::vector<double> charge;
147  std::vector<double> chi2;
148  std::vector<double> chi2_ndof;
149  std::vector<double> eta;
150  std::vector<double> theta;
151  std::vector<double> phi;
152  std::vector<double> p;
153  std::vector<double> d0;
154  std::vector<double> dz;
155  std::vector<double> nvh;
156  std::vector<double> DTtime;
157 
158 };
159 
160 //
161 // constants, enums and typedefs
162 //
163 
164 //
165 // static data member definitions
166 //
167 
168 //
169 // constructors and destructor
170 //
172  trackTags_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracksInputTag"))),
173  clustercollectionToken_(consumes<edmNew::DetSetVector<SiStripCluster> >(iConfig.getParameter<edm::InputTag>("tracksInputTag"))),
174  muonTags_(consumes<reco::MuonCollection>(iConfig.getParameter<edm::InputTag>("muonsInputTag")))
175 {
176  //now do what ever initialization is needed
177  //
178  usesResource(TFileService::kSharedResource);
179  treeEvent = fs->make<TTree>("Event","");
180  treeRun = fs->make<TTree>("Run","");
181  treeCluster = fs->make<TTree>("Cluster","");
182 
183 }
184 
185 
187 {
188 
189  // do anything here that needs to be done at desctruction time
190  // (e.g. close files, deallocate resources etc.)
191 
192 }
193 
194 
195 //
196 // member functions
197 //
199 
200  pt .clear();
201  charge .clear();
202  chi2 .clear();
203  chi2_ndof .clear();
204  eta .clear();
205  theta .clear();
206  phi .clear();
207  p .clear();
208  d0 .clear();
209  dz .clear();
210  nvh .clear();
211  DTtime .clear();
212 
213 }
214 
215 // ------------ method called for each event ------------
216 void
218 {
219  using namespace edm;
220 
221  using reco::TrackCollection;
223  iEvent.getByToken(trackTags_, tracks);
224 
226  iSetup.get<IdealMagneticFieldRecord>().get(magfield);
227 
229  iSetup.get<TrackerTopologyRcd>().get(tTopo);
230 
231  edm::Timestamp ts_begin = iEvent.getRun().beginTime();
232  double t_begin = stampToReal(ts_begin);
233  edm::Timestamp ts_end = iEvent.getRun().endTime();
234  double t_end = stampToReal(ts_end);
235 
236  lastruntime = t_end - t_begin;
237  lastrunnum = iEvent.getRun().run();
238 
240  iSetup.get<SiStripLatencyRcd>().get(apvlat);
241 
242  if (!tracks->empty()) v_ntrk.push_back(tracks->size());
243 
244  ntrk = 0;
245  for(TrackCollection::const_iterator itTrack1 = tracks->begin(); itTrack1 != tracks->end(); ++itTrack1)
246  {
247  pt .push_back(itTrack1->pt());
248  charge .push_back(itTrack1->charge());
249  chi2 .push_back(itTrack1->chi2());
250  chi2_ndof .push_back(itTrack1->normalizedChi2());
251  eta .push_back(itTrack1->eta());
252  theta .push_back(itTrack1->theta());
253  phi .push_back(itTrack1->phi());
254  p .push_back(itTrack1->p());
255  d0 .push_back(itTrack1->d0());
256  dz .push_back(itTrack1->dz());
257  nvh .push_back(itTrack1->numberOfValidHits());
258 
259  int nhitinBPIX = 0;
260  int nhitinFPIX = 0;
261  int nhitinPIXEL = 0;
262  int nhitinTEC = 0;
263  int nhitinTOB = 0;
264  int nhitinTIB = 0;
265  int nhitinTID = 0;
266  int nhitinTECminus= 0;
267  int nhitinTECplus = 0;
268  int nhitinTIDminus= 0;
269  int nhitinTIDplus = 0;
270  int countHit = 0;
271 
272  for(trackingRecHit_iterator iHit1 = itTrack1->recHitsBegin(); iHit1 != itTrack1->recHitsEnd(); ++iHit1)
273  {
274 
275  const DetId detId1((*iHit1)->geographicalId());
276  const int subdetId1 = detId1.subdetId();
277  if (!(*iHit1)->isValid()) continue; // only real hits count as in itTrack1->numberOfValidHits()
278 
280 // Hit information in PixelBarrel //
282  if (PixelSubdetector::PixelBarrel == subdetId1)
283  {
284  ++nhitinBPIX; ++nhitinPIXEL;
285  PixelBarrelName pxbId1(detId1);
286  }
288 // Hit information in PixelEndcap //
290  else if (PixelSubdetector::PixelEndcap == subdetId1)
291  {
292  ++nhitinFPIX; ++nhitinPIXEL;
293  }
295 // Hit information in TEC //
297  else if (SiStripDetId::TEC == subdetId1)
298  {
299  ++nhitinTEC;
300 
301  if (tTopo->tecIsZMinusSide(detId1))
302  {
303  ++nhitinTECminus;
304  }
305  else
306  {
307  ++nhitinTECplus;
308  }
309  }
311 // Hit information in TOB //
313  else if (SiStripDetId::TOB == subdetId1)
314  {
315  ++nhitinTOB;
316  }
318 // Hit information in TIB //
320  else if (SiStripDetId::TIB == subdetId1)
321  {
322  ++nhitinTIB;
323  }
325 // Hit information in TID //
327  else if (SiStripDetId::TID == subdetId1)
328  {
329  ++nhitinTID;
330 
331  if (tTopo->tidIsZMinusSide(detId1))
332  {
333  ++nhitinTIDminus;
334  }
335  else
336  {
337  ++nhitinTIDplus;
338  }
339  }
340 
341  countHit++;
342  } // for Loop over Hits
343 
344  if (nhitinBPIX > 0 ) {track_BPIX++ ;}
345  if (nhitinFPIX > 0 ) {track_FPIX++ ;}
346  if (nhitinPIXEL > 0 ) {track_PIXEL++ ;}
347  if (nhitinTEC > 0 ) {track_TEC++ ;}
348  if (nhitinTECminus > 0 ) {track_TECM++ ;}
349  if (nhitinTECplus > 0 ) {track_TECP++ ;}
350  if (nhitinTOB > 0 ) {track_TOB++ ;}
351  if (nhitinTIB > 0 ) {track_TIB++ ;}
352  if (nhitinTID > 0 ) {track_TID++ ;}
353  if (nhitinTIDminus > 0 ) {track_TIDM++ ;}
354  if (nhitinTIDplus > 0 ) {track_TIDP++ ;}
355 
356  ntrk++;
357  ntrk_runnum++;
358  } // for Loop over TrackCollection
359  events++;
360 
362  iEvent.getByToken(clustercollectionToken_,cluster);
363 
364  for(edmNew::DetSetVector<SiStripCluster>::const_iterator det = cluster->begin();det!=cluster->end();++det)
365  {
366  DetectorID = (det->detId());
367  treeCluster->Fill();
368  }
369 
371  iEvent.getByToken(muonTags_, muH);
372  const reco::MuonCollection& muonsT0 = *(muH.product());
373  float time = -9999.;
374  for (unsigned int i=0; i<muonsT0.size(); i++)
375  {
376  //DT time
377  reco::MuonTime mt0 = muonsT0[i].time();
378  time = mt0.timeAtIpInOut;
379  DTtime.push_back(time);
380  }
381  treeEvent ->Fill();
383 
384 } //Event Loop
385 
386 // ------------ method called once each job just before starting event loop ------------
387 void
389 {
390  treeEvent ->Branch("pt",&pt);
391  treeEvent ->Branch("charge",&charge);
392  treeEvent ->Branch("chi2",&chi2);
393  treeEvent ->Branch("chi2_ndof",&chi2_ndof);
394  treeEvent ->Branch("eta",&eta);
395  treeEvent ->Branch("theta",&theta);
396  treeEvent ->Branch("phi",&phi);
397  treeEvent ->Branch("p",&p);
398  treeEvent ->Branch("d0",&d0);
399  treeEvent ->Branch("dz",&dz);
400  treeEvent ->Branch("nvh",&nvh);
401  treeEvent ->Branch("ntrk",&ntrk);
402  treeEvent ->Branch("DTtime",&DTtime);
403  treeRun ->Branch("run_time",&run_time);
404  treeRun ->Branch("runnum",&runnum);
405  treeRun ->Branch("number_of_events",&number_of_events);
406  treeRun ->Branch("number_of_tracks",&number_of_tracks);
407  treeRun ->Branch("number_of_tracks_PIX",&number_of_tracks_PIX);
408  treeRun ->Branch("number_of_tracks_FPIX",&number_of_tracks_FPIX);
409  treeRun ->Branch("number_of_tracks_BPIX",&number_of_tracks_BPIX);
410  treeRun ->Branch("number_of_tracks_TID",&number_of_tracks_TID);
411  treeRun ->Branch("number_of_tracks_TIDM",&number_of_tracks_TIDM);
412  treeRun ->Branch("number_of_tracks_TIDP",&number_of_tracks_TIDP);
413  treeRun ->Branch("number_of_tracks_TIB",&number_of_tracks_TIB);
414  treeRun ->Branch("number_of_tracks_TEC",&number_of_tracks_TEC);
415  treeRun ->Branch("number_of_tracks_TECP",&number_of_tracks_TECP);
416  treeRun ->Branch("number_of_tracks_TECM",&number_of_tracks_TECM);
417  treeRun ->Branch("number_of_tracks_TOB",&number_of_tracks_TOB);
418  treeCluster ->Branch("DetID",&DetectorID);
419 
420 }
421 
422 // ------------ method called once each job just after ending the event loop ------------
423 void
425 {
426 }
427 
428 // ------------ method called when starting to processes a run ------------
429 void
431 {
432  lastruntime = 0.0;
433  lastrunnum = 0.0;
434  ntrk_runnum = 0.0;
435  events = 0.0;
436  track_BPIX = 0.0;
437  track_FPIX = 0.0;
438  track_PIXEL = 0.0;
439  track_TEC = 0.0;
440  track_TECM = 0.0;
441  track_TECP = 0.0;
442  track_TOB = 0.0;
443  track_TIB = 0.0;
444  track_TID = 0.0;
445  track_TIDM = 0.0;
446  track_TIDP = 0.0;
447 }
448 
449 
450 // ------------ method called when ending the processing of a run ------------
451 
452 void
454 {
457  runnum = lastrunnum;
470  treeRun ->Fill();
471 }
472 
473 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
474 void
477  desc.setComment("Create tuple with all variables required to calculate cosmic event and track rates.");
478  desc.add<edm::InputTag> ("tracksInputTag",edm::InputTag("ALCARECOTkAlCosmicsCTF0T"));
479  desc.add<edm::InputTag> ("muonsInputTag",edm::InputTag("muons1Leg"));
480  descriptions.add("cosmicRateAnalyzer", desc);
481 }
482 
483 //define this as a plug-in
485 
static const std::string kSharedResource
Definition: TFileService.h:76
std::vector< double > theta
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
void endJob() override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
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:114
unsigned int microsecondOffset() const
Microseconds offset within second.
Definition: Timestamp.h:52
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:230
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:41
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)
std::vector< double > chi2
std::vector< int > v_ntrk
std::vector< double > p
Definition: DetId.h:18
edm::RunNumber_t runnum
T const * product() const
Definition: Handle.h:81
void add(std::string const &label, ParameterSetDescription const &psetDescription)
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:68
unsigned int RunNumber_t
static double stampToReal(edm::Timestamp time)
float timeAtIpInOut
Definition: MuonTime.h:14
edm::EDGetTokenT< edmNew::DetSetVector< SiStripCluster > > clustercollectionToken_
Definition: Run.h:44