CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/RecoBTau/JetTagComputer/plugins/JetTagProducer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    JetTagProducer
00004 // Class:      JetTagProducer
00005 // 
00013 //
00014 // Original Author:  Andrea Rizzi
00015 //         Created:  Thu Apr  6 09:56:23 CEST 2006
00016 // $Id: JetTagProducer.cc,v 1.11 2010/02/11 00:13:36 wmtan Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <functional>
00023 #include <memory>
00024 #include <string>
00025 #include <vector>
00026 #include <map>
00027 
00028 // user include files
00029 #include "FWCore/Framework/interface/Frameworkfwd.h"
00030 #include "FWCore/Framework/interface/Event.h"
00031 #include "FWCore/Framework/interface/EventSetup.h"
00032 #include "FWCore/Framework/interface/ESHandle.h"
00033 #include "FWCore/Utilities/interface/InputTag.h"
00034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00035 #include "FWCore/Utilities/interface/Exception.h"
00036 #include "FWCore/Utilities/interface/EDMException.h"
00037 
00038 #include "DataFormats/Common/interface/View.h"
00039 #include "DataFormats/Common/interface/RefToBase.h"
00040 #include "DataFormats/TrackReco/interface/Track.h"
00041 #include "DataFormats/BTauReco/interface/BaseTagInfo.h"
00042 #include "DataFormats/BTauReco/interface/JetTag.h"
00043 
00044 #include "RecoBTau/JetTagComputer/interface/JetTagComputer.h"
00045 #include "RecoBTau/JetTagComputer/interface/JetTagComputerRecord.h"
00046 
00047 #include "JetTagProducer.h"
00048 
00049 using namespace std;
00050 using namespace reco;
00051 using namespace edm;
00052 
00053 //
00054 // constructors and destructor
00055 //
00056 JetTagProducer::JetTagProducer(const ParameterSet& iConfig) :
00057   m_computer(0),
00058   m_jetTagComputer(iConfig.getParameter<string>("jetTagComputer")),
00059   m_tagInfos(iConfig.getParameter< vector<InputTag> >("tagInfos"))
00060 {
00061   produces<JetTagCollection>();
00062 }
00063 
00064 JetTagProducer::~JetTagProducer()
00065 {
00066 }
00067 
00068 //
00069 // member functions
00070 //
00071 // internal method called on first event to locate and setup JetTagComputer
00072 void JetTagProducer::setup(const edm::EventSetup& iSetup)
00073 {
00074   edm::ESHandle<JetTagComputer> computer;
00075   iSetup.get<JetTagComputerRecord>().get( m_jetTagComputer, computer );
00076   m_computer = computer.product();
00077   m_computer->setEventSetup(iSetup);
00078 
00079   // finalize the JetTagProducer <-> JetTagComputer glue setup
00080   vector<string> inputLabels(m_computer->getInputLabels());
00081 
00082   // backward compatible case, use default tagInfo
00083   if (inputLabels.empty())
00084     inputLabels.push_back("tagInfo");
00085 
00086   if (m_tagInfos.size() != inputLabels.size()) {
00087     std::string message("VInputTag size mismatch - the following taginfo "
00088                         "labels are needed:\n");
00089     for(vector<string>::const_iterator iter = inputLabels.begin();
00090         iter != inputLabels.end(); ++iter)
00091       message += "\"" + *iter + "\"\n";
00092     throw edm::Exception(errors::Configuration) << message;
00093   }
00094 }
00095 
00096 // map helper - for some reason RefToBase lacks operator < (...)
00097 namespace {
00098   struct JetRefCompare :
00099        public binary_function<RefToBase<Jet>, RefToBase<Jet>, bool> {
00100     inline bool operator () (const RefToBase<Jet> &j1,
00101                              const RefToBase<Jet> &j2) const
00102     { return j1.id() < j2.id() || (j1.id() == j2.id() && j1.key() < j2.key()); }
00103   };
00104 }
00105 
00106 // ------------ method called to produce the data  ------------
00107 void
00108 JetTagProducer::produce(Event& iEvent, const EventSetup& iSetup)
00109 {
00110   if (m_computer)
00111     m_computer->setEventSetup(iSetup);
00112   else
00113     setup(iSetup);
00114 
00115   // now comes the tricky part:
00116   // we need to collect all requested TagInfos belonging to the same jet
00117 
00118   typedef vector<const BaseTagInfo*> TagInfoPtrs;
00119   typedef RefToBase<Jet> JetRef;
00120   typedef map<JetRef, TagInfoPtrs, JetRefCompare> JetToTagInfoMap;
00121 
00122   JetToTagInfoMap jetToTagInfos;
00123 
00124   // retrieve all requested TagInfos
00125   vector< Handle< View<BaseTagInfo> > > tagInfoHandles(m_tagInfos.size());
00126   unsigned int nTagInfos = m_tagInfos.size();
00127   for(unsigned int i = 0; i < nTagInfos; i++) {
00128     Handle< View<BaseTagInfo> > &tagInfoHandle = tagInfoHandles[i];
00129     iEvent.getByLabel(m_tagInfos[i], tagInfoHandle);
00130 
00131     for(View<BaseTagInfo>::const_iterator iter = tagInfoHandle->begin();
00132         iter != tagInfoHandle->end(); iter++) {
00133       TagInfoPtrs &tagInfos = jetToTagInfos[iter->jet()];
00134       if (tagInfos.empty())
00135         tagInfos.resize(nTagInfos);
00136 
00137       tagInfos[i] = &*iter;
00138     }
00139   }
00140 
00141   // take first tagInfo
00142   Handle< View<BaseTagInfo> > &tagInfoHandle = tagInfoHandles[0];
00143   auto_ptr<JetTagCollection> jetTagCollection;
00144   if (tagInfoHandle.product()->size() > 0) {
00145     RefToBase<Jet> jj = tagInfoHandle->begin()->jet();
00146     jetTagCollection.reset(new JetTagCollection(RefToBaseProd<Jet>(jj)));
00147   } else
00148     jetTagCollection.reset(new JetTagCollection());
00149 
00150   // now loop over the map and compute all JetTags
00151   for(JetToTagInfoMap::const_iterator iter = jetToTagInfos.begin();
00152       iter != jetToTagInfos.end(); iter++) {
00153     const TagInfoPtrs &tagInfoPtrs = iter->second;
00154 
00155     JetTagComputer::TagInfoHelper helper(tagInfoPtrs);
00156     float discriminator = (*m_computer)(helper);
00157 
00158     (*jetTagCollection)[iter->first] = discriminator;
00159   }
00160 
00161   iEvent.put(jetTagCollection);
00162 }
00163 
00164 // define it as plugin
00165 #include "FWCore/Framework/interface/MakerMacros.h"
00166 DEFINE_FWK_MODULE(JetTagProducer);