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