CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/SimGeneral/MixingModule/plugins/HiMixingModule.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    HiMixingModule
00004 // Class:      HiMixingModule
00005 // 
00013 //
00014 // Original Author:  Yetkin Yilmaz
00015 //         Created:  Tue Feb 17 17:32:06 EST 2009
00016 // $Id: HiMixingModule.cc,v 1.7 2010/02/25 00:34:23 wmtan Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <vector>
00023 #include <memory>
00024 #include <iostream>
00025 
00026 // user include files
00027 #include "FWCore/Framework/interface/Frameworkfwd.h"
00028 #include "FWCore/Framework/interface/EDProducer.h"
00029 
00030 #include "FWCore/Framework/interface/Event.h"
00031 #include "FWCore/Framework/interface/MakerMacros.h"
00032 
00033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00034 #include "FWCore/Framework/interface/Event.h"
00035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00036 #include "FWCore/Utilities/interface/InputTag.h"
00037 #include "FWCore/Framework/interface/Selector.h"
00038 
00039 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00040 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00041 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
00042 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00043 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00044 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00045 
00046 #include "DataFormats/Provenance/interface/ProductID.h"
00047 #include "DataFormats/Common/interface/Handle.h"
00048 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00049 #include "FWCore/Utilities/interface/EDMException.h"
00050 #include "FWCore/Utilities/interface/Algorithms.h"
00051 #include "FWCore/Framework/interface/ConstProductRegistry.h"
00052 #include "FWCore/ServiceRegistry/interface/Service.h"
00053 #include "DataFormats/Common/interface/Handle.h"
00054 #include "DataFormats/Provenance/interface/Provenance.h"
00055 #include "DataFormats/Provenance/interface/BranchDescription.h"
00056 #include "FWCore/Services/src/Memory.h"
00057 
00058 #include <string>
00059 
00060 using namespace std;
00061 
00062 //
00063 // class decleration
00064 //
00065 
00066 namespace edm{
00067 
00068    class HiMixingModule;
00069 
00070    class HiMixingWorkerBase{
00071    public:
00072       explicit HiMixingWorkerBase(){;}
00073       
00074       HiMixingWorkerBase(std::string& object, std::vector<InputTag>& tags, std::string& label) :
00075       object_(object),
00076       tags_(tags),
00077       label_(label)
00078       {;}
00079       virtual ~HiMixingWorkerBase(){;}
00080       //   virtual void put(edm::Event &e) = 0;
00081       virtual void addSignals(edm::Event &e) = 0;
00082 
00083       std::string object_;
00084       std::vector<InputTag> tags_;
00085       std::string label_;
00086    };
00087 
00088    
00089    template <class T>
00090    class HiMixingWorker : public HiMixingWorkerBase {
00091    public:
00092       HiMixingWorker(std::string& object, std::vector<InputTag>& tags, std::string& label) : HiMixingWorkerBase(object,tags, label) {;}
00093       ~HiMixingWorker(){;}
00094       void addSignals(edm::Event &e){
00095          std::vector<Handle<std::vector<T> > > handles;
00096          bool get = true;
00097          for(size_t itag = 0; itag < tags_.size(); ++itag){
00098             std::cout<<"itag "<<itag<<std::endl;
00099             std::cout<<"label "<<tags_[itag].label()<<std::endl;
00100             std::cout<<"instance "<<tags_[itag].instance()<<std::endl;
00101             Handle<std::vector<T> > hand;
00102             handles.push_back(hand);
00103             get = get && e.getByLabel(tags_[itag],handles[itag]);
00104             if(!get)  LogError("Product inconsistency")<<"One of the sub-events is missing the product with type "
00105                                                        <<object_
00106                                                        <<", instance "
00107                                                        <<tags_[itag].instance()
00108                                                        <<" whereas the other one is fine.";
00109          }
00110          
00111          if(get){
00112             std::auto_ptr<CrossingFrame<T> > crFrame(new CrossingFrame<T>() );      
00113             crFrame->addSignals(handles[0].product(),e.id());
00114             for(size_t itag = 1; itag < tags_.size(); ++itag){
00115                crFrame->addPileups(0,const_cast< std::vector<T> * >(handles[itag].product()),itag);      
00116             }
00117             e.put(crFrame,label_);
00118          }
00119       }
00120    };
00121    
00122 template <>
00123 void HiMixingWorker<HepMCProduct>::addSignals(edm::Event &e){
00124 
00125    std::vector<Handle<HepMCProduct> > handles;
00126    bool get = true;
00127    for(size_t itag = 0; itag< tags_.size(); ++itag){
00128       Handle<HepMCProduct> hand;
00129       handles.push_back(hand);
00130       get = get && e.getByLabel(tags_[itag],handles[itag]);
00131       if(!get)  LogError("Product inconsistency")<<"One of the sub-events is missing the product with type "
00132                                                  <<object_
00133                                                  <<", instance "
00134                                                  <<tags_[itag].instance()
00135                                                  <<" whereas the other one is fine.";
00136    }
00137    
00138    if(get){
00139       std::auto_ptr<CrossingFrame<HepMCProduct> > crFrame(new CrossingFrame<HepMCProduct>() );
00140       crFrame->addSignals(handles[0].product(),e.id());
00141       for(size_t itag = 1; itag < tags_.size(); ++itag){
00142          crFrame->addPileups(0, const_cast<HepMCProduct *>(handles[itag].product()),itag);
00143       }
00144       e.put(crFrame,label_);
00145    }
00146 }
00147 
00148 class HiMixingModule : public edm::EDProducer {
00149    public:
00150       explicit HiMixingModule(const edm::ParameterSet&);
00151       ~HiMixingModule();
00152 
00153    private:
00154   virtual void beginJob() ;
00155       virtual void produce(edm::Event&, const edm::EventSetup&);
00156       virtual void endJob() ;
00157       bool verifyRegistry(std::string object, std::string subdet, InputTag &tag,std::string &label);      
00158       // ----------member data ---------------------------
00159 
00160    std::vector<HiMixingWorkerBase *> workers_;
00161 
00162 };
00163 
00164 //
00165 // constants, enums and typedefs
00166 //
00167 
00168 
00169 //
00170 // static data member definitions
00171 //
00172 
00173 //
00174 // constructors and destructor
00175 //
00176 HiMixingModule::HiMixingModule(const edm::ParameterSet& pset)
00177 {
00178 
00179    ParameterSet ps=pset.getParameter<ParameterSet>("mixObjects");
00180    std::vector<std::string> names = ps.getParameterNames();
00181    std::vector<std::string> simtags = pset.getParameter<std::vector<std::string> >("srcSIM");
00182    std::vector<std::string> gentags = pset.getParameter<std::vector<std::string> >("srcGEN");
00183 
00184    if(simtags.size() != gentags.size()) LogError("MixingInput")<<"Generator and Simulation input lists are not matching each other"<<endl;
00185    
00186    for (std::vector<string>::iterator it=names.begin();it!= names.end();++it){
00187       
00188       ParameterSet pstag=ps.getParameter<ParameterSet>((*it));
00189       if (!pstag.exists("type"))  continue; //to allow replacement by empty pset
00190       std::string object = pstag.getParameter<std::string>("type");
00191       std::vector<InputTag>  tags=pstag.getParameter<std::vector<InputTag> >("input");
00192 
00193       std::string signal;
00194       for(size_t itag = 0; itag < tags.size(); ++itag){
00195          InputTag tag=tags[itag];
00196          std::vector<InputTag> inputs;
00197 
00198          for(size_t input = 0; input < simtags.size(); ++input){
00199             if (object=="HepMCProduct") signal = gentags[input];
00200             else signal = simtags[input];
00201             inputs.push_back(InputTag(signal,tag.instance()));
00202          }
00203 
00204          std::string label=tag.label()+tag.instance();
00205          //      verifyRegistry(object,std::string(""),tag,label);
00206             if (object=="HepMCProduct"){
00207                workers_.push_back(new HiMixingWorker<HepMCProduct>(object,inputs,label));
00208                produces<CrossingFrame<HepMCProduct> >(label);
00209             }else if (object=="SimTrack"){
00210                workers_.push_back(new HiMixingWorker<SimTrack>(object,inputs,label));
00211                produces<CrossingFrame<SimTrack> >(label);
00212             }else if (object=="SimVertex"){
00213                workers_.push_back(new HiMixingWorker<SimVertex>(object,inputs,label));
00214                produces<CrossingFrame<SimVertex> >(label);
00215             }else if (object=="PSimHit"){
00216                workers_.push_back(new HiMixingWorker<PSimHit>(object,inputs,label));
00217                produces<CrossingFrame<PSimHit> >(label);
00218             }else if (object=="PCaloHit"){
00219                workers_.push_back(new HiMixingWorker<PCaloHit>(object,inputs,label));
00220                produces<CrossingFrame<PCaloHit> >(label);
00221             }else LogInfo("Error")<<"What the hell is this object?!";
00222             
00223             LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;         
00224             cout<<"The COUT : "<<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label<<endl;        
00225       }
00226    }  
00227 }
00228    
00229 
00230 HiMixingModule::~HiMixingModule()
00231 {
00232  
00233    // do anything here that needs to be done at desctruction time
00234    // (e.g. close files, deallocate resources etc.)
00235 
00236 }
00237 
00238 
00239 //
00240 // member functions
00241 //
00242 
00243 // ------------ method called to produce the data  ------------
00244 void
00245 HiMixingModule::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00246 {
00247    using namespace edm;
00248 
00249    for(size_t i = 0; i < workers_.size(); ++i){
00250       (workers_[i])->addSignals(iEvent);
00251    }
00252 }
00253 
00254 // ------------ method called once each job just before starting event loop  ------------
00255 void 
00256 HiMixingModule::beginJob()
00257 {
00258 }
00259 
00260 // ------------ method called once each job just after ending the event loop  ------------
00261 void 
00262 HiMixingModule::endJob() {
00263 }
00264 
00265 bool HiMixingModule::verifyRegistry(std::string object, std::string subdet, InputTag &tag,std::string &label) {
00266    // verify that the given product exists in the product registry                                                                          
00267    // and create the label to be given to the CrossingFrame                                                                                 
00268 
00269    edm::Service<edm::ConstProductRegistry> reg;
00270    // Loop over provenance of products in registry.                                                                                         
00271    std::string lookfor;
00272    if (object=="HepMCProduct") lookfor="edm::"+object;//exception for HepMCProduct                                                          
00273    else if (object=="edm::HepMCProduct") lookfor=object;
00274    else  lookfor="std::vector<"+object+">";
00275    bool found=false;
00276    for (edm::ProductRegistry::ProductList::const_iterator it = reg->productList().begin();
00277         it != reg->productList().end(); ++it) {
00278       // See FWCore/Framework/interface/BranchDescription.h                                                                                  
00279       // BranchDescription contains all the information for the product.                                                                     
00280       edm::BranchDescription desc = it->second;
00281       if (desc.className()==lookfor && desc.moduleLabel()==tag.label() && desc.productInstanceName()==tag.instance()) {
00282          label=desc.moduleLabel()+desc.productInstanceName();
00283          found=true;
00284          /*
00285          wantedBranches_.push_back(desc.friendlyClassName() + '_' +
00286                                    desc.moduleLabel() + '_' +
00287                                    desc.productInstanceName());
00288          */
00289          break;
00290       }
00291    }
00292    if (!found) {
00293       LogWarning("MixingModule")<<"!!!!!!!!!Could not find in registry requested object: "<<object<<" with "<<tag<<".\nWill NOT be considered for mixing!!!!!!!!!";
00294       return false;
00295    }
00296 
00297    return true;
00298 }
00299 
00300 //define this as a plug-in
00301 DEFINE_FWK_MODULE(HiMixingModule);                                                                               
00302 
00303 }