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 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
00068 InputTag mcTruthSource_;
00069
00070 vector<ParameterSet> matchingSources_;
00071 vector<tauMatchingInfoHolder> matchingInfo_;
00072 bool iAmSignal_;
00073
00074 uint32_t maxTracks_;
00075 uint32_t maxPiZeroes_;
00076
00077 std::string outputRootFileName_;
00078 map<string, TTree*> myTrainerTrees_;
00079 TTree* theTruthTree_;
00080 PFTauDiscriminantManager discriminantManager_;
00081 TFile* outputFile_;
00082 DiscriminantList myDiscriminants_;
00083 };
00084
00085
00086
00087
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
00101 discriminantManager_.setSignalFlag(iAmSignal_);
00102
00103 edm::LogInfo("TauMVATrainer") << "Adding discriminants to TauDiscriminantManager...";
00104
00105 for(DiscriminantList::const_iterator aDiscriminant = myDiscriminants_.begin();
00106 aDiscriminant != myDiscriminants_.end();
00107 ++aDiscriminant)
00108 {
00109 discriminantManager_.addDiscriminant(*aDiscriminant);
00110 }
00111
00112
00113 edm::LogInfo("TauMVATrainer") << "Building truth tree...";
00114 TTree* truthTree = new TTree("truth", "truth");
00115
00116 myTrainerTrees_.insert(make_pair("truth", truthTree));
00117 theTruthTree_ = truthTree;
00118
00119 discriminantManager_.branchTree(truthTree);
00120
00121 for(vector<ParameterSet>::const_iterator iSrc = matchingSources_.begin();
00122 iSrc != matchingSources_.end();
00123 ++iSrc)
00124 {
00125
00126 tauMatchingInfoHolder aMatcher;
00127
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
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
00156 Handle<PFTauDecayModeCollection> truthObjects;
00157 iEvent.getByLabel(mcTruthSource_, truthObjects);
00158
00159 size_t numberOfTruthObjects = truthObjects->size();
00160
00161 for(size_t iTrueTau = 0; iTrueTau < numberOfTruthObjects; ++iTrueTau)
00162 {
00163 PFTauDecayModeRef theTrueTau = PFTauDecayModeRef(truthObjects, iTrueTau);
00164
00165
00166 discriminantManager_.setEventData(*theTrueTau, iEvent);
00167 theTruthTree_->Fill();
00168
00169
00170 for(tauMatchingInfos::iterator iMatchingInfo = matchingInfo_.begin();
00171 iMatchingInfo != matchingInfo_.end();
00172 ++iMatchingInfo)
00173 {
00174
00175 Handle<PFTauDecayModeMatchMap>& theMatching = iMatchingInfo->truthToRecoTauMatchingHandle;
00176 iEvent.getByLabel(iMatchingInfo->truthToRecoTauMatchingTag, theMatching);
00177
00178
00179 Handle<PFTauDecayModeAssociation>& theDMAssoc = iMatchingInfo->decayModeToRecoTauAssociationHandle;
00180 iEvent.getByLabel(iMatchingInfo->decayModeToRecoTauAssociationTag, theDMAssoc);
00181
00182
00183 TTree* treeToFill = iMatchingInfo->associatedTTree;
00184
00185
00186 PFTauRef theAssociatedRecoTau = (*theMatching)[theTrueTau];
00187
00188
00189 bool isNonNull = (theAssociatedRecoTau.isNonnull() && theAssociatedRecoTau->signalPFChargedHadrCands().size());
00190
00191
00192 if(isNonNull)
00193 {
00194
00195 const PFTauDecayMode& theAssociatedDecayMode = (*theDMAssoc)[theAssociatedRecoTau];
00196
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
00204 if (numberOfTracks > maxTracks_ || numberOfPiZeros > maxPiZeroes_ || (charge != 1 && numberOfTracks == 3))
00205 preFail = true;
00206
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
00216 discriminantManager_.setNullResult(iEvent);
00217 treeToFill->Fill();
00218 }
00219 }
00220 }
00221
00222
00223 void
00224 TauMVATrainer::beginJob(const edm::EventSetup&)
00225 {
00226 }
00227
00228
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
00242 DEFINE_SEAL_MODULE();
00243 DEFINE_ANOTHER_FWK_MODULE(TauMVATrainer);
00244