CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TauMVATrainer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: TauMVATrainer
4 // Class: TauMVATrainer
5 //
13 //
14 // Original Author: Evan K.Friis, UC Davis (friis@physics.ucdavis.edu)
15 // Created: Fri Aug 15 11:22:14 PDT 2008
16 // $Id: TauMVATrainer.cc,v 1.7 2010/10/19 20:22:27 wmtan Exp $
17 //
18 //
19 
20 
21 // system include files
22 #include <memory>
23 
24 // user include files
27 
30 
32 
33 #include "TTree.h"
34 #include "TFile.h"
38 
39 //
40 // class decleration
41 //
42 using namespace std;
43 using namespace edm;
44 using namespace reco;
45 using namespace PFTauDiscriminants;
46 
48  public:
49 
56  };
57 
58  typedef std::vector<tauMatchingInfoHolder> tauMatchingInfos;
59  typedef std::vector<std::pair<TTree*, const PFTauDecayModeMatchMap*> > treeToMatchTuple;
60 
61  explicit TauMVATrainer(const edm::ParameterSet&);
62  ~TauMVATrainer();
63  virtual void beginJob() ;
64  virtual void analyze(const edm::Event&, const edm::EventSetup&);
65  virtual void endJob() ;
66 
67  private:
68  // ----------member data ---------------------------
70  //vector<InputTag> matchingSources_;
71  std::vector<ParameterSet> matchingSources_;
72  std::vector<tauMatchingInfoHolder> matchingInfo_;
73  bool iAmSignal_;
74 
75  uint32_t maxTracks_; //any objects w/ nTracks > will be automatically flagged as background
76  uint32_t maxPiZeroes_; //any objects w/ nPiZeros > will be automatically flagged as background
77 
79  std::map<string, TTree*> myTrainerTrees_;
80  TTree* theTruthTree_; //cache this to prevent string lookup
82  TFile* outputFile_;
84 };
85 
86 
87 //
88 // constructors and destructor
89 //
91  mcTruthSource_(iConfig.getParameter<InputTag>("mcTruthSource")),
92  matchingSources_(iConfig.getParameter<vector<ParameterSet> >("matchingSources")),
93  iAmSignal_(iConfig.getParameter<bool>("iAmSignal")),
94  maxTracks_(iConfig.getParameter<uint32_t>("maxTracks")),
95  maxPiZeroes_(iConfig.getParameter<uint32_t>("maxPiZeroes")),
96  outputRootFileName_(iConfig.getParameter<string>("outputRootFileName"))
97 
98 {
99  outputFile_ = new TFile(outputRootFileName_.c_str(), "RECREATE");
100  edm::LogInfo("TauMVATrainer") << "Initializing TauMVATrainer ctor...";
101  // set as signal or background
103 
104  edm::LogInfo("TauMVATrainer") << "Adding discriminants to TauDiscriminantManager...";
105  // add the discriminants to the discriminant manager
107  aDiscriminant != myDiscriminants_.end();
108  ++aDiscriminant)
109  {
110  discriminantManager_.addDiscriminant(*aDiscriminant);
111  }
112 
113  //create tree to hold truth variables
114  edm::LogInfo("TauMVATrainer") << "Building truth tree...";
115  TTree* truthTree = new TTree("truth", "truth");
116 // truthTree->SetDebug();
117  myTrainerTrees_.insert(make_pair("truth", truthTree));
118  theTruthTree_ = truthTree;
119  // branch this trees according to the holder variables in the discrimimnant manager
120  discriminantManager_.branchTree(truthTree);
121 
122  for(std::vector<ParameterSet>::const_iterator iSrc = matchingSources_.begin();
123  iSrc != matchingSources_.end();
124  ++iSrc)
125  {
126  //create new matching info record
127  tauMatchingInfoHolder aMatcher;
128  //create a new tree for each input source
129  aMatcher.truthToRecoTauMatchingTag = iSrc->getParameter<InputTag>("truthMatchSource");
130  aMatcher.decayModeToRecoTauAssociationTag = iSrc->getParameter<InputTag>("decayModeAssociationSource");
131  string label = aMatcher.decayModeToRecoTauAssociationTag.label();
132  edm::LogInfo("TauMVATrainer") << "Building reco tree w/ label: " << label << "...";
133  TTree* newTree = new TTree(label.c_str(),label.c_str());
135  aMatcher.associatedTTree = newTree;
136  matchingInfo_.push_back(aMatcher);
137  myTrainerTrees_.insert(make_pair(label, newTree));
138  }
139 
140 }
141 
142 
144 {
145 }
146 
147 
148 // ------------ method called to produce the data ------------
149 void
151 {
152  using namespace edm;
153  using namespace reco;
154 
155 
156  // get list of MC Truth objects
158  iEvent.getByLabel(mcTruthSource_, truthObjects);
159 
160  discriminantManager_.setEvent(iEvent, 1.0); // unit weight for now
161 
162  size_t numberOfTruthObjects = truthObjects->size();
163  // loop over true MCTaus and find matched reco objects for each producer
164  for(size_t iTrueTau = 0; iTrueTau < numberOfTruthObjects; ++iTrueTau)
165  {
166  PFTauDecayModeRef theTrueTau = PFTauDecayModeRef(truthObjects, iTrueTau);
167 
168  // compute quantities for the truth object and fill associated tree
169  discriminantManager_.setTau(*theTrueTau);
170  theTruthTree_->Fill();
171 
172  // loop over the reco object collections
173  for(tauMatchingInfos::iterator iMatchingInfo = matchingInfo_.begin();
174  iMatchingInfo != matchingInfo_.end();
175  ++iMatchingInfo)
176  {
177  //get matching info from event
178  edm::Handle<PFTauDecayModeMatchMap>& theMatching = iMatchingInfo->truthToRecoTauMatchingHandle;
179  iEvent.getByLabel(iMatchingInfo->truthToRecoTauMatchingTag, theMatching);
180 
181  //get PFTau->PFTauDecayMode association from event
182  edm::Handle<PFTauDecayModeAssociation>& theDMAssoc = iMatchingInfo->decayModeToRecoTauAssociationHandle;
183  iEvent.getByLabel(iMatchingInfo->decayModeToRecoTauAssociationTag, theDMAssoc);
184 
185  //get associated ttree
186  TTree* treeToFill = iMatchingInfo->associatedTTree;
187 
188  // Retrieves associated PFTau
189  PFTauRef theAssociatedRecoTau = (*theMatching)[theTrueTau];
190 
191  //determine if there is a RECO match and make sure it has at least one charged signal occupant
192  bool isNonNull = (theAssociatedRecoTau.isNonnull() && theAssociatedRecoTau->signalPFChargedHadrCands().size());
193 
194  // apply discriminants if there is an associated reconstructed object with at least one track
195  if(isNonNull)
196  {
197  // From associated PFTau get the DecayMode reconstruction
198  const PFTauDecayMode& theAssociatedDecayMode = (*theDMAssoc)[theAssociatedRecoTau];
199  //determine if tau needs a PRE-pass/fail cut
200  bool prePass = false;
201  bool preFail = false;
202  unsigned int numberOfTracks = theAssociatedDecayMode.chargedPions().numberOfDaughters();
203  unsigned int charge = std::abs(theAssociatedDecayMode.charge());
204  unsigned int numberOfPiZeros = theAssociatedDecayMode.neutralPions().numberOfDaughters();
205  unsigned int numberOfOutliers = theAssociatedDecayMode.pfTauRef()->isolationPFCands().size();
206  //cut on high multiplicity
207  if (numberOfTracks > maxTracks_ || numberOfPiZeros > maxPiZeroes_ || (charge != 1 && numberOfTracks == 3))
208  preFail = true;
209  //cut on isolated single prong
210  else if (numberOfTracks == 1 && numberOfPiZeros == 0 && numberOfOutliers == 0)
211  {
212  prePass = true;
213  }
214 
215  discriminantManager_.setTau(theAssociatedDecayMode, prePass, preFail);
216  }
217  else
218  // if not, set the null flag
220  treeToFill->Fill();
221  }
222  }
223 }
224 
225 // ------------ method called once each job just before starting event loop ------------
226 void
228 {
229 }
230 
231 // ------------ method called once each job just after ending the event loop ------------
232 void
234  for(std::map<string, TTree*>::iterator iTree = myTrainerTrees_.begin();
235  iTree != myTrainerTrees_.end();
236  ++iTree)
237  {
238  const TTree* myTree = iTree->second;
239  edm::LogInfo("TauMVATrainer") << "Tree " << myTree->GetName() << " has " << myTree->GetEntries() << " entries.";
240  }
241  outputFile_->Write();
242 }
243 
244 //define this as a plug-in
245 
247 //DEFINE_FWK_MODULE(TauMVATrainer);
InputTag mcTruthSource_
std::string outputRootFileName_
std::map< string, TTree * > myTrainerTrees_
void addDiscriminant(Discriminant *const aDiscriminant)
add a discriminant
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::Handle< PFTauDecayModeMatchMap > truthToRecoTauMatchingHandle
virtual void analyze(const edm::Event &, const edm::EventSetup &)
bool setTau(const reco::PFTauDecayMode &theTau, bool prePass=false, bool preFail=false)
set objects for this discriminant
edm::Handle< PFTauDecayModeAssociation > decayModeToRecoTauAssociationHandle
uint32_t maxTracks_
#define abs(x)
Definition: mlp_lapack.h:159
const VertexCompositeCandidate & chargedPions() const
returns collection of charged pions w/ vertex information (tracks are refit)
double charge(const std::vector< uint8_t > &Ampls)
virtual size_type numberOfDaughters() const
number of daughters
const PFTauRef & pfTauRef() const
return reference to associated PFTau object
void beginJob()
Definition: Breakpoints.cc:15
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
int iEvent
Definition: GenABIO.cc:243
std::vector< ParameterSet > matchingSources_
const CompositeCandidate & neutralPions() const
returns a collection of merged Pi0s
std::vector< tauMatchingInfoHolder > tauMatchingInfos
void setEvent(const edm::Event &, double eventWeight)
set the current event. Must be called (once per event) before setTau or setNullResult ...
uint32_t maxPiZeroes_
virtual void beginJob()
std::vector< std::pair< TTree *, const PFTauDecayModeMatchMap * > > treeToMatchTuple
std::vector< tauMatchingInfoHolder > matchingInfo_
virtual void endJob()
edm::Ref< PFTauDecayModeCollection > PFTauDecayModeRef
presistent reference to a PFTauDecayMode
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
bool branchTree(TTree *const treeToBranch, bool addTargetBranch=false, bool addWeightBranch=false)
add a set of branches ot the TTree
const_iterator begin()
iterators over the list
virtual int charge() const GCC11_FINAL
electric charge
std::string const & label() const
Definition: InputTag.h:42
collection::const_iterator const_iterator
DiscriminantList myDiscriminants_
PFTauDiscriminantManager discriminantManager_
TTree * theTruthTree_
TFile * outputFile_
TauMVATrainer(const edm::ParameterSet &)