CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFRecoTauDiscriminationAgainstMuonMVA.cc
Go to the documentation of this file.
1 
11 
17 
19 
26 
30 
31 #include <TMath.h>
32 #include <TFile.h>
33 
34 #include <iostream>
35 
36 using namespace reco;
37 
38 namespace
39 {
40  const GBRForest* loadMVAfromFile(const edm::FileInPath& inputFileName, const std::string& mvaName, std::vector<TFile*>& inputFilesToDelete)
41  {
42  if ( inputFileName.location() == edm::FileInPath::Unknown ) throw cms::Exception("PFRecoTauDiscriminationAgainstMuonMVA::loadMVA")
43  << " Failed to find File = " << inputFileName << " !!\n";
44  TFile* inputFile = new TFile(inputFileName.fullPath().data());
45 
46  //const GBRForest* mva = dynamic_cast<GBRForest*>(inputFile->Get(mvaName.data())); // CV: dynamic_cast<GBRForest*> fails for some reason ?!
47  const GBRForest* mva = (GBRForest*)inputFile->Get(mvaName.data());
48  if ( !mva )
49  throw cms::Exception("PFRecoTauDiscriminationAgainstMuonMVA::loadMVA")
50  << " Failed to load MVA = " << mvaName.data() << " from file = " << inputFileName.fullPath().data() << " !!\n";
51 
52  inputFilesToDelete.push_back(inputFile);
53 
54  return mva;
55  }
56 
57  const GBRForest* loadMVAfromDB(const edm::EventSetup& es, const std::string& mvaName)
58  {
60  es.get<GBRWrapperRcd>().get(mvaName, mva);
61  return mva.product();
62  }
63 
64 
66 {
67  public:
70  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
71  mvaReader_(0),
72  mvaInput_(0)
73  {
74  mvaName_ = cfg.getParameter<std::string>("mvaName");
75  loadMVAfromDB_ = cfg.exists("loadMVAfromDB") ? cfg.getParameter<bool>("loadMVAfromDB") : false;
76  if ( !loadMVAfromDB_ ) {
77  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
78  }
79  mvaInput_ = new float[11];
80 
81  srcMuons_ = cfg.getParameter<edm::InputTag>("srcMuons");
82  Muons_token=consumes<reco::MuonCollection>(srcMuons_);
83  dRmuonMatch_ = cfg.getParameter<double>("dRmuonMatch");
84 
85  verbosity_ = ( cfg.exists("verbosity") ) ?
86  cfg.getParameter<int>("verbosity") : 0;
87 
88  produces<PFTauDiscriminator>("category");
89  }
90 
91  void beginEvent(const edm::Event&, const edm::EventSetup&);
92 
93  double discriminate(const PFTauRef&) const;
94 
95  void endEvent(edm::Event&);
96 
98  {
99  if ( !loadMVAfromDB_ ) delete mvaReader_;
100  delete[] mvaInput_;
101  for ( std::vector<TFile*>::iterator it = inputFilesToDelete_.begin();
102  it != inputFilesToDelete_.end(); ++it ) {
103  delete (*it);
104  }
105  }
106 
107  private:
108 
110 
111  std::string mvaName_;
112  bool loadMVAfromDB_;
113  edm::FileInPath inputFileName_;
114  const GBRForest* mvaReader_;
115  float* mvaInput_;
116 
117  edm::InputTag srcMuons_;
120  double dRmuonMatch_;
121 
123  std::auto_ptr<PFTauDiscriminator> category_output_;
124 
125  std::vector<TFile*> inputFilesToDelete_;
126 
127  int verbosity_;
128 };
129 
131 {
132  if ( !mvaReader_ ) {
133  if ( loadMVAfromDB_ ) {
134  mvaReader_ = loadMVAfromDB(es, mvaName_);
135  } else {
136  mvaReader_ = loadMVAfromFile(inputFileName_, mvaName_, inputFilesToDelete_);
137  }
138  }
139 
140  evt.getByToken(Muons_token, muons_);
141 
142  evt.getByToken(Tau_token, taus_);
143  category_output_.reset(new PFTauDiscriminator(TauRefProd(taus_)));
144 }
145 
146 namespace
147 {
148  void countHits(const reco::Muon& muon, std::vector<int>& numHitsDT, std::vector<int>& numHitsCSC, std::vector<int>& numHitsRPC)
149  {
150  if ( muon.outerTrack().isNonnull() ) {
151  const reco::HitPattern &muonHitPattern = muon.outerTrack()->hitPattern();
152  for (int iHit = 0; iHit < muonHitPattern.numberOfHits(HitPattern::TRACK_HITS); ++iHit) {
153  uint32_t hit = muonHitPattern.getHitPattern(HitPattern::TRACK_HITS, iHit);
154  if ( hit == 0 ) break;
155  if ( muonHitPattern.muonHitFilter(hit) && (muonHitPattern.getHitType(hit) == TrackingRecHit::valid || muonHitPattern.getHitType(hit) == TrackingRecHit::bad) ) {
156  int muonStation = muonHitPattern.getMuonStation(hit) - 1; // CV: map into range 0..3
157  if ( muonStation >= 0 && muonStation < 4 ) {
158  if ( muonHitPattern.muonDTHitFilter(hit) ) ++numHitsDT[muonStation];
159  else if ( muonHitPattern.muonCSCHitFilter(hit) ) ++numHitsCSC[muonStation];
160  else if ( muonHitPattern.muonRPCHitFilter(hit) ) ++numHitsRPC[muonStation];
161  }
162  }
163  }
164  }
165  }
166 }
167 
169 {
170  if ( verbosity_ ) {
171  edm::LogPrint("PFTauAgainstMuonMVA") << "<PFRecoTauDiscriminationAgainstMuonMVA::discriminate>:";
172  edm::LogPrint("PFTauAgainstMuonMVA") << " moduleLabel = " << moduleLabel_;
173  }
174 
175  // CV: define dummy category index in order to use RecoTauDiscriminantCutMultiplexer module to appy WP cuts
176  double category = 0.;
177  category_output_->setValue(tauIndex_, category);
178 
179  // CV: computation of anti-muon MVA value requires presence of leading charged hadron
180  if ( tau->leadPFChargedHadrCand().isNull() ) return 0.;
181 
182  mvaInput_[0] = TMath::Abs(tau->eta());
183  double tauCaloEnECAL = 0.;
184  double tauCaloEnHCAL = 0.;
185  const std::vector<reco::PFCandidatePtr>& tauSignalPFCands = tau->signalPFCands();
186  for ( std::vector<reco::PFCandidatePtr>::const_iterator tauSignalPFCand = tauSignalPFCands.begin();
187  tauSignalPFCand != tauSignalPFCands.end(); ++tauSignalPFCand ) {
188  tauCaloEnECAL += (*tauSignalPFCand)->ecalEnergy();
189  tauCaloEnHCAL += (*tauSignalPFCand)->hcalEnergy();
190  }
191  mvaInput_[1] = TMath::Sqrt(TMath::Max(0., tauCaloEnECAL));
192  mvaInput_[2] = TMath::Sqrt(TMath::Max(0., tauCaloEnHCAL));
193  mvaInput_[3] = tau->leadPFChargedHadrCand()->pt()/TMath::Max(1.,Double_t(tau->pt()));
194  mvaInput_[4] = TMath::Sqrt(TMath::Max(0., tau->leadPFChargedHadrCand()->ecalEnergy()));
195  mvaInput_[5] = TMath::Sqrt(TMath::Max(0., tau->leadPFChargedHadrCand()->hcalEnergy()));
196  int numMatches = 0;
197  std::vector<int> numHitsDT(4);
198  std::vector<int> numHitsCSC(4);
199  std::vector<int> numHitsRPC(4);
200  for ( int iStation = 0; iStation < 4; ++iStation ) {
201  numHitsDT[iStation] = 0;
202  numHitsCSC[iStation] = 0;
203  numHitsRPC[iStation] = 0;
204  }
205  if ( tau->leadPFChargedHadrCand().isNonnull() ) {
206  reco::MuonRef muonRef = tau->leadPFChargedHadrCand()->muonRef();
207  if ( muonRef.isNonnull() ) {
208  numMatches = muonRef->numberOfMatches(reco::Muon::NoArbitration);
209  countHits(*muonRef, numHitsDT, numHitsCSC, numHitsRPC);
210  }
211  }
212  size_t numMuons = muons_->size();
213  for ( size_t idxMuon = 0; idxMuon < numMuons; ++idxMuon ) {
214  reco::MuonRef muon(muons_, idxMuon);
215  if ( tau->leadPFChargedHadrCand().isNonnull() && tau->leadPFChargedHadrCand()->muonRef().isNonnull() && muon == tau->leadPFChargedHadrCand()->muonRef() ) {
216  continue;
217  }
218  double dR = deltaR(muon->p4(), tau->p4());
219  if ( dR < dRmuonMatch_ ) {
220  numMatches += muon->numberOfMatches(reco::Muon::NoArbitration);
221  countHits(*muon, numHitsDT, numHitsCSC, numHitsRPC);
222  }
223  }
224  mvaInput_[6] = numMatches;
225  mvaInput_[7] = numHitsDT[0] + numHitsCSC[0] + numHitsRPC[0];
226  mvaInput_[8] = numHitsDT[1] + numHitsCSC[1] + numHitsRPC[1];
227  mvaInput_[9] = numHitsDT[2] + numHitsCSC[2] + numHitsRPC[2];
228  mvaInput_[10] = numHitsDT[3] + numHitsCSC[3] + numHitsRPC[3];
229 
230  double mvaValue = mvaReader_->GetClassifier(mvaInput_);
231  if ( verbosity_ ) {
232  edm::LogPrint("PFTauAgainstMuonMVA") << "mvaValue = " << mvaValue;
233  }
234  return mvaValue;
235 }
236 
238 {
239  // add all category indices to event
240  evt.put(category_output_, "category");
241 }
242 
243 }
244 
T getParameter(std::string const &) const
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
virtual const LorentzVector & p4() const GCC11_FINAL
four-momentum Lorentz vector
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:446
bool exists(std::string const &parameterName) const
checks if a parameter exists
virtual double discriminate(const TauRef &tau) const =0
tuple numMuons
Definition: patZpeak.py:40
virtual void endEvent(edm::Event &evt)
static bool muonCSCHitFilter(uint16_t pattern)
Definition: HitPattern.h:580
static uint32_t getHitType(uint16_t pattern)
Definition: HitPattern.h:660
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
bool isNull() const
Checks for null.
Definition: Ref.h:247
virtual TrackRef outerTrack() const
reference to Track reconstructed in the muon detector only
Definition: Muon.h:51
int numberOfMatches(ArbitrationType type=SegmentAndTrackArbitration) const
get number of chambers with matched segments
Definition: Muon.cc:60
DEFINE_FWK_MODULE(CosmicTrackingParticleSelector)
static bool muonHitFilter(uint16_t pattern)
Definition: HitPattern.h:609
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:159
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:86
static bool muonDTHitFilter(uint16_t pattern)
Definition: HitPattern.h:570
static uint16_t getMuonStation(uint16_t pattern)
Muon station (1-4). Only valid for muon patterns, of course. only for patterns from muon...
Definition: HitPattern.h:669
std::string fullPath() const
Definition: FileInPath.cc:165
uint16_t getHitPattern(HitCategory category, int position) const
Definition: HitPattern.h:448
moduleLabel_(iConfig.getParameter< string >("@module_label"))
static bool muonRPCHitFilter(uint16_t pattern)
Definition: HitPattern.h:590
virtual void beginEvent(const edm::Event &evt, const edm::EventSetup &evtSetup)
int numberOfHits(HitCategory category) const
Definition: HitPattern.h:721