CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/SimGeneral/MixingModule/plugins/TestMixedSource.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    TestMixedSource
00004 // Class:      TestMixedSource
00005 // 
00013 //
00014 // Original Author:  Emilia Lubenova Becheva
00015 //         Created:  Wed May 20 16:46:58 CEST 2009
00016 // $Id: TestMixedSource.cc,v 1.5 2011/11/15 21:57:47 gowdy Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <memory>
00023 
00024 // user include files
00025 #include "FWCore/Framework/interface/Frameworkfwd.h"
00026 #include "FWCore/Framework/interface/EDAnalyzer.h"
00027 
00028 #include "FWCore/Framework/interface/Event.h"
00029 #include "FWCore/Framework/interface/MakerMacros.h"
00030 
00031 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00032 
00033 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00034 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
00035 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
00036 
00037 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
00038 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00039 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00040 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00041 
00042 #include "TH1I.h"
00043 #include "TFile.h"
00044 
00045 #include "TestMixedSource.h"
00046 
00047 #include <iostream>
00048 #include <fstream>
00049 
00050 
00051 //
00052 // constructors and destructor
00053 //
00054 namespace edm
00055 {
00056 TestMixedSource::TestMixedSource(const edm::ParameterSet& iConfig)
00057 : fileName_(iConfig.getParameter<std::string>("fileName")), minbunch_(iConfig.getParameter<int>("minBunch")),maxbunch_(iConfig.getParameter<int>("maxBunch"))
00058 { 
00059   
00060   histTrack_bunchSignal_ = new TH1I("histoTrackSignal","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
00061   histTrack_bunchPileups_ = new TH1I("histoTrackPileups","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
00062 
00063   // Vertex
00064   histVertex_bunch_ = new TH1I("histoVertex","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
00065   
00066   // PCaloHit
00067   histPCaloHit_bunch_ = new TH1I("histoPCaloHit","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
00068   
00069   // PSimHit
00070   histPSimHit_bunchSignal_TrackerHitsTECHighTof_ = new TH1I("histoPSimHitTrackerHitsTECHighTofSignal","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
00071   histPSimHit_bunchPileups_TrackerHitsTECHighTof_ = new TH1I("histoPSimHitTrackerHitsTECHighTofPileups","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
00072   histPSimHit_bunchSignal_MuonCSCHits_ = new TH1I("histoPSimHitMuonCSCHitsSignal","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
00073   histPSimHit_bunchPileups_MuonCSCHits_ = new TH1I("histoPSimHitMuonCSCHitsPileups","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
00074 
00075   int bsp = 25;//bunchspace
00076   tofhist_ = new TH1I ("TrackerHit_Tof_bcr","TrackerHit_ToF",100,float(bsp*minbunch_),float(bsp*maxbunch_)+50.);
00077   tofhist_sig_ = new TH1I("SignalTrackerHit_Tof_bcr","TrackerHit_ToF",100,float(bsp*minbunch_),float(bsp*maxbunch_)+50.);
00078 
00079 
00080   // HepMCProduct
00081   histHepMCProduct_bunch_ = new TH1I("histoHepMCProduct","Bunchcrossings",maxbunch_-minbunch_+1,minbunch_,maxbunch_+1);
00082 
00083 }
00084 
00085 
00086 TestMixedSource::~TestMixedSource()
00087 { std::cout << " Destructor TestMixed"  << std::endl;
00088 
00089 }
00090 
00091 
00092 //
00093 // member functions
00094 //
00095 
00096 // ------------ method called to for each event  ------------
00097 void
00098 TestMixedSource::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00099 { 
00100   // test SimTracks
00101   //----------------------
00102   edm::Handle<CrossingFrame<SimTrack> > cf_simtrack;
00103   bool gotTracks = iEvent.getByLabel("mix","g4SimHits",cf_simtrack);
00104   if (!gotTracks)  outputFile<<" Could not read SimTracks!!!!" 
00105                    << " Please, check if the object SimTracks has been declared in the"
00106                    << " MixingModule configuration file."<<std::endl;
00107   
00108   if (gotTracks) {
00109     outputFile<<"\n=================== Starting SimTrack access ==================="<<std::endl;
00110 
00111     std::auto_ptr<MixCollection<SimTrack> > col1(new MixCollection<SimTrack>(cf_simtrack.product()));
00112     MixCollection<SimTrack>::iterator cfi1;
00113     int count1=0;
00114     std::cout <<" \nWe got "<<col1->sizeSignal()<<" signal tracks and "<<col1->sizePileup()<<" pileup tracks, total: "<<col1->size()<<std::endl;
00115     for (cfi1=col1->begin(); cfi1!=col1->end();cfi1++) {
00116       //std::cout << " BUNCH cfi1.bunch() = " << cfi1.bunch() << std::endl;
00117       if (cfi1.getTrigger()==0){
00118       histTrack_bunchPileups_->Fill(cfi1.bunch());
00119       }
00120       
00121       if (cfi1.getTrigger()==1){
00122       histTrack_bunchSignal_->Fill(cfi1.bunch());
00123       }
00124 
00125       
00126       int a = count1%4;
00127       if (a==3){
00128       outputFile<<" SimTrack "<<count1<<" has genpart index  "<<cfi1->genpartIndex()<<" vertex Index "<<cfi1->vertIndex() <<" bunchcr "<<cfi1.bunch()<<" trigger "<<cfi1.getTrigger()<<", from EncodedEventId: "<<cfi1->eventId().bunchCrossing() <<" "<<cfi1->eventId().event() <<std::endl;
00129       }
00130       count1++; 
00131     }
00132   }
00133 
00134   
00135   // test SimVertices
00136   //---------------------
00137   edm::Handle<CrossingFrame<SimVertex> > cf_simvtx;
00138   bool gotSimVertex = iEvent.getByLabel("mix","g4SimHits",cf_simvtx);
00139   if (!gotSimVertex) outputFile<<" Could not read Simvertices !!!!"<<std::endl;
00140   else {
00141     outputFile<<"\n=================== Starting SimVertex access ==================="<<std::endl;
00142     std::auto_ptr<MixCollection<SimVertex> > col2(new MixCollection<SimVertex>(cf_simvtx.product()));
00143     MixCollection<SimVertex>::iterator cfi2;
00144     int count2=0;
00145     outputFile <<" \nWe got "<<col2->sizeSignal()<<" signal vertices and "<<col2->sizePileup()<<" pileup vertices, total: "<<col2->size()<<std::endl;
00146     for (cfi2=col2->begin(); cfi2!=col2->end();cfi2++) {
00147       histVertex_bunch_->Fill(cfi2.bunch());
00148       int b = count2%4;
00149       if (count2 == 0 || b==3){
00150       //outputFile<<" SimVertex "<<count2<<" has parent index  "<<cfi2->parentIndex()<<" bunchcr "<<cfi2.bunch()<<" trigger "<<cfi2.getTrigger()<<", from EncodedEventId: "<<cfi2->eventId().bunchCrossing() <<" "<<cfi2->eventId().event() <<std::endl;
00151       }
00152       //SimVertex myvtx=(*cfi2);
00153       //outputFile<<"Same with op*: "<<count2<<" has parent index  "<<myvtx.parentIndex()<<" bunchcr "<<cfi2.bunch()<<" trigger "<<cfi2.getTrigger()<<", from EncodedEventId: "<<myvtx.eventId().bunchCrossing() <<" "<<myvtx.eventId().event() <<std::endl;
00154       count2++; 
00155     }
00156   }
00157   
00158   
00159   //test HepMCProducts
00160   //------------------------------------
00161   edm::Handle<CrossingFrame<edm::HepMCProduct> > cf_hepmc;
00162   bool gotHepMCP = iEvent.getByLabel("mix","generator",cf_hepmc);
00163   if (!gotHepMCP) std::cout<<" Could not read HepMCProducts!!!!"<<std::endl;
00164   else {
00165     outputFile<<"\n=================== Starting HepMCProduct access ==================="<<std::endl;
00166     std::auto_ptr<MixCollection<edm::HepMCProduct> > colhepmc(new MixCollection<edm::HepMCProduct>(cf_hepmc.product()));
00167     MixCollection<edm::HepMCProduct>::iterator cfihepmc;
00168     
00169     int count3=0;
00170     outputFile <<" \nWe got "<<colhepmc->sizeSignal()<<" signal hepmc products and "<<colhepmc->sizePileup()<<" pileup hepmcs, total: "<<colhepmc->size()<<std::endl;
00171     for (cfihepmc=colhepmc->begin(); cfihepmc!=colhepmc->end();cfihepmc++) {
00172       histHepMCProduct_bunch_->Fill(cfihepmc.bunch());
00173       int c = count3%4;
00174       if (count3==0 || c==3){
00175       //outputFile<<" edm::HepMCProduct "<<count3<<" 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;
00176       }
00177       HepMCProduct myprod=colhepmc->getObject(count3);
00178       //outputFile<<"same with getObject:hepmc product   "<<count3<<" has event number "<<myprod.GetEvent()->event_number()<<", "<<myprod.GetEvent()->particles_size()<<" particles and "<<myprod.GetEvent()->vertices_size()<<" vertices"<<std::endl;
00179       count3++;
00180     }
00181   }
00182 
00183 
00184   // test CaloHits
00185   //--------------------------  
00186   const std::string subdetcalo("g4SimHitsEcalHitsEB");
00187   edm::Handle<CrossingFrame<PCaloHit> > cf_calo;
00188   bool gotPCaloHit = iEvent.getByLabel("mix",subdetcalo,cf_calo);
00189   if (!gotPCaloHit) outputFile<<" Could not read CaloHits with label "<<subdetcalo<<"!!!!"<<std::endl;
00190   else {
00191     outputFile<<"\n\n=================== Starting CaloHit access, subdet "<<subdetcalo<<"  ==================="<<std::endl;
00192     std::auto_ptr<MixCollection<PCaloHit> > colcalo(new MixCollection<PCaloHit>(cf_calo.product()));
00193     //outputFile<<*(colcalo.get())<<std::endl;
00194     MixCollection<PCaloHit>::iterator cficalo;
00195     int count4=0;
00196     for (cficalo=colcalo->begin(); cficalo!=colcalo->end();cficalo++) {
00197       histPCaloHit_bunch_->Fill(cficalo.bunch());
00198       int d = count4%4;
00199       if (count4==0 || d==3){
00200       //outputFile<<" CaloHit "<<count4<<" has tof "<<cficalo->time()<<" trackid "<<cficalo->geantTrackId() <<" bunchcr "<<cficalo.bunch()<<" trigger "<<cficalo.getTrigger()<<", from EncodedEventId: "<<cficalo->eventId().bunchCrossing()<<" " <<cficalo->eventId().event() <<std::endl;
00201       }
00202       count4++;
00203     }
00204   }
00205 
00206   
00207   // test PSimHit for one particular subdet
00208   //---------------------------------------
00209   
00210   
00211   const std::string subdet("g4SimHitsTrackerHitsTECHighTof");
00212   edm::Handle<CrossingFrame<PSimHit> > cf_simhit;
00213   bool gotPSimHit = iEvent.getByLabel("mix",subdet,cf_simhit);
00214   if (!gotPSimHit) outputFile<<" Could not read SimHits with label "<<subdet<<"!!!!"<<std::endl;
00215   else {
00216     outputFile<<"\n\n=================== Starting SimHit access, subdet "<<subdet<<"  ==================="<<std::endl;
00217 
00218     std::auto_ptr<MixCollection<PSimHit> > col(new MixCollection<PSimHit>(cf_simhit.product()));
00219     //outputFile<<*(col.get())<<std::endl;
00220     MixCollection<PSimHit>::iterator cfi;
00221     int count5=0;
00222     for (cfi=col->begin(); cfi!=col->end();cfi++) {
00223       
00224       // Signal
00225       if (cfi.getTrigger()==1){ 
00226         histPSimHit_bunchSignal_TrackerHitsTECHighTof_->Fill(cfi.bunch());
00227         tofhist_sig_->Fill(cfi->timeOfFlight());
00228       }
00229       
00230       // Pileups
00231       if (cfi.getTrigger()==0){
00232        histPSimHit_bunchPileups_TrackerHitsTECHighTof_->Fill(cfi.bunch());
00233        std::cout << " cfi->timeOfFlight() = " << cfi->timeOfFlight() << std::endl;
00234        tofhist_->Fill(cfi->timeOfFlight()); 
00235       }
00236       
00237       int e = count5%4;
00238       if (e==3){
00239       outputFile<<" Hit "<<count5<<" 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;
00240       }
00241       count5++;
00242     }
00243   }
00244   
00245   const std::string subdet1("g4SimHitsMuonCSCHits");
00246   edm::Handle<CrossingFrame<PSimHit> > cf_simhit1;
00247   bool gotPSimHit1 = iEvent.getByLabel("mix",subdet1,cf_simhit1);
00248   if (!gotPSimHit1) outputFile<<" Could not read SimHits with label "<<subdet1<<"!!!!"<<std::endl;
00249   else {
00250     outputFile<<"\n\n=================== Starting SimHit access, subdet "<<subdet1<<"  ==================="<<std::endl;
00251 
00252     std::auto_ptr<MixCollection<PSimHit> > col(new MixCollection<PSimHit>(cf_simhit1.product()));
00253     //outputFile<<*(col.get())<<std::endl;
00254     MixCollection<PSimHit>::iterator cfi;
00255     int count5=0;
00256     for (cfi=col->begin(); cfi!=col->end();cfi++) {
00257       
00258       if (cfi.getTrigger()==1) histPSimHit_bunchSignal_MuonCSCHits_->Fill(cfi.bunch());
00259       
00260       if (cfi.getTrigger()==0) histPSimHit_bunchPileups_MuonCSCHits_->Fill(cfi.bunch());
00261       
00262       int e = count5%4;
00263       if (e==3){
00264       outputFile<<" Hit "<<count5<<" 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;
00265       }
00266       count5++;
00267     }
00268   }
00269   
00270 
00271 
00272 }
00273 
00274 
00275 // ------------ method called once each job just before starting event loop  ------------
00276 void 
00277 TestMixedSource::beginJob()
00278 { 
00279    outputFile.open("test.log");
00280    if (!outputFile.is_open())
00281    {
00282      std::cout << "Unable to open file!" << std::endl;
00283    } 
00284    
00285 }
00286 
00287 // ------------ method called once each job just after ending the event loop  ------------
00288 void 
00289 TestMixedSource::endJob() {
00290   
00291   outputFile.close();
00292   
00293   char t[30];
00294   sprintf(t,"%s",fileName_.c_str());
00295   std::cout << " fileName = " << t << std::endl; 
00296   histFile_=new TFile(t,"RECREATE");
00297   histTrack_bunchPileups_->Write();
00298   histTrack_bunchSignal_->Write();
00299   histVertex_bunch_->Write();
00300   histPCaloHit_bunch_->Write();
00301   histPSimHit_bunchPileups_TrackerHitsTECHighTof_->Write();
00302   histPSimHit_bunchSignal_TrackerHitsTECHighTof_->Write();
00303   tofhist_sig_->Write();
00304   tofhist_->Write();
00305   histPSimHit_bunchSignal_MuonCSCHits_->Write();
00306   histPSimHit_bunchPileups_MuonCSCHits_->Write();
00307   histHepMCProduct_bunch_->Write();
00308   histFile_->Write();
00309   histFile_->Close();
00310 
00311 }
00312 }//edm