CMS 3D CMS Logo

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

Generated on Tue Jun 9 17:45:09 2009 for CMSSW by  doxygen 1.5.4