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 
34 public:
35 
38 
43 
44  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
45 
46  TTree* tree;
47  std::vector<std::pair<std::string, float*>> floatBranches;
48  std::vector<std::pair<std::string, int*>> intBranches;
49  std::vector<std::pair<std::string, short*>> shortBranches;
50 
51 private:
52 
53  enum BranchType{
58  };
59 
60  virtual void beginJob() override;
61  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
62  virtual void endJob() override;
63 
64  void bookAllBranches();
65  bool bookBranch(std::string bname, BranchType btype);
66  BranchType searchArray(std::string branchname, int& position);
67  template<typename varType> void setVal(std::string bname, varType value){
68  int varposition=-1;
69  BranchType varbranchtype = searchArray(bname, varposition);
70  if (varposition==-1) std::cerr << "HIPTwoBodyDecayAnalyzer::setVal -> Could not find the branch called " << bname << "!" << std::endl;
71  else if (varbranchtype==BranchType_short_t) *(shortBranches.at(varposition).second)=value;
72  else if (varbranchtype==BranchType_int_t) *(intBranches.at(varposition).second)=value;
73  else if (varbranchtype==BranchType_float_t) *(floatBranches.at(varposition).second)=value;
74  else std::cerr << "HIPTwoBodyDecayAnalyzer::setVal -> Could not find the type " << varbranchtype << " for branch called " << bname << "!" << std::endl;
75  }
76  void cleanBranches();
77  void initializeBranches();
78  bool actuateBranches();
79 
81  std::string strTrackType,
84  bool verbose=false
85  );
89  bool& fitOk
90  );
91 
92 };
93 
95  alcareco_trackCollToken_ = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("alcarecotracks"));
96  refit1_trackCollToken_ = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("refit1tracks"));
97  ctf_trackCollToken_ = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("refit2tracks"));
98  final_trackCollToken_ = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("finaltracks"));
99 
101 
102  tree = fs->make<TTree>("TestTree", "");
103  bookAllBranches();
104 }
105 
106 
108  for (unsigned short el=0; el<shortBranches.size(); el++){
109  if (branchname==shortBranches.at(el).first){
110  position = el;
111  return BranchType_short_t;
112  }
113  }
114  for (unsigned int el=0; el<intBranches.size(); el++){
115  if (branchname==intBranches.at(el).first){
116  position = el;
117  return BranchType_int_t;
118  }
119  }
120  for (unsigned int el=0; el<floatBranches.size(); el++){
121  if (branchname==floatBranches.at(el).first){
122  position = el;
123  return BranchType_float_t;
124  }
125  }
126  return BranchType_unknown_t;
127 }
129  for (unsigned short el=0; el<shortBranches.size(); el++){
130  if (shortBranches.at(el).second!=nullptr) delete shortBranches.at(el).second;
131  shortBranches.at(el).second=nullptr;
132  }
133  shortBranches.clear();
134  for (unsigned int el=0; el<intBranches.size(); el++){
135  if (intBranches.at(el).second!=nullptr) delete intBranches.at(el).second;
136  intBranches.at(el).second=nullptr;
137  }
138  intBranches.clear();
139  for (unsigned int el=0; el<floatBranches.size(); el++){
140  if (floatBranches.at(el).second!=nullptr) delete floatBranches.at(el).second;
141  floatBranches.at(el).second=nullptr;
142  }
143  floatBranches.clear();
144 }
146  for (unsigned short el=0; el<shortBranches.size(); el++){
147  if (shortBranches.at(el).second!=nullptr) *(shortBranches.at(el).second)=0;
148  }
149  for (unsigned int el=0; el<intBranches.size(); el++){
150  if (intBranches.at(el).second!=nullptr) *(intBranches.at(el).second)=0;
151  }
152  for (unsigned int el=0; el<floatBranches.size(); el++){
153  if (floatBranches.at(el).second!=nullptr) *(floatBranches.at(el).second)=0;
154  }
155 }
156 
158  const int nTrackTypes = 4;
159  std::vector<std::string> strTrackTypes; strTrackTypes.reserve(nTrackTypes);
160  strTrackTypes.push_back("alcareco");
161  strTrackTypes.push_back("refit1");
162  strTrackTypes.push_back("refit2");
163  strTrackTypes.push_back("final");
164  for (unsigned int it=0; it<nTrackTypes; it++){
165  std::string& strTrackType = strTrackTypes[it];
166  bookBranch(strTrackType + "_present", BranchType_short_t);
167  bookBranch(strTrackType + "_ZVtxFitOk", BranchType_short_t);
168  bookBranch(strTrackType + "_ZMass", BranchType_float_t); bookBranch(strTrackType + "_ZPt", BranchType_float_t); bookBranch(strTrackType + "_ZPz", BranchType_float_t); bookBranch(strTrackType + "_ZPhi", BranchType_float_t);
169  bookBranch(strTrackType + "_ZVertex_x", BranchType_float_t); bookBranch(strTrackType + "_ZVertex_y", BranchType_float_t); bookBranch(strTrackType + "_ZVertex_z", BranchType_float_t); bookBranch(strTrackType + "_ZVertex_NormChi2", BranchType_float_t);
170  bookBranch(strTrackType + "_MuPlusVertex_x", BranchType_float_t); bookBranch(strTrackType + "_MuPlusVertex_y", BranchType_float_t); bookBranch(strTrackType + "_MuPlusVertex_z", BranchType_float_t);
171  bookBranch(strTrackType + "_MuMinusPt_AfterZVtxFit", BranchType_float_t); bookBranch(strTrackType + "_MuMinusPz_AfterZVtxFit", BranchType_float_t); bookBranch(strTrackType + "_MuMinusPhi_AfterZVtxFit", BranchType_float_t);
172  bookBranch(strTrackType + "_MuPlusPt_AfterZVtxFit", BranchType_float_t); bookBranch(strTrackType + "_MuPlusPz_AfterZVtxFit", BranchType_float_t); bookBranch(strTrackType + "_MuPlusPhi_AfterZVtxFit", BranchType_float_t);
173  bookBranch(strTrackType + "_ZMass_AfterZVtxFit", BranchType_float_t); bookBranch(strTrackType + "_ZPt_AfterZVtxFit", BranchType_float_t); bookBranch(strTrackType + "_ZPz_AfterZVtxFit", BranchType_float_t); bookBranch(strTrackType + "_ZPhi_AfterZVtxFit", BranchType_float_t);
174  bookBranch(strTrackType + "_MuMinusPt", BranchType_float_t); bookBranch(strTrackType + "_MuMinusPz", BranchType_float_t); bookBranch(strTrackType + "_MuMinusPhi", BranchType_float_t);
175  bookBranch(strTrackType + "_MuMinusVertex_x", BranchType_float_t); bookBranch(strTrackType + "_MuMinusVertex_y", BranchType_float_t); bookBranch(strTrackType + "_MuMinusVertex_z", BranchType_float_t);
176  bookBranch(strTrackType + "_MuPlusPt", BranchType_float_t); bookBranch(strTrackType + "_MuPlusPz", BranchType_float_t); bookBranch(strTrackType + "_MuPlusPhi", BranchType_float_t);
177  }
178  actuateBranches();
179 }
181  if (btype==BranchType_float_t) floatBranches.emplace_back(bname, new float);
182  else if (btype==BranchType_int_t) intBranches.emplace_back(bname, new int);
183  else if (btype==BranchType_short_t) shortBranches.emplace_back(bname, new short);
184  else{
185  std::cerr << "HIPTwoBodyDecayAnalyzer::bookBranch: No support for type " << btype << " for the branch " << bname << " is available." << std::endl;
186  return false;
187  }
188  return true;
189 }
191  bool success=true;
192  std::cout << "Begin HIPTwoBodyDecayAnalyzer::actuateBranches" << std::endl;
193  std::cout << "Number of short branches: " << shortBranches.size() << std::endl;
194  std::cout << "Number of int branches: " << intBranches.size() << std::endl;
195  std::cout << "Number of float branches: " << floatBranches.size() << std::endl;
196  if (tree!=nullptr){
197  for (unsigned short el=0; el<shortBranches.size(); el++){
198  std::cout << "Actuating branch " << shortBranches.at(el).first << " at address " << shortBranches.at(el).second << std::endl;
199  if (!tree->GetBranchStatus(shortBranches.at(el).first.c_str()))
200  tree->Branch(shortBranches.at(el).first.c_str(), shortBranches.at(el).second);
201  else std::cout << "Failed!" << std::endl;
202  }
203  for (unsigned int el=0; el<intBranches.size(); el++){
204  std::cout << "Actuating branch " << intBranches.at(el).first.c_str() << " at address " << intBranches.at(el).second << std::endl;
205  if (!tree->GetBranchStatus(intBranches.at(el).first.c_str()))
206  tree->Branch(intBranches.at(el).first.c_str(), intBranches.at(el).second);
207  else std::cout << "Failed!" << std::endl;
208  }
209  for (unsigned int el=0; el<floatBranches.size(); el++){
210  std::cout << "Actuating branch " << floatBranches.at(el).first.c_str() << " at address " << floatBranches.at(el).second << std::endl;
211  if (!tree->GetBranchStatus(floatBranches.at(el).first.c_str()))
212  tree->Branch(floatBranches.at(el).first.c_str(), floatBranches.at(el).second);
213  else std::cout << "Failed!" << std::endl;
214  }
215  }
216  else success=false;
217  if (!success) std::cerr << "HIPTwoBodyDecayAnalyzer::actuateBranch: Failed to actuate the branches!" << std::endl;
218  return success;
219 }
221  cleanBranches();
222 }
223 
225  using namespace edm;
226  using namespace reco;
227  using reco::TrackCollection;
228 
229  edm::Handle<reco::TrackCollection> alcarecotracks;
230  iEvent.getByToken(alcareco_trackCollToken_, alcarecotracks);
232  iEvent.getByToken(refit1_trackCollToken_, refit1tracks);
234  iEvent.getByToken(ctf_trackCollToken_, ctftracks);
236  iEvent.getByToken(final_trackCollToken_, finaltracks);
237 
239  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", theTTBuilder);
240 
242 
243  analyzeTrackCollection("alcareco", theTTBuilder, alcarecotracks);
244  analyzeTrackCollection("refit1", theTTBuilder, refit1tracks);
245  analyzeTrackCollection("refit2", theTTBuilder, ctftracks);
246  analyzeTrackCollection("final", theTTBuilder, finaltracks);
247 
248  tree->Fill();
249 }
250 
253 
256  desc.setUnknown();
257  descriptions.addDefault(desc);
258 }
259 
261  if (verbose) std::cout << "Starting to process the track collection for " << strTrackType << std::endl;
262 
263  using namespace edm;
264  using namespace reco;
265  using reco::TrackCollection;
266 
267  if (!hTrackColl.isValid()){
268  if (verbose) std::cout << "Track collection is invalid." << std::endl;
269  return;
270  }
271  if (hTrackColl->size()<2){
272  if (verbose) std::cout << "Track collection size<2." << std::endl;
273  return;
274  }
275 
276  unsigned int itrk=0;
277  unsigned int j=0;
278  int totalcharge=0;
279  bool isValidPair=true;
280  bool ZVtxOk=false;
281  TLorentzVector trackMom[2];
282  TLorentzVector trackMomAfterZVtxFit[2];
283  TVector3 trackVtx[2];
284 
285  for (unsigned int jtrk=0; jtrk<2; jtrk++){
286  trackMom[jtrk].SetXYZT(0, 0, 0, 0);
287  trackVtx[jtrk].SetXYZ(0, 0, 0);
288  }
289  for (reco::TrackCollection::const_iterator track = hTrackColl->begin(); track != hTrackColl->end(); ++track){
290  int charge = track->charge();
291  totalcharge += charge;
292  if (j==0){
293  itrk = (charge>0 ? 1 : 0);
294  }
295  else itrk = 1-itrk;
296  trackMom[itrk].SetPtEtaPhiM(track->pt(), track->eta(), track->phi(), 0.105);
297  trackVtx[itrk].SetXYZ(track->vx(), track->vy(), track->vz());
298  j++;
299  if (j==2) break;
300  }
301 
302  isValidPair = (totalcharge==0 && trackMom[0].P()!=0. && trackMom[1].P()!=0.);
303  if (verbose && !isValidPair) std::cout << "Track collection does not contain a valid std::pair." << std::endl;
304  setVal(strTrackType + "_present", (isValidPair ? (short)1 : (short)0));
305  if (isValidPair){
306  TLorentzVector ZMom = trackMom[0] + trackMom[1];
307  setVal(strTrackType + "_ZPt", (float)ZMom.Pt());
308  setVal(strTrackType + "_ZPz", (float)ZMom.Pz());
309  setVal(strTrackType + "_ZPhi", (float)ZMom.Phi());
310  setVal(strTrackType + "_ZMass", (float)ZMom.M());
311 
312  reco::Vertex ZVtx = fitDimuonVertex(theTTBuilder, hTrackColl, ZVtxOk);
313  if (ZVtxOk){
314  setVal(strTrackType + "_ZVertex_x", (float)ZVtx.x());
315  setVal(strTrackType + "_ZVertex_y", (float)ZVtx.y());
316  setVal(strTrackType + "_ZVertex_z", (float)ZVtx.z());
317  setVal(strTrackType + "_ZVertex_NormChi2", (float)ZVtx.normalizedChi2());
318 
319  // Recalculate track momenta with this vertex as reference
320  j=0;
321  for (reco::TrackCollection::const_iterator track = hTrackColl->begin(); track != hTrackColl->end(); ++track){
322  TransientTrack t_track = theTTBuilder->build(&(*track));
323  AnalyticalImpactPointExtrapolator extrapolator(t_track.field());
324  TrajectoryStateOnSurface closestIn3DSpaceState = extrapolator.extrapolate(t_track.impactPointState(), RecoVertex::convertPos(ZVtx.position()));
325  GlobalVector mom = closestIn3DSpaceState.globalMomentum();
326  int charge = track->charge();
327  totalcharge += charge;
328  if (j==0){
329  itrk = (charge>0 ? 1 : 0);
330  }
331  else itrk = 1-itrk;
332  trackMomAfterZVtxFit[itrk].SetXYZT(mom.x(), mom.y(), mom.z(), sqrt(pow(0.105, 2) + pow(mom.mag(), 2)));
333  j++;
334  if (j==2) break;
335  }
336  if (totalcharge!=0) std::cerr << "HIPTwoBodyDecayAnalyzer::analyzeTrackCollection: Something went wrong! The total charge is no longer 0!" << std::endl;
337  for (unsigned int jtrk=0; jtrk<2; jtrk++){
338  std::string strMuCore = (jtrk==0 ? "MuMinus" : "MuPlus");
339  setVal(strTrackType + "_" + strMuCore + "Pt_AfterZVtxFit", (float)trackMomAfterZVtxFit[jtrk].Pt());
340  setVal(strTrackType + "_" + strMuCore + "Pz_AfterZVtxFit", (float)trackMomAfterZVtxFit[jtrk].Pz());
341  setVal(strTrackType + "_" + strMuCore + "Phi_AfterZVtxFit", (float)trackMomAfterZVtxFit[jtrk].Phi());
342  }
343  TLorentzVector ZMom_AfterZVtxFit = trackMomAfterZVtxFit[0] + trackMomAfterZVtxFit[1];
344  setVal(strTrackType + "_ZPt_AfterZVtxFit", (float)ZMom_AfterZVtxFit.Pt());
345  setVal(strTrackType + "_ZPz_AfterZVtxFit", (float)ZMom_AfterZVtxFit.Pz());
346  setVal(strTrackType + "_ZPhi_AfterZVtxFit", (float)ZMom_AfterZVtxFit.Phi());
347  setVal(strTrackType + "_ZMass_AfterZVtxFit", (float)ZMom_AfterZVtxFit.M());
348  }
349  else std::cerr << "HIPTwoBodyDecayAnalyzer::analyzeTrackCollection: Z vertex fit failed for track collection " << strTrackType << std::endl;
350  }
351  setVal(strTrackType + "_ZVtxFitOk", (ZVtxOk ? (short)1 : (short)0));
352  for (unsigned int jtrk=0; jtrk<2; jtrk++){
353  std::string strMuCore = (jtrk==0 ? "MuMinus" : "MuPlus");
354  setVal(strTrackType + "_" + strMuCore + "Pt", (float)trackMom[jtrk].Pt());
355  setVal(strTrackType + "_" + strMuCore + "Pz", (float)trackMom[jtrk].Pz());
356  setVal(strTrackType + "_" + strMuCore + "Phi", (float)trackMom[jtrk].Phi());
357  setVal(strTrackType + "_" + strMuCore + "Vertex_x", (float)trackVtx[jtrk].X());
358  setVal(strTrackType + "_" + strMuCore + "Vertex_y", (float)trackVtx[jtrk].Y());
359  setVal(strTrackType + "_" + strMuCore + "Vertex_z", (float)trackVtx[jtrk].Z());
360  }
361 }
362 
364  using namespace edm;
365  using namespace reco;
366 
367  std::vector<TransientTrack> t_tks;
368  for (TrackCollection::const_iterator track = hTrackColl->begin(); track != hTrackColl->end(); ++track){
369  TransientTrack tt = theTTBuilder->build(&(*track));
370  t_tks.push_back(tt);
371  }
372 
373  // Kalman vertex fit without constraint
374  KalmanVertexFitter vtxFitter;
375  TransientVertex stdVertex = vtxFitter.vertex(t_tks);
376  fitOk = stdVertex.isValid();
377  if (fitOk){
378  reco::Vertex stdRecoVertex(
379  Vertex::Point(stdVertex.position()), stdVertex.positionError().matrix(),
380  stdVertex.totalChiSquared(), stdVertex.degreesOfFreedom(), 0
381  );
382  return stdRecoVertex;
383  }
384  else{
385  reco::Vertex stdRecoVertex;
386  return stdRecoVertex;
387  }
388 }
389 
GlobalError positionError() const
reco::Vertex::Point convertPos(const GlobalPoint &p)
T getParameter(std::string const &) const
reco::Vertex fitDimuonVertex(edm::ESHandle< TransientTrackBuilder > &theTTBuilder, edm::Handle< reco::TrackCollection > &hTrackColl, bool &fitOk)
edm::EDGetTokenT< reco::TrackCollection > refit1_trackCollToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
const AlgebraicSymMatrix33 matrix() const
virtual CachingVertex< 5 > vertex(const std::vector< reco::TransientTrack > &tracks) const
float totalChiSquared() const
void analyzeTrackCollection(std::string strTrackType, edm::ESHandle< TransientTrackBuilder > &theTTBuilder, edm::Handle< reco::TrackCollection > &hTrackColl, bool verbose=false)
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
reco::TransientTrack build(const reco::Track *p) const
T y() const
Definition: PV3DBase.h:63
bool bookBranch(std::string bname, BranchType btype)
#define X(str)
Definition: MuonsGrabber.cc:48
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
const MagneticField * field() const
virtual void endJob() override
std::vector< std::pair< std::string, short * > > shortBranches
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
void setVal(std::string bname, varType value)
int iEvent
Definition: GenABIO.cc:230
virtual void beginJob() override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
T mag() const
Definition: PV3DBase.h:67
void addDefault(ParameterSetDescription const &psetDescription)
edm::EDGetTokenT< reco::TrackCollection > alcareco_trackCollToken_
float degreesOfFreedom() const
T sqrt(T t)
Definition: SSEVec.h:18
GlobalPoint position() const
T z() const
Definition: PV3DBase.h:64
math::XYZPoint Point
point in the space
Definition: Vertex.h:39
std::vector< std::pair< std::string, float * > > floatBranches
Definition: value.py:1
bool isValid() const
Definition: HandleBase.h:74
std::vector< std::pair< std::string, int * > > intBranches
HIPTwoBodyDecayAnalyzer(const edm::ParameterSet &)
BranchType searchArray(std::string branchname, int &position)
const T & get() const
Definition: EventSetup.h:55
fixed size matrix
HLT enums.
static int position[264][3]
Definition: ReadPGInfo.cc:509
TrajectoryStateOnSurface impactPointState() const
Definition: tree.py:1
edm::EDGetTokenT< reco::TrackCollection > ctf_trackCollToken_
T x() const
Definition: PV3DBase.h:62
bool isValid() const
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
edm::EDGetTokenT< reco::TrackCollection > final_trackCollToken_