CMS 3D CMS Logo

TestMixedSource.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: TestMixedSource
4 // Class: TestMixedSource
5 //
13 //
14 // Original Author: Emilia Lubenova Becheva
15 // Created: Wed May 20 16:46:58 CEST 2009
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 
23 // user include files
26 
29 
31 
35 
40 
41 #include "TH1I.h"
42 #include "TFile.h"
43 
44 #include "TestMixedSource.h"
45 
46 #include <iostream>
47 #include <fstream>
48 
49 
50 //
51 // constructors and destructor
52 //
53 namespace edm
54 {
56 : fileName_(iConfig.getParameter<std::string>("fileName")), minbunch_(iConfig.getParameter<int>("minBunch")),maxbunch_(iConfig.getParameter<int>("maxBunch"))
57 {
58 
59  histTrack_bunchSignal_ = new TH1I("histoTrackSignal","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
60  histTrack_bunchPileups_ = new TH1I("histoTrackPileups","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
61 
62  // Vertex
63  histVertex_bunch_ = new TH1I("histoVertex","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
64 
65  // PCaloHit
66  histPCaloHit_bunch_ = new TH1I("histoPCaloHit","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
67 
68  // PSimHit
69  histPSimHit_bunchSignal_TrackerHitsTECHighTof_ = new TH1I("histoPSimHitTrackerHitsTECHighTofSignal","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
70  histPSimHit_bunchPileups_TrackerHitsTECHighTof_ = new TH1I("histoPSimHitTrackerHitsTECHighTofPileups","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
71  histPSimHit_bunchSignal_MuonCSCHits_ = new TH1I("histoPSimHitMuonCSCHitsSignal","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
72  histPSimHit_bunchPileups_MuonCSCHits_ = new TH1I("histoPSimHitMuonCSCHitsPileups","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
73 
74  int bsp = 25;//bunchspace
75  tofhist_ = new TH1I ("TrackerHit_Tof_bcr","TrackerHit_ToF",100,float(bsp*minbunch_),float(bsp*maxbunch_)+50.);
76  tofhist_sig_ = new TH1I("SignalTrackerHit_Tof_bcr","TrackerHit_ToF",100,float(bsp*minbunch_),float(bsp*maxbunch_)+50.);
77 
78 
79  // HepMCProduct
80  histHepMCProduct_bunch_ = new TH1I("histoHepMCProduct","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
81 
82  // Tokens
83 
84  edm::InputTag tag = edm::InputTag("mix","g4SimHits");
85 
86  SimTrackToken_ = consumes<CrossingFrame<SimTrack>>(tag);
87  SimVertexToken_ = consumes<CrossingFrame<SimVertex>>(tag);
88 
89  tag = edm::InputTag("mix","g4SimHitsTrackerHitsTECHighTof");
90  TrackerToken0_ = consumes<CrossingFrame<PSimHit>>(tag);
91 
92  tag = edm::InputTag("mix","g4SimHitsEcalHitsEB");
93  CaloToken1_ = consumes<CrossingFrame<PCaloHit>>(tag);
94 
95  tag = edm::InputTag("mix","g4SimHitsMuonCSCHits");
96  MuonToken_ = consumes<CrossingFrame<PSimHit>>(tag);
97 
98  tag = edm::InputTag("mix","generatorSmeared");
99  HepMCToken_ = consumes<CrossingFrame<edm::HepMCProduct>>(tag);
100 
101 
102 }
103 
104 
106 { std::cout << " Destructor TestMixed" << std::endl;
107 
108 }
109 
110 
111 //
112 // member functions
113 //
114 
115 // ------------ method called to for each event ------------
116 void
118 {
119  // test SimTracks
120  //----------------------
122  bool gotTracks = iEvent.getByToken(SimTrackToken_,cf_simtrack);
123  if (!gotTracks) outputFile<<" Could not read SimTracks!!!!"
124  << " Please, check if the object SimTracks has been declared in the"
125  << " MixingModule configuration file."<<std::endl;
126 
127  if (gotTracks) {
128  outputFile<<"\n=================== Starting SimTrack access ==================="<<std::endl;
129 
130  std::unique_ptr<MixCollection<SimTrack> > col1(new MixCollection<SimTrack>(cf_simtrack.product()));
132  int count1=0;
133  std::cout <<" \nWe got "<<col1->sizeSignal()<<" signal tracks and "<<col1->sizePileup()<<" pileup tracks, total: "<<col1->size()<<std::endl;
134  for (cfi1=col1->begin(); cfi1!=col1->end();cfi1++) {
135  //std::cout << " BUNCH cfi1.bunch() = " << cfi1.bunch() << std::endl;
136  if (cfi1.getTrigger()==0){
137  histTrack_bunchPileups_->Fill(cfi1.bunch());
138  }
139 
140  if (cfi1.getTrigger()==1){
141  histTrack_bunchSignal_->Fill(cfi1.bunch());
142  }
143 
144 
145  int a = count1%4;
146  if (a==3){
147  outputFile<<" SimTrack "<<count1<<" has genpart index "<<cfi1->genpartIndex()<<" vertex Index "<<cfi1->vertIndex() <<" bunchcr "<<cfi1.bunch()<<" trigger "<<cfi1.getTrigger()<<", from EncodedEventId: "<<cfi1->eventId().bunchCrossing() <<" "<<cfi1->eventId().event() <<std::endl;
148  }
149  count1++;
150  }
151  }
152 
153 
154  // test SimVertices
155  //---------------------
157  bool gotSimVertex = iEvent.getByToken(SimVertexToken_,cf_simvtx);
158  if (!gotSimVertex) outputFile<<" Could not read Simvertices !!!!"<<std::endl;
159  else {
160  outputFile<<"\n=================== Starting SimVertex access ==================="<<std::endl;
161  std::unique_ptr<MixCollection<SimVertex> > col2(new MixCollection<SimVertex>(cf_simvtx.product()));
163  int count2=0;
164  outputFile <<" \nWe got "<<col2->sizeSignal()<<" signal vertices and "<<col2->sizePileup()<<" pileup vertices, total: "<<col2->size()<<std::endl;
165  for (cfi2=col2->begin(); cfi2!=col2->end();cfi2++) {
166  histVertex_bunch_->Fill(cfi2.bunch());
167  int b = count2%4;
168  if (count2 == 0 || b==3){
169  //outputFile<<" SimVertex "<<count2<<" has parent index "<<cfi2->parentIndex()<<" bunchcr "<<cfi2.bunch()<<" trigger "<<cfi2.getTrigger()<<", from EncodedEventId: "<<cfi2->eventId().bunchCrossing() <<" "<<cfi2->eventId().event() <<std::endl;
170  }
171  //SimVertex myvtx=(*cfi2);
172  //outputFile<<"Same with op*: "<<count2<<" has parent index "<<myvtx.parentIndex()<<" bunchcr "<<cfi2.bunch()<<" trigger "<<cfi2.getTrigger()<<", from EncodedEventId: "<<myvtx.eventId().bunchCrossing() <<" "<<myvtx.eventId().event() <<std::endl;
173  count2++;
174  }
175  }
176 
177 
178  //test HepMCProducts
179  //------------------------------------
181  bool gotHepMCP = iEvent.getByToken(HepMCToken_,cf_hepmc);
182  if (!gotHepMCP) std::cout<<" Could not read HepMCProducts!!!!"<<std::endl;
183  else {
184  outputFile<<"\n=================== Starting HepMCProduct access ==================="<<std::endl;
185  std::unique_ptr<MixCollection<edm::HepMCProduct> > colhepmc(new MixCollection<edm::HepMCProduct>(cf_hepmc.product()));
187 
188  int count3=0;
189  outputFile <<" \nWe got "<<colhepmc->sizeSignal()<<" signal hepmc products and "<<colhepmc->sizePileup()<<" pileup hepmcs, total: "<<colhepmc->size()<<std::endl;
190  for (cfihepmc=colhepmc->begin(); cfihepmc!=colhepmc->end();cfihepmc++) {
191  histHepMCProduct_bunch_->Fill(cfihepmc.bunch());
192  int c = count3%4;
193  if (count3==0 || c==3){
194  //outputFile<<" edm::HepMCProduct "<<count3<<" has event number "<<cfihepmc->GetEvent()->event_number()<<", "<< cfihepmc->GetEvent()->particles_size()<<" particles and "<<cfihepmc->GetEvent()->vertices_size()<<" vertices, bunchcr= "<<cfihepmc.bunch()<<" trigger= "<<cfihepmc.getTrigger() <<" sourcetype= "<<cfihepmc.getSourceType()<<std::endl;
195  }
196  HepMCProduct myprod=colhepmc->getObject(count3);
197  //outputFile<<"same with getObject:hepmc product "<<count3<<" has event number "<<myprod.GetEvent()->event_number()<<", "<<myprod.GetEvent()->particles_size()<<" particles and "<<myprod.GetEvent()->vertices_size()<<" vertices"<<std::endl;
198  count3++;
199  }
200  }
201 
202 
203  // test CaloHits
204  //--------------------------
205  const std::string subdetcalo("g4SimHitsEcalHitsEB");
207  bool gotPCaloHit = iEvent.getByToken(CaloToken1_,cf_calo);
208  if (!gotPCaloHit) outputFile<<" Could not read CaloHits with label "<<subdetcalo<<"!!!!"<<std::endl;
209  else {
210  outputFile<<"\n\n=================== Starting CaloHit access, subdet "<<subdetcalo<<" ==================="<<std::endl;
211  std::unique_ptr<MixCollection<PCaloHit> > colcalo(new MixCollection<PCaloHit>(cf_calo.product()));
212  //outputFile<<*(colcalo.get())<<std::endl;
214  int count4=0;
215  for (cficalo=colcalo->begin(); cficalo!=colcalo->end();cficalo++) {
216  histPCaloHit_bunch_->Fill(cficalo.bunch());
217  int d = count4%4;
218  if (count4==0 || d==3){
219  //outputFile<<" CaloHit "<<count4<<" has tof "<<cficalo->time()<<" trackid "<<cficalo->geantTrackId() <<" bunchcr "<<cficalo.bunch()<<" trigger "<<cficalo.getTrigger()<<", from EncodedEventId: "<<cficalo->eventId().bunchCrossing()<<" " <<cficalo->eventId().event() <<std::endl;
220  }
221  count4++;
222  }
223  }
224 
225 
226  // test PSimHit for one particular subdet
227  //---------------------------------------
228 
229 
230  const std::string subdet("g4SimHitsTrackerHitsTECHighTof");
232  bool gotPSimHit = iEvent.getByToken(TrackerToken0_,cf_simhit);
233  if (!gotPSimHit) outputFile<<" Could not read SimHits with label "<<subdet<<"!!!!"<<std::endl;
234  else {
235  outputFile<<"\n\n=================== Starting SimHit access, subdet "<<subdet<<" ==================="<<std::endl;
236 
237  std::unique_ptr<MixCollection<PSimHit> > col(new MixCollection<PSimHit>(cf_simhit.product()));
238  //outputFile<<*(col.get())<<std::endl;
240  int count5=0;
241  for (cfi=col->begin(); cfi!=col->end();cfi++) {
242 
243  // Signal
244  if (cfi.getTrigger()==1){
246  tofhist_sig_->Fill(cfi->timeOfFlight());
247  }
248 
249  // Pileups
250  if (cfi.getTrigger()==0){
252  std::cout << " cfi->timeOfFlight() = " << cfi->timeOfFlight() << std::endl;
253  tofhist_->Fill(cfi->timeOfFlight());
254  }
255 
256  int e = count5%4;
257  if (e==3){
258  outputFile<<" Hit "<<count5<<" has tof "<<cfi->timeOfFlight()<<" trackid "<<cfi->trackId() <<" bunchcr "<<cfi.bunch()<<" trigger "<<cfi.getTrigger()<<", from EncodedEventId: "<<cfi->eventId().bunchCrossing()<<" " <<cfi->eventId().event() <<" bcr from MixCol "<<cfi.bunch()<<std::endl;
259  }
260  count5++;
261  }
262  }
263 
264  const std::string subdet1("g4SimHitsMuonCSCHits");
266  bool gotPSimHit1 = iEvent.getByToken(MuonToken_,cf_simhit1);
267  if (!gotPSimHit1) outputFile<<" Could not read SimHits with label "<<subdet1<<"!!!!"<<std::endl;
268  else {
269  outputFile<<"\n\n=================== Starting SimHit access, subdet "<<subdet1<<" ==================="<<std::endl;
270 
271  std::unique_ptr<MixCollection<PSimHit> > col(new MixCollection<PSimHit>(cf_simhit1.product()));
272  //outputFile<<*(col.get())<<std::endl;
274  int count5=0;
275  for (cfi=col->begin(); cfi!=col->end();cfi++) {
276 
277  if (cfi.getTrigger()==1) histPSimHit_bunchSignal_MuonCSCHits_->Fill(cfi.bunch());
278 
279  if (cfi.getTrigger()==0) histPSimHit_bunchPileups_MuonCSCHits_->Fill(cfi.bunch());
280 
281  int e = count5%4;
282  if (e==3){
283  outputFile<<" Hit "<<count5<<" has tof "<<cfi->timeOfFlight()<<" trackid "<<cfi->trackId() <<" bunchcr "<<cfi.bunch()<<" trigger "<<cfi.getTrigger()<<", from EncodedEventId: "<<cfi->eventId().bunchCrossing()<<" " <<cfi->eventId().event() <<" bcr from MixCol "<<cfi.bunch()<<std::endl;
284  }
285  count5++;
286  }
287  }
288 
289 
290 
291 }
292 
293 
294 // ------------ method called once each job just before starting event loop ------------
295 void
297 {
298  outputFile.open("test.log");
299  if (!outputFile.is_open())
300  {
301  std::cout << "Unable to open file!" << std::endl;
302  }
303 
304 }
305 
306 // ------------ method called once each job just after ending the event loop ------------
307 void
309 
310  outputFile.close();
311 
312  char t[30];
313  sprintf(t,"%s",fileName_.c_str());
314  std::cout << " fileName = " << t << std::endl;
315  histFile_=new TFile(t,"RECREATE");
316  histTrack_bunchPileups_->Write();
317  histTrack_bunchSignal_->Write();
318  histVertex_bunch_->Write();
319  histPCaloHit_bunch_->Write();
322  tofhist_sig_->Write();
323  tofhist_->Write();
326  histHepMCProduct_bunch_->Write();
327  histFile_->Write();
328  histFile_->Close();
329 
330 }
331 }//edm
TH1I * histPSimHit_bunchPileups_TrackerHitsTECHighTof_
TH1I * histPSimHit_bunchSignal_MuonCSCHits_
TH1I * histPSimHit_bunchPileups_MuonCSCHits_
edm::EDGetTokenT< CrossingFrame< PSimHit > > TrackerToken0_
edm::EDGetTokenT< CrossingFrame< SimTrack > > SimTrackToken_
TH1I * histPSimHit_bunchSignal_TrackerHitsTECHighTof_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
edm::EDGetTokenT< CrossingFrame< PSimHit > > MuonToken_
bool getTrigger() const
Definition: MixCollection.h:96
int iEvent
Definition: GenABIO.cc:230
edm::EDGetTokenT< CrossingFrame< PCaloHit > > CaloToken1_
edm::EDGetTokenT< CrossingFrame< SimVertex > > SimVertexToken_
T const * product() const
Definition: Handle.h:81
double b
Definition: hdecay.h:120
std::ofstream outputFile
HLT enums.
double a
Definition: hdecay.h:121
edm::EDGetTokenT< CrossingFrame< edm::HepMCProduct > > HepMCToken_
col
Definition: cuy.py:1009
void analyze(const edm::Event &, const edm::EventSetup &) override
void endJob() override
TestMixedSource(const edm::ParameterSet &)
void beginJob() override