CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/FWCore/Skeletons/scripts/mkTemplates/EDProducer/edproducer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    prodname
00004 // Class:      prodname
00005 // 
00013 //
00014 // Original Author:  John Doe
00015 //         Created:  day-mon-xx
00016 // RCS(Id)
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/EDProducer.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 @example_myparticle #include "DataFormats/MuonReco/interface/Muon.h"
00034 @example_myparticle #include "DataFormats/EgammaCandidates/interface/PixelMatchGsfElectron.h"
00035 @example_myparticle #include "DataFormats/Candidate/interface/Particle.h"
00036 @example_myparticle #include "FWCore/MessageLogger/interface/MessageLogger.h"
00037 @example_myparticle #include "FWCore/Utilities/interface/InputTag.h"
00038 
00039 //
00040 // class declaration
00041 //
00042 
00043 class prodname : public edm::EDProducer {
00044    public:
00045       explicit prodname(const edm::ParameterSet&);
00046       ~prodname();
00047 
00048       static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
00049 
00050    private:
00051       virtual void beginJob() ;
00052       virtual void produce(edm::Event&, const edm::EventSetup&);
00053       virtual void endJob() ;
00054       
00055       virtual void beginRun(edm::Run&, edm::EventSetup const&);
00056       virtual void endRun(edm::Run&, edm::EventSetup const&);
00057       virtual void beginLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&);
00058       virtual void endLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&);
00059 
00060       // ----------member data ---------------------------
00061 @example_myparticle       edm::InputTag muonTags_; 
00062 @example_myparticle       edm::InputTag electronTags_;
00063 };
00064 
00065 //
00066 // constants, enums and typedefs
00067 //
00068 
00069 @example_myparticle       // define container that will be booked into event
00070 @example_myparticle       typedef std::vector<reco::Particle> MyParticleCollection;
00071 
00072 //
00073 // static data member definitions
00074 //
00075 
00076 //
00077 // constructors and destructor
00078 //
00079 prodname::prodname(const edm::ParameterSet& iConfig)
00080 @example_myparticle :
00081 @example_myparticle   muonTags_( iConfig.getParameter<edm::InputTag>( "muons" )),
00082 @example_myparticle   electronTags_( iConfig.getParameter<edm::InputTag>( "electrons" ))
00083 {
00084    //register your products
00085 /* Examples
00086    produces<ExampleData2>();
00087 
00088    //if do put with a label
00089    produces<ExampleData2>("label");
00090  
00091    //if you want to put into the Run
00092    produces<ExampleData2,InRun>();
00093 */
00094 @example_myparticle   produces<MyParticleCollection>( "particles" );
00095    //now do what ever other initialization is needed
00096   
00097 }
00098 
00099 
00100 prodname::~prodname()
00101 {
00102  
00103    // do anything here that needs to be done at desctruction time
00104    // (e.g. close files, deallocate resources etc.)
00105 
00106 }
00107 
00108 
00109 //
00110 // member functions
00111 //
00112 
00113 // ------------ method called to produce the data  ------------
00114 void
00115 prodname::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00116 {
00117    using namespace edm;
00118 @example_myparticle    using namespace reco;
00119 @example_myparticle    using namespace std;
00120 /* This is an event example
00121    //Read 'ExampleData' from the Event
00122    Handle<ExampleData> pIn;
00123    iEvent.getByLabel("example",pIn);
00124 
00125    //Use the ExampleData to create an ExampleData2 which 
00126    // is put into the Event
00127    std::auto_ptr<ExampleData2> pOut(new ExampleData2(*pIn));
00128    iEvent.put(pOut);
00129 */
00130 
00131 /* this is an EventSetup example
00132    //Read SetupData from the SetupRecord in the EventSetup
00133    ESHandle<SetupData> pSetup;
00134    iSetup.get<SetupRecord>().get(pSetup);
00135 */
00136  
00137 @example_myparticle    Handle<MuonCollection> muons;
00138 @example_myparticle    iEvent.getByLabel( muonTags_, muons );
00139 @example_myparticle    
00140 @example_myparticle    Handle<PixelMatchGsfElectronCollection> electrons;
00141 @example_myparticle    iEvent.getByLabel( electronTags_, electrons );
00142 @example_myparticle    
00143 @example_myparticle    // create a new collection of Particle objects
00144 @example_myparticle    auto_ptr<MyParticleCollection> newParticles( new MyParticleCollection );      
00145 @example_myparticle 
00146 @example_myparticle    // if the number of electrons or muons is 4 (or 2 and 2), costruct a new particle
00147 @example_myparticle    if( muons->size() == 4 || electrons->size() == 4 || ( muons->size() == 2 && electrons->size() == 2 ) ) {
00148 @example_myparticle       
00149 @example_myparticle       // sums of momenta and charges will be calculated
00150 @example_myparticle       Particle::LorentzVector totalP4( 0, 0, 0, 0 );
00151 @example_myparticle       Particle::Charge charge( 0 );
00152 @example_myparticle       
00153 @example_myparticle       // loop over muons, sum over p4s and charges. Later same for electrons
00154 @example_myparticle       for( MuonCollection::const_iterator muon = muons->begin(); muon != muons->end(); ++muon ) {
00155 @example_myparticle          totalP4 += muon->p4();
00156 @example_myparticle          charge += muon->charge();
00157 @example_myparticle       }
00158 @example_myparticle       
00159 @example_myparticle       for( PixelMatchGsfElectronCollection::const_iterator electron = electrons->begin(); electron != electrons->end(); ++electron ) {
00160 @example_myparticle          totalP4 += electron->p4(); 
00161 @example_myparticle          charge += electron->charge(); 
00162 @example_myparticle       }
00163 @example_myparticle       
00164 @example_myparticle       // create a particle with momentum and charge from muons and electrons
00165 @example_myparticle       Particle h;
00166 @example_myparticle       h.setP4(totalP4);
00167 @example_myparticle       h.setCharge(charge);
00168 @example_myparticle 
00169 @example_myparticle       // fill the particles into the vector
00170 @example_myparticle       newParticles->push_back( h );      
00171 @example_myparticle    }
00172 @example_myparticle    
00173 @example_myparticle    // save the vector
00174 @example_myparticle    iEvent.put( newParticles, "particles" );
00175 }
00176 
00177 // ------------ method called once each job just before starting event loop  ------------
00178 void 
00179 prodname::beginJob()
00180 {
00181 }
00182 
00183 // ------------ method called once each job just after ending the event loop  ------------
00184 void 
00185 prodname::endJob() {
00186 }
00187 
00188 // ------------ method called when starting to processes a run  ------------
00189 void 
00190 prodname::beginRun(edm::Run&, edm::EventSetup const&)
00191 {
00192 }
00193 
00194 // ------------ method called when ending the processing of a run  ------------
00195 void 
00196 prodname::endRun(edm::Run&, edm::EventSetup const&)
00197 {
00198 }
00199 
00200 // ------------ method called when starting to processes a luminosity block  ------------
00201 void 
00202 prodname::beginLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&)
00203 {
00204 }
00205 
00206 // ------------ method called when ending the processing of a luminosity block  ------------
00207 void 
00208 prodname::endLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&)
00209 {
00210 }
00211 
00212 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
00213 void
00214 prodname::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
00215   //The following says we do not know what parameters are allowed so do no validation
00216   // Please change this to state exactly what you do use, even if it is no parameters
00217   edm::ParameterSetDescription desc;
00218   desc.setUnknown();
00219   descriptions.addDefault(desc);
00220 @example_myparticle  
00221 @example_myparticle  //Specify that only 'muons' and 'electrons' are allowed
00222 @example_myparticle  //To use, remove the default given above and uncomment below
00223 @example_myparticle  //ParameterSetDescription desc;
00224 @example_myparticle  //desc.add<edm::InputTag>("muons","muons");
00225 @example_myparticle  //desc.add<edm::InputTag>("electrons","pixelMatchGsfElectrons");
00226 @example_myparticle  //descriptions.addDefault(desc);
00227 }
00228 
00229 //define this as a plug-in
00230 DEFINE_FWK_MODULE(prodname);