HighLAND
MomentumResolVariation.cxx
1 #include "MomentumResolVariation.hxx"
2 #include "ND280AnalysisUtils.hxx"
3 #include "EventBoxTracker.hxx"
4 #include "VersioningUtils.hxx"
5 #include "ConstituentsUtils.hxx"
6 #include "CutUtils.hxx"
7 
8 
9 //#define DEBUG true
10 
11 //********************************************************************
13  //********************************************************************
14 
15  if (!track) return;
16 
17  // Initial momenum to be varied
18  Float_t p = track->Momentum;
19 
20  // Get the true momentum
21  if (!track->TrueObject) return;
22 
23  // Then the change cannot be undone
24  if(!track->Original) return;
25 
26 
27 #ifdef DEBUG
28  std::cout << " MomentumResolVariation::ApplyVariation() - start " << std::endl;
29 #endif
30 
31  Float_t variation;
32 
33  if (!GetVariation(track, variation, exp)) return; // for prod6 only depends on coordinates
34 
35  Float_t p0_true = track->GetTrueParticle()->Momentum;
36 
37 #ifdef DEBUG
38  std::cout << " MomentumResolVariation::ApplyVariation() p0, p0_true = " << p << " " << p0_true << std::endl;
39 #endif
40 
41  // Get the inverse transverse momentum
42  Float_t pt0_inv = anaUtils::ComputeInversePT(*track);
43 
44  Float_t pt0_inv_flip = anaUtils::ComputeInversePTFlip(*track);
45 
46  if (pt0_inv < 0) return;
47 
48  // Get the true inverse transverse momentum
49  Float_t pt0_inv_true = anaUtils::ComputeInversePT(*(track->GetTrueParticle()));
50 
51 #ifdef DEBUG
52  std::cout << " MomentumResolVariation::ApplyVariation() 1/pt0, 1/pt0_true = " << pt0_inv << " " << pt0_inv_true << std::endl;
53  std::cout << " MomentumResolVariation::ApplyVariation() 1/pt0_flip = " << pt0_inv_flip << std::endl;
54  std::cout << " MomentumResolVariation::ApplyVariation() var = " << variation << std::endl;
55 #endif
56 
57  // Apply momentum resolution smearing
58  Float_t pt_inv = (1 + variation) * (pt0_inv - pt0_inv_true) + pt0_inv_true;
59 
60  // Same for the flip, since we assume this will be used as a correct one when ToF applied...
61  Float_t pt_inv_flip = (1 + variation) * (pt0_inv_flip - pt0_inv_true) + pt0_inv_true;
62 
63  // Convert back to the full momentum
64  p = fabs(anaUtils::ComputeMomentumFromInversePT( *(track), pt_inv));
65 
66  if (p < 0) return;
67 
68 
69  // Set the new momentum
70  track->Momentum = p;
71 
72  Float_t p_flip = fabs(anaUtils::ComputeMomentumFromInversePTFlip( *(track), pt_inv_flip));
73  if (p_flip > 0)
74  track->MomentumFlip = p_flip;
75 
76 
77  // Apply the smearing to the TPC constituents
78  if (track->nTPCSegments == 0) return;
79 
80 
81  Float_t p0 = track->GetOriginalTrack()->Momentum;
82 
83  // Translate the smearing on the error of the local momentum and its error
84  Float_t smearingFact = (p - p0_true)/(p0 - p0_true);
85  Float_t smearingFactMom = p / p0;
86 
87  for(int iseg = 0; iseg < track->nTPCSegments; iseg++){
88  AnaTPCParticleB* tpcTrackOrig = static_cast<const AnaTrackB*>(track->Original)->TPCSegments[iseg];
89  AnaTPCParticleB* tpcTrack = track->TPCSegments[iseg];
90  Float_t tpcmomerr = tpcTrackOrig->MomentumError;
91  Float_t tpcmom = tpcTrackOrig->Momentum;
92 
93  tpcTrack->MomentumError = tpcmomerr * smearingFact;
94  tpcTrack->Momentum = tpcmom * smearingFactMom;
95  }
96 
97 #ifdef DEBUG
98  std::cout << " MomentumResolVariation::ApplyVariation() p0 new = " << track->Momentum << std::endl;
99  std::cout << " MomentumResolVariation::ApplyVariation() p0flip new = " << track->MomentumFlip << std::endl;
100 #endif
101 
102 }
103 
104 
105 //********************************************************************
107  //********************************************************************
108 
109 #ifdef DEBUG
110  std::cout << " MomentumResolVariation::ApplyVariationTPCBased() - start " << std::endl;
111 #endif
112 
113  if (!track) return;
114 
115  // Get the true momentum
116  if (!track->TrueObject) return;
117 
118  // Then the change cannot be undone
119  if(!track->Original) return;
120 
121 #ifdef DEBUG
122  Float_t p0_true = track->GetTrueParticle()->Momentum;
123  std::cout << " MomentumResolVariation::ApplyVariationTPCBased() p0, p0_true = " << track->Momentum << " " << p0_true << std::endl;
124  std::cout << " MomentumResolVariation::ApplyVariationTPCBased() p0flip, p0_true = " << track->MomentumFlip << " " << p0_true << std::endl;
125 #endif
126 
127  // Do nothing if there are no TPC constituents
128  if (track->nTPCSegments == 0) return;
129 
130  // Smear each constituent separately
131  for (int iseg = 0; iseg < track->nTPCSegments; iseg++){
132  if (!track->TPCSegments[iseg]) continue;
133 
134  // Smear the TPC momentum
135  ApplyVariationTPC(track->TPCSegments[iseg], exp);
136  }
137 
138  // Using the TPC closest to the starting point smear the global momentum
139  AnaParticleMomB* tpcStart = static_cast<AnaParticleMomB*>(
141 
142  if (tpcStart)
143  // Retrieve the original momentum and apply the differnece to the one of the global track
144  if (tpcStart->Original)
145  track->Momentum += tpcStart->Momentum - static_cast<const AnaParticleMomB*>(tpcStart->Original)->Momentum ;
146 
147 
148  // Using the TPC closest to the end point smear the flip momentum
149  AnaParticleMomB* tpcEnd = static_cast<AnaParticleMomB*>(
151  if (tpcEnd)
152  // Retrieve the original momentum and apply the differnece to the one of the global track
153  if (tpcEnd->Original)
154  track->MomentumFlip += tpcEnd->Momentum - static_cast<const AnaParticleMomB*>(tpcEnd->Original)->Momentum ;
155 
156 #ifdef DEBUG
157  std::cout << " MomentumResolVariation::ApplyVariationTPCBased() p0 new = " << track->Momentum << std::endl;
158  std::cout << " MomentumResolVariation::ApplyVariationTPCBased() p0flip new = " << track->MomentumFlip << std::endl;
159 #endif
160 
161 }
162 
163 
164 
165 //********************************************************************
167  //********************************************************************
168 
169  if (!track) return;
170 
171  // Initial momenum to be varied
172  Float_t p = track->Momentum;
173 
174  // Get the true momentum
175  if (!track->TrueObject) return;
176 
177  // Then the change cannot be undone
178  if(!track->Original) return;
179 
180  Float_t variation;
181 
182  if (!GetVariationTPC(track, variation, exp)) return;
183 
185  if (!SubDetId::IsTPCDetector(tpc_det)) return;
186 
187 
188  AnaTrueParticleB* trueTrack = static_cast<AnaTrueParticleB*>(track->TrueObject);
189 
190  if (!trueTrack) return;
191 
192 
193  AnaDetCrossingB* cross = anaUtils::GetAnaDetCrossing(trueTrack, tpc_det);
194 
195  if (!cross)
196  return;
197 
198  Float_t p0_true = anaUtils::GetEntranceMomentum(*cross);
199 
200 #ifdef DEBUG
201  std::cout << " MomentumResolVariation::ApplyVariationTPC() p0, p0_true = " << p << " " << p0_true << std::endl;
202 #endif
203 
204  // Get the inverse transverse momentum
205  Float_t pt0_inv = anaUtils::ComputeInversePT(*track);
206 
207 
208  if (pt0_inv < 0) return;
209 
210  // Get the true inverse transverse momentum
211  Float_t pt0_inv_true = anaUtils::ComputeInversePT(*cross);
212 
213 #ifdef DEBUG
214  std::cout << " MomentumResolVariation::ApplyVariationTPC() 1/pt0, 1/pt0_true = " << pt0_inv << " " << pt0_inv_true << std::endl;
215  std::cout << " MomentumResolVariation::ApplyVariationTPC() var = " << variation << std::endl;
216 #endif
217 
218  // Apply momentum resolution smearing
219  Float_t pt_inv = (1 + variation) * (pt0_inv - pt0_inv_true) + pt0_inv_true;
220 
221  // Convert back to the full momentum
222  p = fabs(anaUtils::ComputeMomentumFromInversePT( *(track), pt_inv));
223 
224  if (p < 0) return;
225 
226 
227  // Set the new momentum
228  track->Momentum = p;
229 
230 #ifdef DEBUG
231  std::cout << " MomentumResolVariation::ApplyVariationTPC() p_err = " << track->MomentumError << " " << std::endl;
232 #endif
233 
234  Float_t p0 = static_cast<const AnaParticleMomB*>(track->Original)->Momentum;
235 
236  if (p0 - p0_true != 0)
237  track->MomentumError *= (p - p0_true)/(p0 - p0_true);
238 
239 #ifdef DEBUG
240  std::cout << " MomentumResolVariation::ApplyVariationTPC() p = " << p << " " << std::endl;
241  std::cout << " MomentumResolVariation::ApplyVariationTPC() p_err = " << track->MomentumError << " " << std::endl;
242 #endif
243 
244  return;
245 }
246 
247 
248 
249 
250 //********************************************************************
252  AnaTrackB* track,
253  Float_t& value1,
254  Float_t& value2,
255  Int_t& index1,
256  Int_t& index2,
257  ModeEnum mode){
258  //********************************************************************
259 
260  if (!track) return false;
261 
262  if (!_params) return false;
263 
264  // Use a x dependence for the moment for prod6
265  Float_t x_zmin = -10000;
266  Float_t x_zmax = -10000;
267  Float_t zmin = 10000;
268  Float_t zmax = -10000;
269 
270  // Get array of TPC segments, but only one per TPC, the one with more nodes
271  AnaTPCParticleB* TPCSegments[3];
272  Int_t nTPCSegments = anaUtils::GetOneSegmentPerTPC(track->TPCSegments, track->nTPCSegments, TPCSegments);
273 
274  // Loop over TPC segments
275  for (int k = 0; k < nTPCSegments; k++) {
276  AnaTPCParticleB* tpcTrack = TPCSegments[k];
277 
278  // Find the Most upstream TPC point in the global track
279  if (tpcTrack->PositionStart[2]<zmin){
280  x_zmin = tpcTrack->PositionStart[0];
281  zmin = tpcTrack->PositionStart[2];
282  }
283  // Find the Most downstream TPC point in the global track
284  if (tpcTrack->PositionEnd[2]>zmax){
285  x_zmax = tpcTrack->PositionEnd[0];
286  zmax = tpcTrack->PositionEnd[2];
287  }
288  }
289 
290 #ifdef DEBUG
291  std::cout <<" MomentumResolVariation::GetXBinnedValues() mode " << mode
292  << " bin: " << x_zmin << " " << x_zmax << " " << zmin << " " << zmax
293  << std::endl;
294 #endif
295 
296  return GetXBinnedValues(x_zmin, x_zmax, value1, value2, index1, index2, mode);
297 
298 }
299 
300 
301 //********************************************************************
303  AnaTPCParticleB* tpcTrack,
304  Float_t& value1,
305  Float_t& value2,
306  Int_t& index1,
307  Int_t& index2,
308  ModeEnum mode){
309  //********************************************************************
310 
311  if (!tpcTrack) return false;
312 
313  if (!_params) return false;
314 
315  // Use a x dependence for the moment for prod6
316  Float_t xmin = tpcTrack->PositionStart[0];
317  Float_t xmax = tpcTrack->PositionEnd[0];
318 
319  return GetXBinnedValues(xmin, xmax, value1, value2, index1, index2, mode);
320 
321 }
322 
323 //**************************************************
325  //**************************************************
326 
327  Float_t dist = 9999999.;
328 
329  AnaParticleB* tpcTrack = NULL;
330 
331  for(int i = 0; i < track.nTPCSegments; ++i){
332  AnaTPCParticleB* tpc_track_tmp = track.TPCSegments[i];
333 
334  if (!tpc_track_tmp) continue;
335 
336  if (!cutUtils::TPCTrackQualityCut(*tpc_track_tmp)) continue;
337 
338  Float_t dist_tmp = std::min(
339  anaUtils::GetSeparationSquared(pos, tpc_track_tmp->PositionStart),
340  anaUtils::GetSeparationSquared(pos, tpc_track_tmp->PositionEnd)
341  );
342 
343  if (dist_tmp < dist){
344  dist = dist_tmp;
345  tpcTrack = tpc_track_tmp;
346  }
347  }
348 
349  return tpcTrack;
350 }
351 
352 
353 //**************************************************
354 bool MomentumResolVariation::GetXBinnedValues(Float_t xmin, Float_t xmax,
355  Float_t& value1, Float_t& value2,
356  Int_t& index1, Int_t& index2,
357  ModeEnum mode){
358 //**************************************************
359 
360  // A protection against crazy z values leading to x values not found
361  if (xmin < -1000 || xmax < -1000) return false;
362 
363 
364  Float_t val1_tmp, val2_tmp;
365 
366  // Get parameters for upstream point
367  bool ok1 = mode == kCorr ? _params->GetBinValues(xmin, value1, val1_tmp, index1) :
368  _params->GetBinValues(xmin, val1_tmp, value1, index1);
369 
370  // Get parameters for downstream point
371  bool ok2 = mode == kCorr ? _params->GetBinValues(xmax, value2, val2_tmp, index2) :
372  _params->GetBinValues(xmax, val2_tmp, value2, index2);
373 
374 
375 #ifdef DEBUG
376  std::cout <<" MomentumResolVariation::GetXBinnedValues() " << mode
377  << " bin: " << xmin << " " << xmax
378  << " " << value1 << " " << value2 << std::endl;
379 #endif
380 
381  if (ok1 && ok2){
382  return true;
383  }
384  else if (ok1 && !ok2){
385  value2 = value1;
386  index2 = index1;
387  return true;
388  }
389  else if (!ok1 && ok2){
390  value1 = value2;
391  index1 = index2;
392  return true;
393  }
394  else
395  return false;
396 
397 
398 
399 
400 }
virtual void ApplyVariation(AnaTrackB *track, const ToyExperiment &exp)
const AnaTrackB * GetOriginalTrack() const
Return a casted version of the original AnaParticleB associated.
Float_t PositionStart[4]
The reconstructed start position of the particle.
unsigned long Detector
virtual bool GetVariationTPC(AnaTPCParticleB *track, Float_t &variation, const ToyExperiment &exp)
Abstract class to get the variation given a TPC object.
/// Extension to AnaParticleB containing momentum and charge info
AnaTPCParticleB * TPCSegments[NMAXTPCS]
The TPC segments that contributed to this global track.
Float_t ComputeMomentumFromInversePT(const AnaParticleB &part, Float_t PTinv)
compute the total momentum given the part and the inverse transverse momentum
AnaTrueObjectC * TrueObject
The link to the true oject that most likely generated this reconstructed object.
Float_t Momentum
The initial momentum of the true particle.
float GetSeparationSquared(const Float_t *pos1, const Float_t *pos2)
Calculate the distance between two points.
static AnaParticleB * GetClosestTPCSegmentWithGoodDQ(const AnaTrackB &track, const Float_t *pos)
Get TPC segment that satisfies DQ and is closest.
bool GetBinValues(Float_t value, Float_t &mean, Float_t &sigma)
Gets the bin values for a 1D source.
Float_t MomentumError
Error of the momentum at the start of the segment.
Float_t MomentumFlip
Momentum for the main PID hypothesis and reverse sense.
Float_t Momentum
The reconstructed momentum of the particle, at the start position.
int nTPCSegments
How many TPC tracks are associated with this track.
Representation of a true Monte Carlo trajectory/particle.
virtual void ApplyVariationTPCBased(AnaTrackB *track, const ToyExperiment &exp)
Float_t GetEntranceMomentum(const AnaDetCrossingB &cross)
Get the momentum value from AnaDetCrossing.
Definition: TruthUtils.cxx:451
Float_t ComputeInversePT(const AnaDetCrossingB &cross, bool entrance=true)
Compute inverse PT given an AnaDetCrossing.
const AnaParticleB * Original
static bool IsTPCDetector(SubDetId::SubDetEnum det)
Check if a detector enumeration refers to a TPC or not.
Definition: SubDetId.cxx:122
Float_t ComputeMomentumFromInversePTFlip(const AnaParticleB &part, Float_t PTinv)
Compute the total momentum (flip) given the part and the inverse transverse momentum.
Representation of a detector crossing info for a true particle (G4 trajectory).
SubDetEnum
Enumeration of all detector systems and subdetectors.
Definition: SubDetId.hxx:25
Representation of a global track.
AnaDetCrossingB * GetAnaDetCrossing(const AnaTrueParticleB *track, SubDetId::SubDetEnum det)
Definition: TruthUtils.cxx:422
virtual void ApplyVariationTPC(AnaTPCParticleB *track, const ToyExperiment &exp)
Smear TPC track based on the corresponding true track.
static SubDetId::SubDetEnum GetSubdetectorEnum(unsigned long BitField)
Get the single subdetector that this track is from.
Definition: SubDetId.cxx:165
Representation of a TPC segment of a global track.
bool GetXBinnedValues(AnaTrackB *track, Float_t &value1, Float_t &value2, Int_t &index1, Int_t &index2, ModeEnum mode)
Get parameters for this global track assumed one uses X bins.
virtual bool GetVariation(AnaTrackB *track, Float_t &variation, const ToyExperiment &exp)=0
Abstract class to get the variation given a track.
AnaTrueParticleB * GetTrueParticle() const
Return a casted version of the AnaTrueObject associated.
Representation of a reconstructed particle (track or shower).
Float_t PositionEnd[4]
The reconstructed end position of the particle.
BinnedParams * _params
Binned data to read the parameters.