CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TestMix.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Class: TestMix
4 //
10 //
11 // Original Author: Ursula Berthon
12 // Created: Fri Sep 23 11:38:38 CEST 2005
13 //
14 //
15 
16 
17 // system include files
18 #include <memory>
19 #include <utility>
20 #include <iostream>
21 
22 // user include files
25 
28 
30 
34 
35 #include "TestMix.h"
36 
41 
42 
43 using namespace edm;
44 
46  level_(iConfig.getUntrackedParameter<int>("PrintLevel"))
47 {
48  std::cout << "Constructed testMix , level "<<level_<<std::endl;
49 
50  track_containers_.push_back("g4SimHitsTrackerHitsTECHighTof");
51  track_containers_.push_back("g4SimHitsTrackerHitsTECLowTof");
52 
53  track_containers2_.push_back("g4SimHitsTrackerHitsTECLowTof");
54  track_containers2_.push_back("g4SimHitsTrackerHitsTECHighTof");
55 
56 }
57 
58 
60 {
61 
62  // do anything here that needs to be done at desctruction time
63  // (e.g. close files, deallocate resources etc.)
64 
65 }
66 
67 
68 //
69 // member functions
70 //
71 
72 // ------------ method called to analyze the data ------------
73 void
75 {
76  using namespace edm;
77  bool got;
78  int count=0;
79 
80  // test accesses to CrossingFrame
81  // attention: operator-> returns the templated object, but
82  // bunch() and getTrigger() are methods of the iterator itself!
83 
84  // test access to SimTracks directly in CrossingFrame
85 
87  bool gotTracks = iEvent.getByLabel("mix","g4SimHits",cf_simtrack);
88  if (!gotTracks) std::cout<<" Could not read SimTracks!!!!"<<std::endl;
89 
90  // not pointer compatible!!!!
91 // if (gotTracks) {
92 // std::vector<SimTrack>::const_iterator first;
93 // std::vector<SimTrack>::const_iterator last;
94 // cf_simtrack->getPileups(first,last);
95 // unsigned int ic=0;
96 // for (std::vector<SimTrack>::const_iterator it=first;it!=last;it++) {
97 // std::cout<<" pileup SimTrack "<<ic<<" has genpart index "<<(*it).genpartIndex()<<" vertex Index "<<(*it).vertIndex() <<" bunchcrossing "<<cf_simtrack->getBunchCrossing(ic)<<std::endl;
98 // ic++;
99 // }
100 // }
101 
102 
103  const std::string subdet("g4SimHitsTrackerHitsTECHighTof");
105  got = iEvent.getByLabel("mix",subdet,cf_simhit);
106  if (!got) std::cout<<" Could not read SimHits with label "<<subdet<<"!!!!"<<std::endl;
107  else {
108  std::cout<<"\n\n=================== Starting SimHit access, subdet "<<subdet<<" ==================="<<std::endl;
109 
110  std::auto_ptr<MixCollection<PSimHit> > col(new MixCollection<PSimHit>(cf_simhit.product(),std::pair<int,int>(-1,1)));
111  std::cout<<*(col.get())<<std::endl;
113  for (cfi=col->begin(); cfi!=col->end();cfi++) {
114  std::cout<<" Hit "<<count<<" 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;
115  // std::cout<<" Hit: "<<(*cfi)<<std::endl;
116  count++;
117  }
118  }
119 
120  // test access to CaloHits
121  const std::string subdetcalo("g4SimHitsEcalHitsEB");
123  got = iEvent.getByLabel("mix",subdetcalo,cf_calo);
124  if (!got) std::cout<<" Could not read CaloHits with label "<<subdetcalo<<"!!!!"<<std::endl;
125  else {
126  std::cout<<"\n\n=================== Starting CaloHit access, subdet "<<subdetcalo<<" ==================="<<std::endl;
127  std::auto_ptr<MixCollection<PCaloHit> > colcalo(new MixCollection<PCaloHit>(cf_calo.product(), std::pair<int,int>(-1,1)));
128  std::cout<<*(colcalo.get())<<std::endl;
130  count=0;
131  for (cficalo=colcalo->begin(); cficalo!=colcalo->end();cficalo++) {
132  std::cout<<" CaloHit "<<count<<" has tof "<<cficalo->time()<<" trackid "<<cficalo->geantTrackId() <<" bunchcr "<<cficalo.bunch()<<" trigger "<<cficalo.getTrigger()<<", from EncodedEventId: "<<cficalo->eventId().bunchCrossing()<<" " <<cficalo->eventId().event() <<std::endl;
133  // std::cout<<" Calo Hit: "<<(*cficalo)<<std::endl;
134  count++;
135  }
136  }
137 
138  // test access to SimTracks
139  if (gotTracks) {
140  std::cout<<"\n=================== Starting SimTrack access ==================="<<std::endl;
141  // edm::Handle<CrossingFrame<SimTrack> > cf_simtrack;
142  // iEvent.getByLabel("mix",cf_simtrack);
143  std::auto_ptr<MixCollection<SimTrack> > col2(new MixCollection<SimTrack>(cf_simtrack.product()));
145  int count2=0;
146  std::cout <<" \nWe got "<<col2->sizeSignal()<<" signal tracks and "<<col2->sizePileup()<<" pileup tracks, total: "<<col2->size()<<std::endl;
147  for (cfi2=col2->begin(); cfi2!=col2->end();cfi2++) {
148  std::cout<<" SimTrack "<<count2<<" has genpart index "<<cfi2->genpartIndex()<<" vertex Index "<<cfi2->vertIndex() <<" bunchcr "<<cfi2.bunch()<<" trigger "<<cfi2.getTrigger()<<", from EncodedEventId: "<<cfi2->eventId().bunchCrossing() <<" "<<cfi2->eventId().event() <<std::endl;
149  count2++;
150  }
151  }
152 
153  // test access to SimVertices
155  got = iEvent.getByLabel("mix","g4SimHits",cf_simvtx);
156  if (!got) std::cout<<" Could not read Simvertices !!!!"<<std::endl;
157  else {
158  std::cout<<"\n=================== Starting SimVertex access ==================="<<std::endl;
159  std::auto_ptr<MixCollection<SimVertex> > col3(new MixCollection<SimVertex>(cf_simvtx.product()));
161  int count3=0;
162  std::cout <<" \nWe got "<<col3->sizeSignal()<<" signal vertices and "<<col3->sizePileup()<<" pileup vertices, total: "<<col3->size()<<std::endl;
163  for (cfi3=col3->begin(); cfi3!=col3->end();cfi3++) {
164  std::cout<<" SimVertex "<<count3<<" has parent index "<<cfi3->parentIndex()<<" bunchcr "<<cfi3.bunch()<<" trigger "<<cfi3.getTrigger()<<", from EncodedEventId: "<<cfi3->eventId().bunchCrossing() <<" "<<cfi3->eventId().event() <<std::endl;
165  SimVertex myvtx=(*cfi3);
166  std::cout<<"Same with op*: "<<count3<<" has parent index "<<myvtx.parentIndex()<<" bunchcr "<<cfi3.bunch()<<" trigger "<<cfi3.getTrigger()<<", from EncodedEventId: "<<myvtx.eventId().bunchCrossing() <<" "<<myvtx.eventId().event() <<std::endl;
167  count3++;
168  }
169  }
170 
171  //test MixCollection constructor with several subdetector names
172  bool got1,got2=false;
173  std::auto_ptr<MixCollection<PSimHit> > all_trackhits;
174  std::auto_ptr<MixCollection<PSimHit> > all_trackhits2;
175  std::cout<<"\n=================== Starting test for coll of several ROU-s ==================="<<std::endl;
176  // edm::Handle<CrossingFrame<PSimHit> > cf_simhit;
177  std::vector<const CrossingFrame<PSimHit> *> cfvec;
178  got1 = iEvent.getByLabel("mix",track_containers_[0],cf_simhit);
179  if (!got1) std::cout<<" Could not read SimHits with label "<<track_containers_[0]<<"!!!!"<<std::endl;
180  else {
181  std::cout<<"\n=================== Starting test for coll of several ROU-s ==================="<<std::endl;
182  cfvec.push_back(cf_simhit.product());
183  std::cout <<" \nFirst container "<<track_containers_[0]<<" Nr signals "<<cf_simhit->getNrSignals() << ", Nr pileups "<<cf_simhit->getNrPileups() <<std::endl;
184  got2 = iEvent.getByLabel("mix",track_containers_[1],cf_simhit);
185  if (got2) {
186  cfvec.push_back(cf_simhit.product());
187  std::cout <<" \nSecond container "<<track_containers_[1]<<" Nr signals "<<cf_simhit->getNrSignals() << ", Nr pileups "<<cf_simhit->getNrPileups() <<std::endl;
188  all_trackhits= std::auto_ptr<MixCollection<PSimHit> >(new MixCollection<PSimHit>(cfvec));
189 
190  std::cout <<" \nFor all containers we got "<<all_trackhits->sizeSignal()<<" signal hits and "<<all_trackhits->sizePileup()<<" pileup hits, total: "<<all_trackhits->size()<<std::endl;
191 
193  int ii=0;
194  for (it=all_trackhits->begin(); it!= all_trackhits->end();it++) {
195  std::cout<<" Hit "<<ii<<" of all hits has tof "<<it->timeOfFlight()<<" trackid "<<it->trackId() <<" bunchcr "<<it.bunch()<<" trigger "<<it.getTrigger()<<", from EncodedEventId: "<<it->eventId().bunchCrossing() <<" "<<it->eventId().event()<<std::endl;
196  ii++;
197  }
198  }
199  }
200 
201  //test the same in different order: should be the same sizes, different order
203  int ii2=0;
204  std::vector<const CrossingFrame<PSimHit> *> cfvec2;
205  got = iEvent.getByLabel("mix",track_containers2_[0],cf_simhit);
206  if (!got) std::cout<<" Could not read SimHits with label "<<track_containers2_[0]<<"!!!!"<<std::endl;
207  else {
208  cfvec2.push_back(cf_simhit.product());
209  got2 = iEvent.getByLabel("mix",track_containers2_[1],cf_simhit);
210  if (got2) {
211  cfvec2.push_back(cf_simhit.product());
212  all_trackhits2= std::auto_ptr<MixCollection<PSimHit> > (new MixCollection<PSimHit>(cfvec2));
213  std::cout <<" \nSame containers, different order: we got "<<all_trackhits2->sizeSignal()<<" signal hits and "<<all_trackhits2->sizePileup()<<" pileup hits, total: "<<all_trackhits2->size()<<std::endl;
214  for (it2=all_trackhits2->begin(); it2!= all_trackhits2->end();it2++) {
215  std::cout<<" Hit "<<ii2<<" of all hits has tof "<<it2->timeOfFlight()<<" trackid "<<it2->trackId() <<" bunchcr "<<it2.bunch()<<" trigger "<<it2.getTrigger()<<", bcr from Id: "<<it2->eventId().bunchCrossing() <<" evtnr in id "<<it2->eventId().event()<<std::endl;
216  ii2++;
217  }
218  }
219  }
220 
221  //test MixCollection for HepMCProducts
222  //at the same time test getObject method
223  //we should have each line twice
224  //------------------------------------
226  got = iEvent.getByLabel("mix","generator",cf_hepmc);
227  if (!got) std::cout<<" Could not read HepMCProducts!!!!"<<std::endl;
228  else {
229  std::auto_ptr<MixCollection<edm::HepMCProduct> > colhepmc(new MixCollection<edm::HepMCProduct>(cf_hepmc.product()));
231  int counthepmc=0;
232  std::cout <<" \nWe got "<<colhepmc->sizeSignal()<<" signal hepmc products and "<<colhepmc->sizePileup()<<" pileup hepmcs, total: "<<colhepmc->size()<<std::endl;
233  for (cfihepmc=colhepmc->begin(); cfihepmc!=colhepmc->end();cfihepmc++) {
234  std::cout<<" edm::HepMCProduct "<<counthepmc<<" 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;
235  HepMCProduct myprod=colhepmc->getObject(counthepmc);
236  std::cout<<"same with getObject:hepmc product "<<counthepmc<<" has event number "<<myprod.GetEvent()->event_number()<<", "<<myprod.GetEvent()->particles_size()<<" particles and "<<myprod.GetEvent()->vertices_size()<<" vertices"<<std::endl;
237  counthepmc++;
238  }
239  }
240  //----------------------------------------------------------------------------
241  // testing special situations
242  //----------------------------------------------------------------------------
243 
244  if (got2) {
245  // test reusage of the same iterator
246  int ii3=0;
247  for (it2=all_trackhits2->begin(); it2!= all_trackhits2->end();it2++) ii3++;
248  if (ii3!=ii2) std::cout<<" Problem when re-using iterator!!"<<std::endl;
249  else std::cout<<" \nNo problem when re-using iterator."<<std::endl;
250  }
251  // test access to non-filled collections
252  //cases: 0) ok, collection has members
253  // 1) bunchrange given outside of existent bunchcrossing numbers ==>exc
254 
255  std::cout<<"\n=================== Starting tests for abnormal conditions ==================="<<std::endl;
256 
257  // test case 0
258  if (got1) {
259  std::cout<<"\n[ Testing abnormal conditions case 0]Should be all ok: registry: "<<all_trackhits->inRegistry()<<" size: "<<all_trackhits->size()<<std::endl;
260 
261  // test case 1
262  std::cout<<"\n[ Testing abnormal conditions case 1] Should throw an exception " <<std::endl;
263  MixCollection<PSimHit> * col21=0;
264  col21=new MixCollection<PSimHit>(cf_simhit.product(),std::pair<int,int>(-10,20));
265  delete col21;
266  }
267 }
268 
int event() const
get the contents of the subdetector field (should be protected?)
int ii
Definition: cuy.py:588
bool getTrigger() const
Definition: MixCollection.h:89
int iEvent
Definition: GenABIO.cc:243
int parentIndex() const
Definition: SimVertex.h:33
std::vector< std::string > track_containers2_
Definition: TestMix.h:47
int bunchCrossing() const
get the detector field from this detid
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:35
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: TestMix.cc:74
std::vector< std::string > track_containers_
Definition: TestMix.h:46
EncodedEventId eventId() const
Definition: CoreSimVertex.h:30
tuple cout
Definition: gather_cfg.py:121
int getSourceType() const
Definition: MixCollection.h:91
int col
Definition: cuy.py:1008
TestMix(const edm::ParameterSet &)
Definition: TestMix.cc:45
int level_
Definition: TestMix.h:45