CMS 3D CMS Logo

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