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