CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TagProbeFitTreeProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: TagProbeFitTreeProducer
4 // Class: TagProbeFitTreeProducer
5 //
13 //
14 // Original Author: Sep 15 09:45
15 // Created: Mon Sep 15 09:49:08 CEST 2008
16 // $Id: TagProbeFitTreeProducer.cc,v 1.7 2010/05/02 15:18:25 gpetrucc Exp $
17 //
18 //
19 
20 
21 // system include files
22 #include <memory>
23 #include <ctype.h>
24 
25 // user include files
28 
33 
36 
39 
42 
43 #include <set>
44 
46 
47 //
48 // class decleration
49 //
50 
52  public:
55 
56 
57  private:
58  virtual void analyze(const edm::Event&, const edm::EventSetup&);
59  virtual void endJob() ;
60 
61  //---- MC truth information
63  bool isMC_;
67  std::set<int32_t> motherPdgId_;
69  bool checkMother(const reco::GenParticleRef &ref) const ;
70 
71  //---- Unbiased MC truth information
78 
82  std::auto_ptr<tnp::TPTreeFiller> treeFiller_;
84  std::auto_ptr<tnp::BaseTreeFiller> mcUnbiasFiller_;
85  std::auto_ptr<tnp::BaseTreeFiller> oldTagFiller_;
86  std::auto_ptr<tnp::BaseTreeFiller> tagFiller_;
87  std::auto_ptr<tnp::BaseTreeFiller> pairFiller_;
88  std::auto_ptr<tnp::BaseTreeFiller> mcFiller_;
89 };
90 
91 //
92 // constructors and destructor
93 //
95  isMC_(iConfig.getParameter<bool>("isMC")),
96  makeMCUnbiasTree_(isMC_ ? iConfig.getParameter<bool>("makeMCUnbiasTree") : false),
97  checkMotherInUnbiasEff_(makeMCUnbiasTree_ ? iConfig.getParameter<bool>("checkMotherInUnbiasEff") : false),
98  tagProbePairMaker_(iConfig),
99  treeFiller_(new tnp::TPTreeFiller(iConfig)),
100  oldTagFiller_((iConfig.existsAs<bool>("fillTagTree") && iConfig.getParameter<bool>("fillTagTree")) ? new tnp::BaseTreeFiller("tag_tree",iConfig) : 0)
101 {
102  if (isMC_) {
103  // For mc efficiency we need the MC matches for tags & probes
104  tagMatches_ = iConfig.getParameter<edm::InputTag>("tagMatches");
105  probeMatches_ = iConfig.getParameter<edm::InputTag>("probeMatches");
106  //.. and the pdgids of the possible mothers
107  if (iConfig.existsAs<int32_t>("motherPdgId")) {
108  motherPdgId_.insert(iConfig.getParameter<int32_t>("motherPdgId"));
109  } else {
110  std::vector<int32_t> motherIds = iConfig.getParameter<std::vector<int32_t> >("motherPdgId");
111  motherPdgId_.insert(motherIds.begin(), motherIds.end());
112  }
113 
114  // For unbiased efficiency we also need the collection of all probes
115  if (makeMCUnbiasTree_) {
116  allProbes_ = iConfig.getParameter<edm::InputTag>("allProbes");
117  mcUnbiasFiller_.reset(new tnp::BaseTreeFiller("mcUnbias_tree",iConfig));
118  }
119  }
120 
121  edm::ParameterSet tagPSet;
122  if (iConfig.existsAs<edm::ParameterSet>("tagVariables")) tagPSet.addParameter<edm::ParameterSet>("variables", iConfig.getParameter<edm::ParameterSet>("tagVariables"));
123  if (iConfig.existsAs<edm::ParameterSet>("tagFlags" )) tagPSet.addParameter<edm::ParameterSet>("flags", iConfig.getParameter<edm::ParameterSet>("tagFlags"));
124  if (!tagPSet.empty()) { tagFiller_.reset(new tnp::BaseTreeFiller(*treeFiller_, tagPSet, "tag_")); }
125  edm::ParameterSet mcPSet;
126  if (iConfig.existsAs<edm::ParameterSet>("mcVariables")) mcPSet.addParameter<edm::ParameterSet>("variables", iConfig.getParameter<edm::ParameterSet>("mcVariables"));
127  if (iConfig.existsAs<edm::ParameterSet>("mcFlags" )) mcPSet.addParameter<edm::ParameterSet>("flags", iConfig.getParameter<edm::ParameterSet>("mcFlags"));
128  if (!mcPSet.empty()) { mcFiller_.reset(new tnp::BaseTreeFiller(*treeFiller_, mcPSet, "mc_")); }
129  edm::ParameterSet pairPSet;
130  if (iConfig.existsAs<edm::ParameterSet>("pairVariables")) pairPSet.addParameter<edm::ParameterSet>("variables", iConfig.getParameter<edm::ParameterSet>("pairVariables"));
131  if (iConfig.existsAs<edm::ParameterSet>("pairFlags" )) pairPSet.addParameter<edm::ParameterSet>("flags", iConfig.getParameter<edm::ParameterSet>("pairFlags"));
132  if (!pairPSet.empty()) { pairFiller_.reset(new tnp::BaseTreeFiller(*treeFiller_, pairPSet, "pair_")); }
133 }
134 
135 
137 {
138 }
139 
140 
141 //
142 // member functions
143 //
144 
145 // ------------ method called to for each event ------------
146 void
148 {
149  using namespace edm; using namespace std;
150  Handle<reco::CandidateView> src, allProbes;
151  Handle<Association<vector<reco::GenParticle> > > tagMatches, probeMatches;
152 
153  treeFiller_->init(iEvent); // read out info from the event if needed (external vars, list of passing probes, ...)
154  if (oldTagFiller_.get()) oldTagFiller_->init(iEvent);
155  if (tagFiller_.get()) tagFiller_->init(iEvent);
156  if (pairFiller_.get()) pairFiller_->init(iEvent);
157  if (mcFiller_.get()) mcFiller_->init(iEvent);
158 
159  // on mc we want to load also the MC match info
160  if (isMC_) {
161  iEvent.getByLabel(tagMatches_, tagMatches);
162  iEvent.getByLabel(probeMatches_, probeMatches);
163  }
164 
165  // get the list of (tag+probe) pairs, performing arbitration
166  tnp::TagProbePairs pairs = tagProbePairMaker_.run(iEvent);
167  // loop on them to fill the tree
168  for (tnp::TagProbePairs::const_iterator it = pairs.begin(), ed = pairs.end(); it != ed; ++it) {
169  // on mc, fill mc info (on non-mc, let it to 'true', the treeFiller will ignore it anyway
170  bool mcTrue = false;
171  if (isMC_) {
172  reco::GenParticleRef mtag = (*tagMatches)[it->tag], mprobe = (*probeMatches)[it->probe];
173  mcTrue = checkMother(mtag) && checkMother(mprobe);
174  if (mcTrue && mcFiller_.get()) mcFiller_->fill(reco::CandidateBaseRef(mprobe));
175  }
176  // fill in the variables for this t+p pair
177  if (tagFiller_.get()) tagFiller_->fill(it->tag);
178  if (oldTagFiller_.get()) oldTagFiller_->fill(it->tag);
179  if (pairFiller_.get()) pairFiller_->fill(it->pair);
180  treeFiller_->fill(it->probe, it->mass, mcTrue);
181  }
182 
183  if (isMC_ && makeMCUnbiasTree_) {
184  // read full collection of probes
185  iEvent.getByLabel(allProbes_, allProbes);
186  // init the tree filler
187  mcUnbiasFiller_->init(iEvent);
188  // loop on probes
189  for (size_t i = 0, n = allProbes->size(); i < n; ++i) {
190  const reco::CandidateBaseRef & probe = allProbes->refAt(i);
191  // check mc match, and possibly mother match
192  reco::GenParticleRef probeMatch = (*probeMatches)[probe];
193  bool probeOk = checkMotherInUnbiasEff_ ? checkMother(probeMatch) : probeMatch.isNonnull();
194  // fill the tree only for good ones
195  if (probeOk) mcUnbiasFiller_->fill(probe);
196  }
197  }
198 
199 }
200 
201 bool
203  if (ref.isNull()) return false;
204  if (motherPdgId_.empty()) return true;
205  if (motherPdgId_.find(abs(ref->pdgId())) != motherPdgId_.end()) return true;
206  reco::GenParticle::mothers m = ref->motherRefVector();
207  for (reco::GenParticle::mothers::const_iterator it = m.begin(), e = m.end(); it != e; ++it) {
208  if (checkMother(*it)) return true;
209  }
210  return false;
211 }
212 
213 
214 // ------------ method called once each job just after ending the event loop ------------
215 void
217  // ask to write the current PSet info into the TTree header
218  treeFiller_->writeProvenance(edm::getProcessParameterSet());
219 }
220 
221 //define this as a plug-in
T getParameter(std::string const &) const
bool empty() const
Definition: ParameterSet.h:219
int i
Definition: DBlmapReader.cc:9
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:187
virtual void analyze(const edm::Event &, const edm::EventSetup &)
edm::InputTag tagMatches_
InputTag to an edm::Association&lt;reco::GenParticle&gt; from tags &amp; probes to MC truth.
std::auto_ptr< tnp::TPTreeFiller > treeFiller_
The object that actually computes variables and fills the tree for T&amp;P.
std::vector< TagProbePair > TagProbePairs
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::auto_ptr< tnp::BaseTreeFiller > oldTagFiller_
std::auto_ptr< tnp::BaseTreeFiller > tagFiller_
#define abs(x)
Definition: mlp_lapack.h:159
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:249
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:244
bool checkMotherInUnbiasEff_
Check mother pdgId in unbiased inefficiency measurement.
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
int iEvent
Definition: GenABIO.cc:243
bool isNull() const
Checks for null.
Definition: Ref.h:247
edm::InputTag allProbes_
InputTag to the collection of all probes.
bool isMC_
Is this sample MC?
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:145
std::auto_ptr< tnp::BaseTreeFiller > mcFiller_
bool makeMCUnbiasTree_
Do we have to compute this.
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
tnp::TagProbePairMaker tagProbePairMaker_
The object that produces pairs of tags and probes, making any arbitration needed. ...
std::auto_ptr< tnp::BaseTreeFiller > mcUnbiasFiller_
The object that actually computes variables and fills the tree for unbiased MC.
ParameterSet const & getProcessParameterSet()
Definition: Registry.cc:34
bool checkMother(const reco::GenParticleRef &ref) const
Return true if ref is not null and has an ancestor with pdgId inside &#39;motherPdgId_&#39;.
std::set< int32_t > motherPdgId_
Possible pdgids for the mother. If empty, any truth-matched mu will be considered good...
TagProbePairs run(const edm::Event &iEvent) const
fill in tghe T&amp;P pairs for this event
std::auto_ptr< tnp::BaseTreeFiller > pairFiller_
TagProbeFitTreeProducer(const edm::ParameterSet &)