Go to the documentation of this file.00001
00002
00003
00004
00005
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <memory>
00023
00024
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
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
00069 InputTag mcTruthSource_;
00070
00071 std::vector<ParameterSet> matchingSources_;
00072 std::vector<tauMatchingInfoHolder> matchingInfo_;
00073 bool iAmSignal_;
00074
00075 uint32_t maxTracks_;
00076 uint32_t maxPiZeroes_;
00077
00078 std::string outputRootFileName_;
00079 std::map<string, TTree*> myTrainerTrees_;
00080 TTree* theTruthTree_;
00081 PFTauDiscriminantManager discriminantManager_;
00082 TFile* outputFile_;
00083 DiscriminantList myDiscriminants_;
00084 };
00085
00086
00087
00088
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
00102 discriminantManager_.setSignalFlag(iAmSignal_);
00103
00104 edm::LogInfo("TauMVATrainer") << "Adding discriminants to TauDiscriminantManager...";
00105
00106 for(DiscriminantList::const_iterator aDiscriminant = myDiscriminants_.begin();
00107 aDiscriminant != myDiscriminants_.end();
00108 ++aDiscriminant)
00109 {
00110 discriminantManager_.addDiscriminant(*aDiscriminant);
00111 }
00112
00113
00114 edm::LogInfo("TauMVATrainer") << "Building truth tree...";
00115 TTree* truthTree = new TTree("truth", "truth");
00116
00117 myTrainerTrees_.insert(make_pair("truth", truthTree));
00118 theTruthTree_ = truthTree;
00119
00120 discriminantManager_.branchTree(truthTree);
00121
00122 for(std::vector<ParameterSet>::const_iterator iSrc = matchingSources_.begin();
00123 iSrc != matchingSources_.end();
00124 ++iSrc)
00125 {
00126
00127 tauMatchingInfoHolder aMatcher;
00128
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
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
00157 edm::Handle<PFTauDecayModeCollection> truthObjects;
00158 iEvent.getByLabel(mcTruthSource_, truthObjects);
00159
00160 discriminantManager_.setEvent(iEvent, 1.0);
00161
00162 size_t numberOfTruthObjects = truthObjects->size();
00163
00164 for(size_t iTrueTau = 0; iTrueTau < numberOfTruthObjects; ++iTrueTau)
00165 {
00166 PFTauDecayModeRef theTrueTau = PFTauDecayModeRef(truthObjects, iTrueTau);
00167
00168
00169 discriminantManager_.setTau(*theTrueTau);
00170 theTruthTree_->Fill();
00171
00172
00173 for(tauMatchingInfos::iterator iMatchingInfo = matchingInfo_.begin();
00174 iMatchingInfo != matchingInfo_.end();
00175 ++iMatchingInfo)
00176 {
00177
00178 edm::Handle<PFTauDecayModeMatchMap>& theMatching = iMatchingInfo->truthToRecoTauMatchingHandle;
00179 iEvent.getByLabel(iMatchingInfo->truthToRecoTauMatchingTag, theMatching);
00180
00181
00182 edm::Handle<PFTauDecayModeAssociation>& theDMAssoc = iMatchingInfo->decayModeToRecoTauAssociationHandle;
00183 iEvent.getByLabel(iMatchingInfo->decayModeToRecoTauAssociationTag, theDMAssoc);
00184
00185
00186 TTree* treeToFill = iMatchingInfo->associatedTTree;
00187
00188
00189 PFTauRef theAssociatedRecoTau = (*theMatching)[theTrueTau];
00190
00191
00192 bool isNonNull = (theAssociatedRecoTau.isNonnull() && theAssociatedRecoTau->signalPFChargedHadrCands().size());
00193
00194
00195 if(isNonNull)
00196 {
00197
00198 const PFTauDecayMode& theAssociatedDecayMode = (*theDMAssoc)[theAssociatedRecoTau];
00199
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
00207 if (numberOfTracks > maxTracks_ || numberOfPiZeros > maxPiZeroes_ || (charge != 1 && numberOfTracks == 3))
00208 preFail = true;
00209
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
00219 discriminantManager_.setNullResult();
00220 treeToFill->Fill();
00221 }
00222 }
00223 }
00224
00225
00226 void
00227 TauMVATrainer::beginJob()
00228 {
00229 }
00230
00231
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
00245
00246 DEFINE_FWK_MODULE(TauMVATrainer);
00247