CMS 3D CMS Logo

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