CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/RecoTauTag/TauTagTools/plugins/TauMVATrainer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    TauMVATrainer
00004 // Class:      TauMVATrainer
00005 // 
00013 //
00014 // Original Author:  Evan K.Friis, UC Davis  (friis@physics.ucdavis.edu)
00015 //         Created:  Fri Aug 15 11:22:14 PDT 2008
00016 // $Id: TauMVATrainer.cc,v 1.7 2010/10/19 20:22:27 wmtan Exp $
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/EDAnalyzer.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 #include "TTree.h"
00034 #include "TFile.h"
00035 #include "RecoTauTag/TauTagTools/interface/TauDecayModeTruthMatcher.h"
00036 #include "RecoTauTag/TauTagTools/interface/DiscriminantList.h"
00037 #include "DataFormats/TauReco/interface/PFTauDecayModeAssociation.h"
00038 
00039 //
00040 // class decleration
00041 //
00042 using namespace std;
00043 using namespace edm;
00044 using namespace reco;
00045 using namespace PFTauDiscriminants;
00046 
00047 class TauMVATrainer : public edm::EDAnalyzer {
00048    public:
00049       
00050       struct tauMatchingInfoHolder {
00051          InputTag                               truthToRecoTauMatchingTag;         
00052          edm::Handle<PFTauDecayModeMatchMap>         truthToRecoTauMatchingHandle;
00053          InputTag                               decayModeToRecoTauAssociationTag;
00054          edm::Handle<PFTauDecayModeAssociation>      decayModeToRecoTauAssociationHandle;
00055          TTree*                                 associatedTTree;
00056       };
00057 
00058       typedef std::vector<tauMatchingInfoHolder> tauMatchingInfos;
00059       typedef std::vector<std::pair<TTree*, const PFTauDecayModeMatchMap*> > treeToMatchTuple;
00060 
00061       explicit TauMVATrainer(const edm::ParameterSet&);
00062       ~TauMVATrainer();
00063       virtual void beginJob() ;
00064       virtual void analyze(const edm::Event&, const edm::EventSetup&);
00065       virtual void endJob() ;
00066 
00067    private:
00068       // ----------member data ---------------------------
00069       InputTag                  mcTruthSource_;
00070       //vector<InputTag>          matchingSources_;
00071       std::vector<ParameterSet>      matchingSources_;
00072       std::vector<tauMatchingInfoHolder> matchingInfo_;
00073       bool                      iAmSignal_;
00074 
00075       uint32_t                  maxTracks_;     //any objects w/ nTracks > will be automatically flagged as background
00076       uint32_t                  maxPiZeroes_;   //any objects w/ nPiZeros > will be automatically flagged as background
00077 
00078       std::string               outputRootFileName_;
00079       std::map<string, TTree*>       myTrainerTrees_;
00080       TTree*                    theTruthTree_;  //cache this to prevent string lookup
00081       PFTauDiscriminantManager  discriminantManager_;
00082       TFile*                    outputFile_;
00083       DiscriminantList          myDiscriminants_;
00084 };
00085 
00086 
00087 //
00088 // constructors and destructor
00089 //
00090 TauMVATrainer::TauMVATrainer(const edm::ParameterSet& iConfig):
00091                    mcTruthSource_(iConfig.getParameter<InputTag>("mcTruthSource")),
00092                    matchingSources_(iConfig.getParameter<vector<ParameterSet> >("matchingSources")),
00093                    iAmSignal_(iConfig.getParameter<bool>("iAmSignal")),
00094                    maxTracks_(iConfig.getParameter<uint32_t>("maxTracks")),
00095                    maxPiZeroes_(iConfig.getParameter<uint32_t>("maxPiZeroes")),
00096                    outputRootFileName_(iConfig.getParameter<string>("outputRootFileName"))
00097 
00098 {
00099    outputFile_ = new TFile(outputRootFileName_.c_str(), "RECREATE");
00100    edm::LogInfo("TauMVATrainer") << "Initializing TauMVATrainer ctor...";
00101    // set as signal or background
00102    discriminantManager_.setSignalFlag(iAmSignal_);
00103 
00104    edm::LogInfo("TauMVATrainer") << "Adding discriminants to TauDiscriminantManager...";
00105    // add the discriminants to the discriminant manager
00106    for(DiscriminantList::const_iterator aDiscriminant  = myDiscriminants_.begin();
00107                                         aDiscriminant != myDiscriminants_.end();
00108                                       ++aDiscriminant)
00109    {
00110       discriminantManager_.addDiscriminant(*aDiscriminant);
00111    }
00112 
00113    //create tree to hold truth variables
00114    edm::LogInfo("TauMVATrainer") << "Building truth tree...";
00115    TTree* truthTree = new TTree("truth", "truth");
00116 //   truthTree->SetDebug();
00117    myTrainerTrees_.insert(make_pair("truth", truthTree));
00118    theTruthTree_ = truthTree;
00119    // branch this trees according to the holder variables in the discrimimnant manager
00120    discriminantManager_.branchTree(truthTree);
00121 
00122    for(std::vector<ParameterSet>::const_iterator iSrc  = matchingSources_.begin();
00123                                             iSrc != matchingSources_.end();
00124                                           ++iSrc)
00125    {
00126       //create new matching info record
00127       tauMatchingInfoHolder aMatcher;
00128       //create a new tree for each input source
00129       aMatcher.truthToRecoTauMatchingTag        = iSrc->getParameter<InputTag>("truthMatchSource");
00130       aMatcher.decayModeToRecoTauAssociationTag = iSrc->getParameter<InputTag>("decayModeAssociationSource"); 
00131       string label = aMatcher.decayModeToRecoTauAssociationTag.label();
00132       edm::LogInfo("TauMVATrainer") << "Building reco tree w/ label: " << label << "...";
00133       TTree* newTree = new TTree(label.c_str(),label.c_str());
00134       discriminantManager_.branchTree(newTree);
00135       aMatcher.associatedTTree = newTree;
00136       matchingInfo_.push_back(aMatcher);
00137       myTrainerTrees_.insert(make_pair(label, newTree));
00138    }
00139 
00140 }
00141 
00142 
00143 TauMVATrainer::~TauMVATrainer()
00144 {
00145 }
00146 
00147 
00148 // ------------ method called to produce the data  ------------
00149 void
00150 TauMVATrainer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00151 {
00152    using namespace edm;
00153    using namespace reco;
00154 
00155 
00156    // get list of MC Truth objects
00157    edm::Handle<PFTauDecayModeCollection> truthObjects;
00158    iEvent.getByLabel(mcTruthSource_, truthObjects);
00159 
00160    discriminantManager_.setEvent(iEvent, 1.0); // unit weight for now
00161 
00162    size_t numberOfTruthObjects = truthObjects->size();
00163    // loop over true MCTaus and find matched reco objects for each producer
00164    for(size_t iTrueTau = 0; iTrueTau < numberOfTruthObjects; ++iTrueTau)
00165    {
00166       PFTauDecayModeRef theTrueTau = PFTauDecayModeRef(truthObjects, iTrueTau);
00167 
00168       // compute quantities for the truth object and fill associated tree
00169       discriminantManager_.setTau(*theTrueTau);
00170       theTruthTree_->Fill();
00171 
00172       // loop over the reco object collections
00173       for(tauMatchingInfos::iterator iMatchingInfo  = matchingInfo_.begin();
00174                                      iMatchingInfo != matchingInfo_.end();
00175                                    ++iMatchingInfo)
00176       {
00177          //get matching info from event
00178          edm::Handle<PFTauDecayModeMatchMap>& theMatching = iMatchingInfo->truthToRecoTauMatchingHandle;
00179          iEvent.getByLabel(iMatchingInfo->truthToRecoTauMatchingTag, theMatching);
00180 
00181          //get PFTau->PFTauDecayMode association from event
00182          edm::Handle<PFTauDecayModeAssociation>& theDMAssoc = iMatchingInfo->decayModeToRecoTauAssociationHandle;
00183          iEvent.getByLabel(iMatchingInfo->decayModeToRecoTauAssociationTag, theDMAssoc);
00184 
00185          //get associated ttree
00186          TTree* treeToFill           = iMatchingInfo->associatedTTree;
00187 
00188          // Retrieves associated PFTau
00189          PFTauRef          theAssociatedRecoTau   = (*theMatching)[theTrueTau];
00190 
00191          //determine if there is a RECO match and make sure it has at least one charged signal occupant
00192          bool isNonNull = (theAssociatedRecoTau.isNonnull() && theAssociatedRecoTau->signalPFChargedHadrCands().size());
00193 
00194          // apply discriminants if there is an associated reconstructed object with at least one track
00195          if(isNonNull)
00196          {
00197             // From associated PFTau get the DecayMode reconstruction
00198             const PFTauDecayMode& theAssociatedDecayMode = (*theDMAssoc)[theAssociatedRecoTau];
00199             //determine if tau needs a PRE-pass/fail cut
00200             bool prePass = false;
00201             bool preFail = false;
00202             unsigned int numberOfTracks   = theAssociatedDecayMode.chargedPions().numberOfDaughters();
00203             unsigned int charge           = std::abs(theAssociatedDecayMode.charge());
00204             unsigned int numberOfPiZeros  = theAssociatedDecayMode.neutralPions().numberOfDaughters();
00205             unsigned int numberOfOutliers = theAssociatedDecayMode.pfTauRef()->isolationPFCands().size();
00206             //cut on high multiplicity
00207             if (numberOfTracks > maxTracks_ || numberOfPiZeros > maxPiZeroes_  || (charge != 1 && numberOfTracks == 3))
00208                preFail = true;
00209             //cut on isolated single prong
00210             else if (numberOfTracks == 1 && numberOfPiZeros == 0 && numberOfOutliers == 0)
00211             {
00212                prePass = true;
00213             }
00214 
00215             discriminantManager_.setTau(theAssociatedDecayMode, prePass, preFail);
00216          }
00217          else 
00218             // if not, set the null flag
00219             discriminantManager_.setNullResult();
00220          treeToFill->Fill();
00221       }
00222    }
00223 }
00224 
00225 // ------------ method called once each job just before starting event loop  ------------
00226 void 
00227 TauMVATrainer::beginJob()
00228 {
00229 }
00230 
00231 // ------------ method called once each job just after ending the event loop  ------------
00232 void 
00233 TauMVATrainer::endJob() {
00234    for(std::map<string, TTree*>::iterator iTree  = myTrainerTrees_.begin();
00235                                      iTree != myTrainerTrees_.end();
00236                                    ++iTree)
00237    {
00238       const TTree* myTree = iTree->second;
00239       edm::LogInfo("TauMVATrainer") << "Tree " << myTree->GetName() << " has " << myTree->GetEntries() << " entries.";
00240    }
00241    outputFile_->Write();
00242 }
00243 
00244 //define this as a plug-in
00245 
00246 DEFINE_FWK_MODULE(TauMVATrainer);
00247 //DEFINE_FWK_MODULE(TauMVATrainer);