CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/SimGeneral/MixingModule/plugins/CFWriter.cc

Go to the documentation of this file.
00001 // File: CFWriter.cc
00002 // Description:  see CFWriter.h
00003 // Author:  Emilia Becheva, LLR Palaiseau
00004 //
00005 //--------------------------------------------
00006 
00007 #include "SimGeneral/MixingModule/plugins/CFWriter.h"
00008 
00009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "FWCore/Utilities/interface/EDMException.h"
00012 #include "FWCore/Utilities/interface/Algorithms.h"
00013 #include "FWCore/Utilities/interface/TypeID.h"
00014 #include "FWCore/ServiceRegistry/interface/Service.h"
00015 #include "DataFormats/Common/interface/Handle.h"
00016 #include "DataFormats/Provenance/interface/Provenance.h"
00017 #include "DataFormats/Provenance/interface/BranchDescription.h"
00018 #include "FWCore/Framework/interface/TriggerNamesService.h"
00019 
00020 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
00021 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
00022 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00023 #include "SimDataFormats/CrossingFrame/interface/PCrossingFrame.h"
00024 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00025 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
00026 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00027 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00028 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00029 
00030 #include "FWCore/Framework/interface/Event.h"
00031 #include "FWCore/Framework/interface/EventSetup.h"
00032 
00033 #include "FWCore/Services/src/Memory.h"
00034 
00035 
00036 namespace edm 
00037 {
00038 CFWriter::CFWriter(const edm::ParameterSet& iConfig)
00039 : flagSimTrack_(false), 
00040   flagSimVertex_(false), 
00041   flagHepMCProduct_(false),  
00042   flagPCaloHit_(false),
00043   flagPSimHit_(false)
00044 { 
00045   
00046   //register your products
00047   ParameterSet ps=iConfig.getParameter<ParameterSet>("mixObjects");
00048   std::vector<std::string> names = ps.getParameterNames();
00049   for (std::vector<std::string>::iterator it=names.begin();it!= names.end();++it)
00050   {
00051     ParameterSet pset=ps.getParameter<ParameterSet>((*it));
00052     if (!pset.exists("type"))  continue; //to allow replacement by empty pset
00053 
00054     std::string object = pset.getParameter<std::string>("type");
00055     std::vector<InputTag>  tags=pset.getParameter<std::vector<InputTag> >("input");
00056     
00057     //SimTracks
00058     if (object=="SimTrack") {
00059       flagSimTrack_ = true;
00060       
00061       InputTag tag;
00062       if (tags.size()>0) tag=tags[0];
00063       std::string label;      
00064       branchesActivate(TypeID(typeid(std::vector<SimTrack>)).friendlyClassName(),std::string(""),tag,label);
00065       produces<PCrossingFrame<SimTrack> >(label);
00066       LogInfo("MixingModule") <<"Add PCrossingFrame<SimTrack> "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00067       
00068      } 
00069     //SimVertices
00070     else if (object=="SimVertex") {
00071       flagSimVertex_ = true;
00072       
00073       InputTag tag;
00074       if (tags.size()>0) tag=tags[0];
00075       std::string label;             
00076       branchesActivate(TypeID(typeid(std::vector<SimVertex>)).friendlyClassName(),std::string(""),tag,label);
00077       produces<PCrossingFrame<SimVertex> >(label);
00078       LogInfo("MixingModule") <<"Add SimVertexContainer "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00079      
00080     }  
00081     // PCaloHit
00082     else if (object=="PCaloHit"){
00083       flagPCaloHit_ = true; 
00084               
00085       std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00086       for (unsigned int ii=0;ii<subdets.size();ii++) {
00087         InputTag tag;
00088         if (tags.size()==1) tag=tags[0];
00089         else if(tags.size()>1) tag=tags[ii];
00090         std::string label;
00091 
00092         branchesActivate(TypeID(typeid(std::vector<PCaloHit>)).friendlyClassName(),subdets[ii],tag,label);
00093         produces<PCrossingFrame<PCaloHit> >(label);
00094         LogInfo("MixingModule") <<"Add PCrossingFrame<PCaloHit> "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00095          
00096         // fill table with labels
00097         labCaloHit.push_back(label);
00098       }       
00099     }
00100     
00101     // PSimHit
00102     else if (object=="PSimHit"){
00103       flagPSimHit_ = true;
00104       
00105       std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00106       for (unsigned int ii=0;ii<subdets.size();ii++) {
00107         InputTag tag;
00108         if (tags.size()==1) tag=tags[0];
00109         
00110         else if(tags.size()>1) tag=tags[ii];
00111         std::string label;
00112         
00113         branchesActivate(TypeID(typeid(std::vector<PSimHit>)).friendlyClassName(),subdets[ii],tag,label);
00114         produces<PCrossingFrame<PSimHit> >(label);
00115         LogInfo("MixingModule") <<"Add PSimHitContainer "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00116         
00117         // fill table with labels
00118         labSimHit.push_back(label);
00119       } //end for  
00120     }
00121     
00122     // HepMCProduct
00123     else if (object=="HepMCProduct"){
00124       flagHepMCProduct_ = true;      
00125       
00126       InputTag tag;
00127       if (tags.size()>0) tag=tags[0];
00128       std::string label;
00129 
00130       branchesActivate(TypeID(typeid(HepMCProduct)).friendlyClassName(),std::string(""),tag,label);
00131       produces<PCrossingFrame<edm::HepMCProduct> >(label);
00132       LogInfo("MixingModule") <<"Add HepMCProduct "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;      
00133     }  
00134     else LogWarning("MixingModule") <<"You did not mix a type of object("<<object<<").";
00135  
00136   }//end for 
00137 }
00138 
00139 
00140 void CFWriter::beginRun(const edm::Run & run,edm::EventSetup const& setup){
00141 }
00142 
00143 
00144 
00145 CFWriter::~CFWriter(){}
00146 
00147 
00148 void CFWriter::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00149 { 
00150     
00151   if (flagSimTrack_){
00152     bool gotTracks;  
00153     edm::Handle<CrossingFrame<SimTrack> > cf_simtrack;
00154     gotTracks = iEvent.getByLabel("mix","g4SimHits",cf_simtrack);
00155     
00156     if (gotTracks){ 
00157        PCrossingFrame<SimTrack> * PCFbis = new PCrossingFrame<SimTrack>(*cf_simtrack.product());
00158        std::auto_ptr<PCrossingFrame<SimTrack> > pOutTrack(PCFbis);    
00159        iEvent.put(pOutTrack,"g4SimHits");
00160     }
00161     else{
00162        LogInfo("MixingModule") << " Please, check if the object <SimTrack> has been mixed by the MixingModule!";  
00163     }
00164   }//end if flagSimTrack_
00165   
00166     //SimVertex
00167   if (flagSimVertex_){
00168     bool gotSimVertex;
00169     edm::Handle<CrossingFrame<SimVertex> > cf_simvtx;
00170     gotSimVertex=iEvent.getByLabel("mix","g4SimHits",cf_simvtx);
00171     
00172     if (gotSimVertex){ 
00173        PCrossingFrame<SimVertex> * PCFvtx = new PCrossingFrame<SimVertex>(*cf_simvtx.product());
00174        std::auto_ptr<PCrossingFrame<SimVertex> > pOutVertex(PCFvtx);    
00175        iEvent.put(pOutVertex,"g4SimHits");
00176     }
00177     else{
00178        LogInfo("MixingModule") << " Please, check if the object <SimVertex> has been mixed by the MixingModule!";
00179     }
00180   }
00181    
00182   // PCaloHit
00183   if (flagPCaloHit_){  
00184   
00185     for (unsigned int ii=0;ii<labCaloHit.size();ii++){
00186       bool gotPCaloHit;
00187       edm::Handle<CrossingFrame<PCaloHit> > cf_calohit;
00188       gotPCaloHit=iEvent.getByLabel("mix",labCaloHit[ii],cf_calohit);
00189       
00190       if (gotPCaloHit){   
00191         PCrossingFrame<PCaloHit> * PCFPhCaloHit = new PCrossingFrame<PCaloHit>(*cf_calohit.product()); 
00192         std::auto_ptr<PCrossingFrame<PCaloHit> > pOutHCalo(PCFPhCaloHit);
00193         iEvent.put(pOutHCalo,labCaloHit[ii]); 
00194       }
00195       else{
00196         LogInfo("MixingModule") << " Please, check if the object <PCaloHit> " << labCaloHit[ii] << " has been mixed by the MixingModule!";
00197       }
00198     }
00199   
00200   }
00201   
00202    if (flagPSimHit_){
00203 
00204     for (unsigned int ii=0;ii<labSimHit.size();ii++) {
00205       bool gotPSimHit;
00206       edm::Handle<CrossingFrame<PSimHit> > cf_simhit;
00207       gotPSimHit=iEvent.getByLabel("mix",labSimHit[ii],cf_simhit);
00208       
00209       if (gotPSimHit){ 
00210         PCrossingFrame<PSimHit> * PCFSimHit = new PCrossingFrame<PSimHit>(*cf_simhit.product());
00211         std::auto_ptr<PCrossingFrame<PSimHit> > pOutSimHit(PCFSimHit);
00212         iEvent.put(pOutSimHit,labSimHit[ii]); 
00213       }
00214       else{           
00215         LogInfo("MixingModule") << " Please, check if the object <PSimHit> " << labSimHit[ii] << " has been mixed by the MixingModule!";
00216       }
00217                       
00218     }    
00219   }
00220   
00221   
00222   //HepMCProduct 
00223   if (flagHepMCProduct_){
00224     bool gotHepMCProduct;
00225     edm::Handle<CrossingFrame<edm::HepMCProduct> > cf_hepmc;
00226     gotHepMCProduct=iEvent.getByLabel("mix","generator",cf_hepmc);
00227     if (gotHepMCProduct){ 
00228        PCrossingFrame<edm::HepMCProduct> * PCFHepMC = new PCrossingFrame<edm::HepMCProduct>(*cf_hepmc.product());
00229        std::auto_ptr<PCrossingFrame<edm::HepMCProduct> > pOuthepmcpr(PCFHepMC);
00230        iEvent.put(pOuthepmcpr,"generator");
00231     }
00232     else{
00233        LogInfo("MixingModule") << " Please, check if the object <HepMCProduct> has been mixed by the MixingModule!";
00234     }
00235 
00236   }// end if flagHepMCProduct_
00237             
00238 }
00239 
00240 
00241 void CFWriter::branchesActivate(const std::string &friendlyName, std::string subdet, InputTag &tag,std::string &label) {
00242        
00243   label=tag.label()+tag.instance();
00244   wantedBranches_.push_back(friendlyName + '_' +
00245                               tag.label() + '_' +
00246                               tag.instance());
00247                                           
00248   //if useCurrentProcessOnly, we have to change the input tag
00249   if (useCurrentProcessOnly_) {
00250     const std::string processName = edm::Service<edm::service::TriggerNamesService>()->getProcessName();
00251     tag = InputTag(tag.label(),tag.instance(),processName);
00252   }    
00253     
00254 }
00255 
00256 }//edm