CMS 3D CMS Logo

HIPTwoBodyDecayAnalyzer.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <vector>
3 #include <memory>
4 #include <utility>
5 #include <cmath>
6 #include <string>
7 #include "TLorentzVector.h"
8 #include "TTree.h"
9 #include "TH1F.h"
10 #include "TMath.h"
11 
12 // user include files
15 
24 
31 
32 class HIPTwoBodyDecayAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
33 public:
35  ~HIPTwoBodyDecayAnalyzer() override;
36 
41 
42  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
43 
44  TTree* tree;
45  std::vector<std::pair<std::string, float*>> floatBranches;
46  std::vector<std::pair<std::string, int*>> intBranches;
47  std::vector<std::pair<std::string, short*>> shortBranches;
48 
49 private:
50  // es consumes
52 
54  void analyze(const edm::Event&, const edm::EventSetup&) override;
55 
56  void bookAllBranches();
58  BranchType searchArray(std::string branchname, int& position);
59  template <typename varType>
61  int varposition = -1;
62  BranchType varbranchtype = searchArray(bname, varposition);
63  if (varposition == -1)
64  std::cerr << "HIPTwoBodyDecayAnalyzer::setVal -> Could not find the branch called " << bname << "!" << std::endl;
65  else if (varbranchtype == BranchType_short_t)
66  *(shortBranches.at(varposition).second) = value;
67  else if (varbranchtype == BranchType_int_t)
68  *(intBranches.at(varposition).second) = value;
69  else if (varbranchtype == BranchType_float_t)
70  *(floatBranches.at(varposition).second) = value;
71  else
72  std::cerr << "HIPTwoBodyDecayAnalyzer::setVal -> Could not find the type " << varbranchtype
73  << " for branch called " << bname << "!" << std::endl;
74  }
75  void cleanBranches();
76  void initializeBranches();
77  bool actuateBranches();
78 
79  void analyzeTrackCollection(std::string strTrackType,
80  const TransientTrackBuilder& theTTBuilder,
82  bool verbose = false);
85  bool& fitOk);
86 };
87 
89  : ttkbuilderToken_(esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))) {
90  usesResource(TFileService::kSharedResource);
91 
92  alcareco_trackCollToken_ = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("alcarecotracks"));
93  refit1_trackCollToken_ = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("refit1tracks"));
94  ctf_trackCollToken_ = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("refit2tracks"));
95  final_trackCollToken_ = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("finaltracks"));
96 
98 
99  tree = fs->make<TTree>("TestTree", "");
100  bookAllBranches();
101 }
102 
104  for (unsigned short el = 0; el < shortBranches.size(); el++) {
105  if (branchname == shortBranches.at(el).first) {
106  position = el;
107  return BranchType_short_t;
108  }
109  }
110  for (unsigned int el = 0; el < intBranches.size(); el++) {
111  if (branchname == intBranches.at(el).first) {
112  position = el;
113  return BranchType_int_t;
114  }
115  }
116  for (unsigned int el = 0; el < floatBranches.size(); el++) {
117  if (branchname == floatBranches.at(el).first) {
118  position = el;
119  return BranchType_float_t;
120  }
121  }
122  return BranchType_unknown_t;
123 }
125  for (unsigned short el = 0; el < shortBranches.size(); el++) {
126  if (shortBranches.at(el).second != nullptr)
127  delete shortBranches.at(el).second;
128  shortBranches.at(el).second = nullptr;
129  }
130  shortBranches.clear();
131  for (unsigned int el = 0; el < intBranches.size(); el++) {
132  if (intBranches.at(el).second != nullptr)
133  delete intBranches.at(el).second;
134  intBranches.at(el).second = nullptr;
135  }
136  intBranches.clear();
137  for (unsigned int el = 0; el < floatBranches.size(); el++) {
138  if (floatBranches.at(el).second != nullptr)
139  delete floatBranches.at(el).second;
140  floatBranches.at(el).second = nullptr;
141  }
142  floatBranches.clear();
143 }
145  for (unsigned short el = 0; el < shortBranches.size(); el++) {
146  if (shortBranches.at(el).second != nullptr)
147  *(shortBranches.at(el).second) = 0;
148  }
149  for (unsigned int el = 0; el < intBranches.size(); el++) {
150  if (intBranches.at(el).second != nullptr)
151  *(intBranches.at(el).second) = 0;
152  }
153  for (unsigned int el = 0; el < floatBranches.size(); el++) {
154  if (floatBranches.at(el).second != nullptr)
155  *(floatBranches.at(el).second) = 0;
156  }
157 }
158 
160  const int nTrackTypes = 4;
161  std::vector<std::string> strTrackTypes;
162  strTrackTypes.reserve(nTrackTypes);
163  strTrackTypes.push_back("alcareco");
164  strTrackTypes.push_back("refit1");
165  strTrackTypes.push_back("refit2");
166  strTrackTypes.push_back("final");
167  for (unsigned int it = 0; it < nTrackTypes; it++) {
168  std::string& strTrackType = strTrackTypes[it];
169  bookBranch(strTrackType + "_present", BranchType_short_t);
170  bookBranch(strTrackType + "_ZVtxFitOk", BranchType_short_t);
171  bookBranch(strTrackType + "_ZMass", BranchType_float_t);
172  bookBranch(strTrackType + "_ZPt", BranchType_float_t);
173  bookBranch(strTrackType + "_ZPz", BranchType_float_t);
174  bookBranch(strTrackType + "_ZPhi", BranchType_float_t);
175  bookBranch(strTrackType + "_ZVertex_x", BranchType_float_t);
176  bookBranch(strTrackType + "_ZVertex_y", BranchType_float_t);
177  bookBranch(strTrackType + "_ZVertex_z", BranchType_float_t);
178  bookBranch(strTrackType + "_ZVertex_NormChi2", BranchType_float_t);
179  bookBranch(strTrackType + "_MuPlusVertex_x", BranchType_float_t);
180  bookBranch(strTrackType + "_MuPlusVertex_y", BranchType_float_t);
181  bookBranch(strTrackType + "_MuPlusVertex_z", BranchType_float_t);
182  bookBranch(strTrackType + "_MuMinusPt_AfterZVtxFit", BranchType_float_t);
183  bookBranch(strTrackType + "_MuMinusPz_AfterZVtxFit", BranchType_float_t);
184  bookBranch(strTrackType + "_MuMinusPhi_AfterZVtxFit", BranchType_float_t);
185  bookBranch(strTrackType + "_MuPlusPt_AfterZVtxFit", BranchType_float_t);
186  bookBranch(strTrackType + "_MuPlusPz_AfterZVtxFit", BranchType_float_t);
187  bookBranch(strTrackType + "_MuPlusPhi_AfterZVtxFit", BranchType_float_t);
188  bookBranch(strTrackType + "_ZMass_AfterZVtxFit", BranchType_float_t);
189  bookBranch(strTrackType + "_ZPt_AfterZVtxFit", BranchType_float_t);
190  bookBranch(strTrackType + "_ZPz_AfterZVtxFit", BranchType_float_t);
191  bookBranch(strTrackType + "_ZPhi_AfterZVtxFit", BranchType_float_t);
192  bookBranch(strTrackType + "_MuMinusPt", BranchType_float_t);
193  bookBranch(strTrackType + "_MuMinusPz", BranchType_float_t);
194  bookBranch(strTrackType + "_MuMinusPhi", BranchType_float_t);
195  bookBranch(strTrackType + "_MuMinusVertex_x", BranchType_float_t);
196  bookBranch(strTrackType + "_MuMinusVertex_y", BranchType_float_t);
197  bookBranch(strTrackType + "_MuMinusVertex_z", BranchType_float_t);
198  bookBranch(strTrackType + "_MuPlusPt", BranchType_float_t);
199  bookBranch(strTrackType + "_MuPlusPz", BranchType_float_t);
200  bookBranch(strTrackType + "_MuPlusPhi", BranchType_float_t);
201  }
202  actuateBranches();
203 }
205  if (btype == BranchType_float_t)
206  floatBranches.emplace_back(bname, new float);
207  else if (btype == BranchType_int_t)
208  intBranches.emplace_back(bname, new int);
209  else if (btype == BranchType_short_t)
210  shortBranches.emplace_back(bname, new short);
211  else {
212  std::cerr << "HIPTwoBodyDecayAnalyzer::bookBranch: No support for type " << btype << " for the branch " << bname
213  << " is available." << std::endl;
214  return false;
215  }
216  return true;
217 }
219  bool success = true;
220  std::cout << "Begin HIPTwoBodyDecayAnalyzer::actuateBranches" << std::endl;
221  std::cout << "Number of short branches: " << shortBranches.size() << std::endl;
222  std::cout << "Number of int branches: " << intBranches.size() << std::endl;
223  std::cout << "Number of float branches: " << floatBranches.size() << std::endl;
224  if (tree != nullptr) {
225  for (unsigned short el = 0; el < shortBranches.size(); el++) {
226  std::cout << "Actuating branch " << shortBranches.at(el).first << " at address " << shortBranches.at(el).second
227  << std::endl;
228  if (!tree->GetBranchStatus(shortBranches.at(el).first.c_str()))
229  tree->Branch(shortBranches.at(el).first.c_str(), shortBranches.at(el).second);
230  else
231  std::cout << "Failed!" << std::endl;
232  }
233  for (unsigned int el = 0; el < intBranches.size(); el++) {
234  std::cout << "Actuating branch " << intBranches.at(el).first.c_str() << " at address "
235  << intBranches.at(el).second << std::endl;
236  if (!tree->GetBranchStatus(intBranches.at(el).first.c_str()))
237  tree->Branch(intBranches.at(el).first.c_str(), intBranches.at(el).second);
238  else
239  std::cout << "Failed!" << std::endl;
240  }
241  for (unsigned int el = 0; el < floatBranches.size(); el++) {
242  std::cout << "Actuating branch " << floatBranches.at(el).first.c_str() << " at address "
243  << floatBranches.at(el).second << std::endl;
244  if (!tree->GetBranchStatus(floatBranches.at(el).first.c_str()))
245  tree->Branch(floatBranches.at(el).first.c_str(), floatBranches.at(el).second);
246  else
247  std::cout << "Failed!" << std::endl;
248  }
249  } else
250  success = false;
251  if (!success)
252  std::cerr << "HIPTwoBodyDecayAnalyzer::actuateBranch: Failed to actuate the branches!" << std::endl;
253  return success;
254 }
256 
258  using namespace edm;
259  using namespace reco;
260  using reco::TrackCollection;
261 
262  edm::Handle<reco::TrackCollection> alcarecotracks;
263  iEvent.getByToken(alcareco_trackCollToken_, alcarecotracks);
265  iEvent.getByToken(refit1_trackCollToken_, refit1tracks);
267  iEvent.getByToken(ctf_trackCollToken_, ctftracks);
269  iEvent.getByToken(final_trackCollToken_, finaltracks);
270 
271  const auto& theTTBuilder = iSetup.getData(ttkbuilderToken_);
273 
274  analyzeTrackCollection("alcareco", theTTBuilder, alcarecotracks);
275  analyzeTrackCollection("refit1", theTTBuilder, refit1tracks);
276  analyzeTrackCollection("refit2", theTTBuilder, ctftracks);
277  analyzeTrackCollection("final", theTTBuilder, finaltracks);
278 
279  tree->Fill();
280 }
281 
284  desc.add<edm::InputTag>("alcarecotracks", edm::InputTag("ALCARECOTkAlZMuMu"));
285  desc.add<edm::InputTag>("refit1tracks", edm::InputTag("FirstTrackRefitter"));
286  desc.add<edm::InputTag>("refit2tracks", edm::InputTag("HitFilteredTracksTrackFitter"));
287  desc.add<edm::InputTag>("finaltracks", edm::InputTag("FinalTrackRefitter"));
288  descriptions.addWithDefaultLabel(desc);
289 }
290 
292  const TransientTrackBuilder& theTTBuilder,
294  bool verbose) {
295  if (verbose)
296  std::cout << "Starting to process the track collection for " << strTrackType << std::endl;
297 
298  using namespace edm;
299  using namespace reco;
300  using reco::TrackCollection;
301 
302  if (!hTrackColl.isValid()) {
303  if (verbose)
304  std::cout << "Track collection is invalid." << std::endl;
305  return;
306  }
307  if (hTrackColl->size() < 2) {
308  if (verbose)
309  std::cout << "Track collection size<2." << std::endl;
310  return;
311  }
312 
313  unsigned int itrk = 0;
314  unsigned int j = 0;
315  int totalcharge = 0;
316  bool isValidPair = true;
317  bool ZVtxOk = false;
318  TLorentzVector trackMom[2];
319  TLorentzVector trackMomAfterZVtxFit[2];
320  TVector3 trackVtx[2];
321 
322  for (unsigned int jtrk = 0; jtrk < 2; jtrk++) {
323  trackMom[jtrk].SetXYZT(0, 0, 0, 0);
324  trackVtx[jtrk].SetXYZ(0, 0, 0);
325  }
326  for (reco::TrackCollection::const_iterator track = hTrackColl->begin(); track != hTrackColl->end(); ++track) {
327  int charge = track->charge();
328  totalcharge += charge;
329  if (j == 0) {
330  itrk = (charge > 0 ? 1 : 0);
331  } else
332  itrk = 1 - itrk;
333  trackMom[itrk].SetPtEtaPhiM(track->pt(), track->eta(), track->phi(), 0.105);
334  trackVtx[itrk].SetXYZ(track->vx(), track->vy(), track->vz());
335  j++;
336  if (j == 2)
337  break;
338  }
339 
340  isValidPair = (totalcharge == 0 && trackMom[0].P() != 0. && trackMom[1].P() != 0.);
341  if (verbose && !isValidPair)
342  std::cout << "Track collection does not contain a valid std::pair." << std::endl;
343  setVal(strTrackType + "_present", (isValidPair ? (short)1 : (short)0));
344  if (isValidPair) {
345  TLorentzVector ZMom = trackMom[0] + trackMom[1];
346  setVal(strTrackType + "_ZPt", (float)ZMom.Pt());
347  setVal(strTrackType + "_ZPz", (float)ZMom.Pz());
348  setVal(strTrackType + "_ZPhi", (float)ZMom.Phi());
349  setVal(strTrackType + "_ZMass", (float)ZMom.M());
350 
351  reco::Vertex ZVtx = fitDimuonVertex(theTTBuilder, hTrackColl, ZVtxOk);
352  if (ZVtxOk) {
353  setVal(strTrackType + "_ZVertex_x", (float)ZVtx.x());
354  setVal(strTrackType + "_ZVertex_y", (float)ZVtx.y());
355  setVal(strTrackType + "_ZVertex_z", (float)ZVtx.z());
356  setVal(strTrackType + "_ZVertex_NormChi2", (float)ZVtx.normalizedChi2());
357 
358  // Recalculate track momenta with this vertex as reference
359  j = 0;
360  for (reco::TrackCollection::const_iterator track = hTrackColl->begin(); track != hTrackColl->end(); ++track) {
361  TransientTrack t_track = theTTBuilder.build(&(*track));
362  AnalyticalImpactPointExtrapolator extrapolator(t_track.field());
363  TrajectoryStateOnSurface closestIn3DSpaceState =
364  extrapolator.extrapolate(t_track.impactPointState(), RecoVertex::convertPos(ZVtx.position()));
365  GlobalVector mom = closestIn3DSpaceState.globalMomentum();
366  int charge = track->charge();
367  totalcharge += charge;
368  if (j == 0) {
369  itrk = (charge > 0 ? 1 : 0);
370  } else
371  itrk = 1 - itrk;
372  trackMomAfterZVtxFit[itrk].SetXYZT(mom.x(), mom.y(), mom.z(), sqrt(pow(0.105, 2) + pow(mom.mag(), 2)));
373  j++;
374  if (j == 2)
375  break;
376  }
377  if (totalcharge != 0)
378  std::cerr
379  << "HIPTwoBodyDecayAnalyzer::analyzeTrackCollection: Something went wrong! The total charge is no longer 0!"
380  << std::endl;
381  for (unsigned int jtrk = 0; jtrk < 2; jtrk++) {
382  std::string strMuCore = (jtrk == 0 ? "MuMinus" : "MuPlus");
383  setVal(strTrackType + "_" + strMuCore + "Pt_AfterZVtxFit", (float)trackMomAfterZVtxFit[jtrk].Pt());
384  setVal(strTrackType + "_" + strMuCore + "Pz_AfterZVtxFit", (float)trackMomAfterZVtxFit[jtrk].Pz());
385  setVal(strTrackType + "_" + strMuCore + "Phi_AfterZVtxFit", (float)trackMomAfterZVtxFit[jtrk].Phi());
386  }
387  TLorentzVector ZMom_AfterZVtxFit = trackMomAfterZVtxFit[0] + trackMomAfterZVtxFit[1];
388  setVal(strTrackType + "_ZPt_AfterZVtxFit", (float)ZMom_AfterZVtxFit.Pt());
389  setVal(strTrackType + "_ZPz_AfterZVtxFit", (float)ZMom_AfterZVtxFit.Pz());
390  setVal(strTrackType + "_ZPhi_AfterZVtxFit", (float)ZMom_AfterZVtxFit.Phi());
391  setVal(strTrackType + "_ZMass_AfterZVtxFit", (float)ZMom_AfterZVtxFit.M());
392  } else
393  std::cerr << "HIPTwoBodyDecayAnalyzer::analyzeTrackCollection: Z vertex fit failed for track collection "
394  << strTrackType << std::endl;
395  }
396  setVal(strTrackType + "_ZVtxFitOk", (ZVtxOk ? (short)1 : (short)0));
397  for (unsigned int jtrk = 0; jtrk < 2; jtrk++) {
398  std::string strMuCore = (jtrk == 0 ? "MuMinus" : "MuPlus");
399  setVal(strTrackType + "_" + strMuCore + "Pt", (float)trackMom[jtrk].Pt());
400  setVal(strTrackType + "_" + strMuCore + "Pz", (float)trackMom[jtrk].Pz());
401  setVal(strTrackType + "_" + strMuCore + "Phi", (float)trackMom[jtrk].Phi());
402  setVal(strTrackType + "_" + strMuCore + "Vertex_x", (float)trackVtx[jtrk].X());
403  setVal(strTrackType + "_" + strMuCore + "Vertex_y", (float)trackVtx[jtrk].Y());
404  setVal(strTrackType + "_" + strMuCore + "Vertex_z", (float)trackVtx[jtrk].Z());
405  }
406 }
407 
410  bool& fitOk) {
411  using namespace edm;
412  using namespace reco;
413 
414  std::vector<TransientTrack> t_tks;
415  for (TrackCollection::const_iterator track = hTrackColl->begin(); track != hTrackColl->end(); ++track) {
416  TransientTrack tt = theTTBuilder.build(&(*track));
417  t_tks.push_back(tt);
418  }
419 
420  // Kalman vertex fit without constraint
421  KalmanVertexFitter vtxFitter;
422  TransientVertex stdVertex = vtxFitter.vertex(t_tks);
423  fitOk = stdVertex.isValid();
424  if (fitOk) {
425  reco::Vertex stdRecoVertex(Vertex::Point(stdVertex.position()),
426  stdVertex.positionError().matrix(),
427  stdVertex.totalChiSquared(),
428  stdVertex.degreesOfFreedom(),
429  0);
430  return stdRecoVertex;
431  } else {
432  reco::Vertex stdRecoVertex;
433  return stdRecoVertex;
434  }
435 }
436 
static const std::string kSharedResource
Definition: TFileService.h:76
reco::Vertex::Point convertPos(const GlobalPoint &p)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
float totalChiSquared() const
edm::EDGetTokenT< reco::TrackCollection > refit1_trackCollToken_
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
const edm::ESGetToken< TransientTrackBuilder, TransientTrackRecord > ttkbuilderToken_
GlobalPoint position() const
T z() const
Definition: PV3DBase.h:61
GlobalError positionError() const
bool verbose
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
bool bookBranch(std::string bname, BranchType btype)
#define X(str)
Definition: MuonsGrabber.cc:38
std::vector< std::pair< std::string, short * > > shortBranches
float degreesOfFreedom() const
void analyze(const edm::Event &, const edm::EventSetup &) override
reco::Vertex fitDimuonVertex(const TransientTrackBuilder &theTTBuilder, edm::Handle< reco::TrackCollection > &hTrackColl, bool &fitOk)
void setVal(std::string bname, varType value)
CachingVertex< 5 > vertex(const std::vector< reco::TransientTrack > &tracks) const override
reco::TransientTrack build(const reco::Track *p) const
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
int iEvent
Definition: GenABIO.cc:224
Definition: TTTypes.h:54
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::EDGetTokenT< reco::TrackCollection > alcareco_trackCollToken_
bool isValid() const
T sqrt(T t)
Definition: SSEVec.h:23
const MagneticField * field() const
T mag() const
Definition: PV3DBase.h:64
math::XYZPoint Point
point in the space
Definition: Vertex.h:40
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const AlgebraicSymMatrix33 matrix() const
std::vector< std::pair< std::string, float * > > floatBranches
Definition: value.py:1
std::vector< std::pair< std::string, int * > > intBranches
HIPTwoBodyDecayAnalyzer(const edm::ParameterSet &)
BranchType searchArray(std::string branchname, int &position)
bool isValid() const
Definition: HandleBase.h:70
fixed size matrix
HLT enums.
static int position[264][3]
Definition: ReadPGInfo.cc:289
Definition: tree.py:1
edm::EDGetTokenT< reco::TrackCollection > ctf_trackCollToken_
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
void analyzeTrackCollection(std::string strTrackType, const TransientTrackBuilder &theTTBuilder, edm::Handle< reco::TrackCollection > &hTrackColl, bool verbose=false)
TrajectoryStateOnSurface impactPointState() const
edm::EDGetTokenT< reco::TrackCollection > final_trackCollToken_