CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/SimGeneral/MixingModule/plugins/TestMix.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Class:      TestMix
00004 // 
00010 //
00011 // Original Author:  Ursula Berthon
00012 //         Created:  Fri Sep 23 11:38:38 CEST 2005
00013 // $Id: TestMix.cc,v 1.20 2011/11/15 21:57:47 gowdy Exp $
00014 //
00015 //
00016 
00017 
00018 // system include files
00019 #include <memory>
00020 #include <utility>
00021 #include <iostream>
00022 
00023 // user include files
00024 #include "FWCore/Framework/interface/Frameworkfwd.h"
00025 #include "FWCore/Framework/interface/EDAnalyzer.h"
00026 
00027 #include "FWCore/Framework/interface/Event.h"
00028 #include "FWCore/Framework/interface/MakerMacros.h"
00029 
00030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00031 
00032 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00033 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
00034 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
00035 
00036 #include "TestMix.h"
00037 
00038 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
00039 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00040 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00041 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00042 
00043 
00044 using namespace edm;
00045 
00046 TestMix::TestMix(const edm::ParameterSet& iConfig): 
00047   level_(iConfig.getUntrackedParameter<int>("PrintLevel"))
00048 {
00049   std::cout << "Constructed testMix , level "<<level_<<std::endl;
00050 
00051   track_containers_.push_back("g4SimHitsTrackerHitsTECHighTof");
00052   track_containers_.push_back("g4SimHitsTrackerHitsTECLowTof");
00053 
00054   track_containers2_.push_back("g4SimHitsTrackerHitsTECLowTof");
00055   track_containers2_.push_back("g4SimHitsTrackerHitsTECHighTof");
00056 
00057 }
00058 
00059 
00060 TestMix::~TestMix()
00061 {
00062  
00063   // do anything here that needs to be done at desctruction time
00064   // (e.g. close files, deallocate resources etc.)
00065 
00066 }
00067 
00068 
00069 //
00070 // member functions
00071 //
00072 
00073 // ------------ method called to analyze the data  ------------
00074 void
00075 TestMix::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00076 {
00077   using namespace edm;
00078   bool got;
00079   int count=0;
00080 
00081   // test accesses to CrossingFrame
00082   // attention: operator-> returns the templated object, but
00083   // bunch() and getTrigger() are methods of the iterator itself!
00084 
00085   // test access to SimTracks directly in CrossingFrame
00086 
00087   edm::Handle<CrossingFrame<SimTrack> > cf_simtrack;
00088   bool gotTracks = iEvent.getByLabel("mix","g4SimHits",cf_simtrack);
00089   if (!gotTracks)  std::cout<<" Could not read SimTracks!!!!"<<std::endl;
00090 
00091   // not pointer compatible!!!!
00092 //   if (gotTracks) {
00093 //     std::vector<SimTrack>::const_iterator first;
00094 //     std::vector<SimTrack>::const_iterator last;
00095 //     cf_simtrack->getPileups(first,last);
00096 //     unsigned int ic=0;
00097 //     for (std::vector<SimTrack>::const_iterator it=first;it!=last;it++) {
00098 //       std::cout<<" pileup SimTrack "<<ic<<" has genpart index  "<<(*it).genpartIndex()<<" vertex Index "<<(*it).vertIndex()  <<" bunchcrossing "<<cf_simtrack->getBunchCrossing(ic)<<std::endl;
00099 //       ic++;
00100 //     }
00101 //   } 
00102 
00103 
00104   const std::string subdet("g4SimHitsTrackerHitsTECHighTof");
00105   edm::Handle<CrossingFrame<PSimHit> > cf_simhit;
00106   got = iEvent.getByLabel("mix",subdet,cf_simhit);
00107   if (!got) std::cout<<" Could not read SimHits with label "<<subdet<<"!!!!"<<std::endl;
00108   else {
00109     std::cout<<"\n\n=================== Starting SimHit access, subdet "<<subdet<<"  ==================="<<std::endl;
00110 
00111     std::auto_ptr<MixCollection<PSimHit> > col(new MixCollection<PSimHit>(cf_simhit.product(),std::pair<int,int>(-1,1)));
00112     std::cout<<*(col.get())<<std::endl;
00113     MixCollection<PSimHit>::iterator cfi;
00114     for (cfi=col->begin(); cfi!=col->end();cfi++) {
00115       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;
00116       //      std::cout<<" Hit: "<<(*cfi)<<std::endl;
00117       count++;
00118     }
00119   }
00120 
00121   // test access to CaloHits
00122   const std::string subdetcalo("g4SimHitsEcalHitsEB");
00123   edm::Handle<CrossingFrame<PCaloHit> > cf_calo;
00124   got = iEvent.getByLabel("mix",subdetcalo,cf_calo);
00125   if (!got) std::cout<<" Could not read CaloHits with label "<<subdetcalo<<"!!!!"<<std::endl;
00126   else {
00127     std::cout<<"\n\n=================== Starting CaloHit access, subdet "<<subdetcalo<<"  ==================="<<std::endl;
00128     std::auto_ptr<MixCollection<PCaloHit> > colcalo(new MixCollection<PCaloHit>(cf_calo.product(), std::pair<int,int>(-1,1)));
00129     std::cout<<*(colcalo.get())<<std::endl;
00130     MixCollection<PCaloHit>::iterator cficalo;
00131     count=0;
00132     for (cficalo=colcalo->begin(); cficalo!=colcalo->end();cficalo++) {
00133       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;
00134       //      std::cout<<" Calo Hit: "<<(*cficalo)<<std::endl;
00135       count++;
00136     }
00137   }
00138 
00139   // test access to SimTracks
00140   if (gotTracks) {
00141     std::cout<<"\n=================== Starting SimTrack access ==================="<<std::endl;
00142     //   edm::Handle<CrossingFrame<SimTrack> > cf_simtrack;
00143     //   iEvent.getByLabel("mix",cf_simtrack);
00144     std::auto_ptr<MixCollection<SimTrack> > col2(new MixCollection<SimTrack>(cf_simtrack.product()));
00145     MixCollection<SimTrack>::iterator cfi2;
00146     int count2=0;
00147     std::cout <<" \nWe got "<<col2->sizeSignal()<<" signal tracks and "<<col2->sizePileup()<<" pileup tracks, total: "<<col2->size()<<std::endl;
00148     for (cfi2=col2->begin(); cfi2!=col2->end();cfi2++) {
00149       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;
00150       count2++; 
00151     }
00152   }
00153 
00154   // test access to SimVertices
00155   edm::Handle<CrossingFrame<SimVertex> > cf_simvtx;
00156   got = iEvent.getByLabel("mix","g4SimHits",cf_simvtx);
00157   if (!got) std::cout<<" Could not read Simvertices !!!!"<<std::endl;
00158   else {
00159     std::cout<<"\n=================== Starting SimVertex access ==================="<<std::endl;
00160     std::auto_ptr<MixCollection<SimVertex> > col3(new MixCollection<SimVertex>(cf_simvtx.product()));
00161     MixCollection<SimVertex>::iterator cfi3;
00162     int count3=0;
00163     std::cout <<" \nWe got "<<col3->sizeSignal()<<" signal vertices and "<<col3->sizePileup()<<" pileup vertices, total: "<<col3->size()<<std::endl;
00164     for (cfi3=col3->begin(); cfi3!=col3->end();cfi3++) {
00165       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;
00166       SimVertex myvtx=(*cfi3);
00167       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;
00168       count3++; 
00169     }
00170   }
00171 
00172   //test MixCollection constructor with several subdetector names
00173   bool got1,got2=false;
00174   std::auto_ptr<MixCollection<PSimHit> > all_trackhits;
00175   std::auto_ptr<MixCollection<PSimHit> > all_trackhits2;
00176   std::cout<<"\n=================== Starting test for coll of several ROU-s ==================="<<std::endl;
00177   //  edm::Handle<CrossingFrame<PSimHit> > cf_simhit;
00178   std::vector<const CrossingFrame<PSimHit> *> cfvec;
00179   got1 = iEvent.getByLabel("mix",track_containers_[0],cf_simhit);
00180   if (!got1) std::cout<<" Could not read SimHits with label "<<track_containers_[0]<<"!!!!"<<std::endl;
00181   else {
00182     std::cout<<"\n=================== Starting test for coll of several ROU-s ==================="<<std::endl;
00183     cfvec.push_back(cf_simhit.product());
00184     std::cout <<" \nFirst container "<<track_containers_[0]<<" Nr signals "<<cf_simhit->getNrSignals() << ", Nr pileups "<<cf_simhit->getNrPileups() <<std::endl;
00185     got2 = iEvent.getByLabel("mix",track_containers_[1],cf_simhit);
00186     if (got2) {
00187       cfvec.push_back(cf_simhit.product());
00188       std::cout <<" \nSecond container "<<track_containers_[1]<<" Nr signals "<<cf_simhit->getNrSignals() << ", Nr pileups "<<cf_simhit->getNrPileups() <<std::endl;
00189       all_trackhits= std::auto_ptr<MixCollection<PSimHit> >(new MixCollection<PSimHit>(cfvec));
00190 
00191       std::cout <<" \nFor all containers we got "<<all_trackhits->sizeSignal()<<" signal hits and "<<all_trackhits->sizePileup()<<" pileup hits, total: "<<all_trackhits->size()<<std::endl;
00192     
00193       MixCollection<PSimHit>::iterator it;
00194       int ii=0;
00195       for (it=all_trackhits->begin(); it!= all_trackhits->end();it++) {
00196         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;
00197         ii++;
00198       }
00199     }
00200   }
00201 
00202   //test the same in different order: should be the same sizes, different order
00203   MixCollection<PSimHit>::iterator it2;
00204   int ii2=0;
00205   std::vector<const CrossingFrame<PSimHit> *> cfvec2;
00206   got = iEvent.getByLabel("mix",track_containers2_[0],cf_simhit);
00207   if (!got) std::cout<<" Could not read SimHits with label "<<track_containers2_[0]<<"!!!!"<<std::endl;
00208   else {
00209     cfvec2.push_back(cf_simhit.product());
00210     got2 = iEvent.getByLabel("mix",track_containers2_[1],cf_simhit);
00211     if (got2) {
00212       cfvec2.push_back(cf_simhit.product());
00213       all_trackhits2= std::auto_ptr<MixCollection<PSimHit> > (new MixCollection<PSimHit>(cfvec2));
00214       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;
00215       for (it2=all_trackhits2->begin(); it2!= all_trackhits2->end();it2++) {
00216         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;
00217         ii2++;
00218       }
00219     }
00220   }
00221 
00222   //test MixCollection for HepMCProducts
00223   //at the same time test getObject method
00224   //we should have each line twice
00225   //------------------------------------
00226   edm::Handle<CrossingFrame<edm::HepMCProduct> > cf_hepmc;
00227   got = iEvent.getByLabel("mix","generator",cf_hepmc);
00228   if (!got) std::cout<<" Could not read HepMCProducts!!!!"<<std::endl;
00229   else {
00230     std::auto_ptr<MixCollection<edm::HepMCProduct> > colhepmc(new MixCollection<edm::HepMCProduct>(cf_hepmc.product()));
00231     MixCollection<edm::HepMCProduct>::iterator cfihepmc;
00232     int counthepmc=0;
00233     std::cout <<" \nWe got "<<colhepmc->sizeSignal()<<" signal hepmc products and "<<colhepmc->sizePileup()<<" pileup hepmcs, total: "<<colhepmc->size()<<std::endl;
00234     for (cfihepmc=colhepmc->begin(); cfihepmc!=colhepmc->end();cfihepmc++) {
00235       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;
00236       HepMCProduct myprod=colhepmc->getObject(counthepmc);
00237       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;
00238       counthepmc++;
00239     }
00240   }
00241   //----------------------------------------------------------------------------
00242   //  testing special situations
00243   //----------------------------------------------------------------------------
00244  
00245   if (got2) {
00246     // test reusage of the same iterator
00247     int ii3=0;
00248     for (it2=all_trackhits2->begin(); it2!= all_trackhits2->end();it2++) ii3++;
00249     if (ii3!=ii2) std::cout<<" Problem when re-using iterator!!"<<std::endl;
00250     else  std::cout<<" \nNo problem when re-using iterator."<<std::endl;
00251   }
00252   // test access to non-filled collections
00253   //cases:   0) ok, collection has members
00254   //         1) bunchrange given outside of existent bunchcrossing numbers ==>exc
00255 
00256   std::cout<<"\n=================== Starting tests for abnormal conditions ==================="<<std::endl;
00257 
00258   // test case 0
00259   if (got1) {
00260     std::cout<<"\n[ Testing abnormal conditions case 0]Should be all ok: registry: "<<all_trackhits->inRegistry()<<" size: "<<all_trackhits->size()<<std::endl;
00261 
00262                                                   // test case 1
00263                                                   std::cout<<"\n[ Testing abnormal conditions case 1] Should throw an exception " <<std::endl;
00264                                                                                                 MixCollection<PSimHit> * col21=0;
00265                                                                                                 col21=new MixCollection<PSimHit>(cf_simhit.product(),std::pair<int,int>(-10,20));
00266                                                                                                 delete col21;
00267   }
00268 }
00269