CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/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.10 2012/07/19 16:09:31 wdd 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 
00038 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00039 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00040 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
00041 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00042 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00043 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00044 #include "SimDataFormats/PileupSummaryInfo/interface/PileupMixingContent.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            LogInfo("HiEmbedding")<<"itag "<<itag;
00099            LogInfo("HiEmbedding")<<"label "<<tags_[itag].label();
00100            LogInfo("HiEmbedding")<<"instance "<<tags_[itag].instance();
00101             Handle<std::vector<T> > hand;
00102             handles.push_back(hand);
00103             get = get && e.getByLabel(tags_[itag],handles[itag]);
00104             if(!get)  LogWarning("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)  LogWarning("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("HiMixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;       
00224       }
00225    }
00226 
00227    produces<PileupMixingContent>();
00228 }
00229    
00230 
00231 HiMixingModule::~HiMixingModule()
00232 {
00233  
00234    // do anything here that needs to be done at desctruction time
00235    // (e.g. close files, deallocate resources etc.)
00236 
00237 }
00238 
00239 
00240 //
00241 // member functions
00242 //
00243 
00244 // ------------ method called to produce the data  ------------
00245 void
00246 HiMixingModule::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00247 {
00248    using namespace edm;
00249 
00250    for(size_t i = 0; i < workers_.size(); ++i){
00251       (workers_[i])->addSignals(iEvent);
00252    }
00253 
00254    std::auto_ptr< PileupMixingContent > PileupMixing_ = std::auto_ptr< PileupMixingContent >(new PileupMixingContent());
00255    iEvent.put(PileupMixing_);
00256 
00257 }
00258 
00259 // ------------ method called once each job just before starting event loop  ------------
00260 void 
00261 HiMixingModule::beginJob()
00262 {
00263 }
00264 
00265 // ------------ method called once each job just after ending the event loop  ------------
00266 void 
00267 HiMixingModule::endJob() {
00268 }
00269 
00270 bool HiMixingModule::verifyRegistry(std::string object, std::string subdet, InputTag &tag,std::string &label) {
00271    // verify that the given product exists in the product registry                                                                          
00272    // and create the label to be given to the CrossingFrame                                                                                 
00273 
00274    edm::Service<edm::ConstProductRegistry> reg;
00275    // Loop over provenance of products in registry.                                                                                         
00276    std::string lookfor;
00277    if (object=="HepMCProduct") lookfor="edm::"+object;//exception for HepMCProduct                                                          
00278    else if (object=="edm::HepMCProduct") lookfor=object;
00279    else  lookfor="std::vector<"+object+">";
00280    bool found=false;
00281    for (edm::ProductRegistry::ProductList::const_iterator it = reg->productList().begin();
00282         it != reg->productList().end(); ++it) {
00283       // See FWCore/Framework/interface/BranchDescription.h                                                                                  
00284       // BranchDescription contains all the information for the product.                                                                     
00285       edm::BranchDescription desc = it->second;
00286       if (desc.className()==lookfor && desc.moduleLabel()==tag.label() && desc.productInstanceName()==tag.instance()) {
00287          label=desc.moduleLabel()+desc.productInstanceName();
00288          found=true;
00289          /*
00290          wantedBranches_.push_back(desc.friendlyClassName() + '_' +
00291                                    desc.moduleLabel() + '_' +
00292                                    desc.productInstanceName());
00293          */
00294          break;
00295       }
00296    }
00297    if (!found) {
00298       LogWarning("MixingModule")<<"!!!!!!!!!Could not find in registry requested object: "<<object<<" with "<<tag<<".\nWill NOT be considered for mixing!!!!!!!!!";
00299       return false;
00300    }
00301 
00302    return true;
00303 }
00304 
00305 //define this as a plug-in
00306 DEFINE_FWK_MODULE(HiMixingModule);                                                                               
00307 
00308 }