CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Validation/RecoVertex/src/MCVerticesWeight.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    PileUp
00004 // Class:      MCVerticesWeight
00005 // 
00013 //
00014 // Original Author:  Andrea Venturi
00015 //         Created:  Tue Oct 21 20:55:22 CEST 2008
00016 //
00017 //
00018 
00019 
00020 // system include files
00021 #include <memory>
00022 #include <string>
00023 
00024 // user include files
00025 #include "FWCore/Framework/interface/Frameworkfwd.h"
00026 #include "FWCore/Framework/interface/EDFilter.h"
00027 
00028 #include "FWCore/Framework/interface/Event.h"
00029 #include "FWCore/Framework/interface/MakerMacros.h"
00030 #include "FWCore/Framework/interface/ESHandle.h"
00031 #include "FWCore/Framework/interface/ESWatcher.h"
00032 
00033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00034 
00035 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00036 
00037 #include "FWCore/Utilities/interface/InputTag.h"
00038 
00039 #include "SimDataFormats/PileupSummaryInfo/interface/PileupSummaryInfo.h"
00040 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00041 #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
00042 
00043 #include "Validation/RecoVertex/interface/VertexWeighter.h"
00044 
00045 //
00046 // class declaration
00047 //
00048 
00049 class MCVerticesWeight : public edm::EDFilter {
00050    public:
00051       explicit MCVerticesWeight(const edm::ParameterSet&);
00052       ~MCVerticesWeight();
00053 
00054    private:
00055       virtual void beginJob() ;
00056       virtual bool filter(edm::Event&, const edm::EventSetup&);
00057       virtual void endJob() ;
00058       
00059       // ----------member data ---------------------------
00060 
00061   edm::InputTag m_pileupcollection;
00062   edm::InputTag m_mctruthcollection;
00063   const VertexWeighter m_weighter;
00064 
00065 };
00066 
00067 //
00068 // constants, enums and typedefs
00069 //
00070 
00071 //
00072 // static data member definitions
00073 //
00074 
00075 //
00076 // constructors and destructor
00077 //
00078 MCVerticesWeight::MCVerticesWeight(const edm::ParameterSet& iConfig):
00079   m_pileupcollection(iConfig.getParameter<edm::InputTag>("pileupSummaryCollection")),
00080   m_mctruthcollection(iConfig.getParameter<edm::InputTag>("mcTruthCollection")),
00081   m_weighter(iConfig.getParameter<edm::ParameterSet>("weighterConfig"))
00082 {
00083 
00084   produces<double>();
00085 
00086 }
00087 
00088 MCVerticesWeight::~MCVerticesWeight()
00089 {
00090  
00091    // do anything here that needs to be done at desctruction time
00092    // (e.g. close files, deallocate resources etc.)
00093 
00094 }
00095 
00096 
00097 //
00098 // member functions
00099 //
00100 
00101 // ------------ method called on each new Event  ------------
00102 bool
00103 MCVerticesWeight::filter(edm::Event& iEvent, const edm::EventSetup& iSetup)
00104 {
00105   using namespace edm;
00106   
00107   bool selected = true;
00108   
00109   double computed_weight(1);
00110 
00111   Handle<std::vector<PileupSummaryInfo> > pileupinfos;
00112    iEvent.getByLabel(m_pileupcollection,pileupinfos);
00113 
00114 
00115   // look for the intime PileupSummaryInfo
00116 
00117    std::vector<PileupSummaryInfo>::const_iterator pileupinfo;
00118    for(pileupinfo = pileupinfos->begin(); pileupinfo != pileupinfos->end() ; ++pileupinfo) {
00119      if(pileupinfo->getBunchCrossing()==0) break;
00120    } 
00121    
00122    //
00123    if(pileupinfo->getBunchCrossing()!=0) {
00124      edm::LogError("NoInTimePileUpInfo") << "Cannot find the in-time pileup info " << pileupinfo->getBunchCrossing();
00125    }
00126    else {
00127      
00128      //     pileupinfo->getPU_NumInteractions();
00129      
00130      const std::vector<float>& zpositions = pileupinfo->getPU_zpositions();
00131      
00132      //     for(std::vector<float>::const_iterator zpos = zpositions.begin() ; zpos != zpositions.end() ; ++zpos) {
00133        
00134      //     }
00135      
00136      // main interaction part
00137      
00138      Handle< HepMCProduct > EvtHandle ;
00139      iEvent.getByLabel(m_mctruthcollection, EvtHandle ) ;
00140      
00141      const HepMC::GenEvent* Evt = EvtHandle->GetEvent();
00142      
00143      // get the first vertex
00144      
00145      double zmain = 0.0;
00146      if(Evt->vertices_begin() != Evt->vertices_end()) {
00147        zmain = (*Evt->vertices_begin())->point3d().z()/10.;
00148      }
00149      
00150      //
00151     
00152      
00153      computed_weight = m_weighter.weight(zpositions,zmain);
00154      
00155    }
00156    
00157    std::auto_ptr<double> weight(new double(computed_weight));
00158    
00159    iEvent.put(weight);
00160    
00161    //
00162    
00163   return selected;
00164 }
00165 
00166 // ------------ method called once each job just before starting event loop  ------------
00167 void 
00168 MCVerticesWeight::beginJob()
00169 {
00170 }
00171 
00172 // ------------ method called once each job just after ending the event loop  ------------
00173 void 
00174 MCVerticesWeight::endJob() {
00175 }
00176 
00177 //define this as a plug-in
00178 DEFINE_FWK_MODULE(MCVerticesWeight);