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