HighLAND
TPCPIDSystematics.cxx
1 #include "TPCPIDSystematics.hxx"
2 #include "ND280AnalysisUtils.hxx"
3 #include "EventBoxTracker.hxx"
4 #include <cassert>
5 #include "Parameters.hxx"
6 
7 //#define DEBUG
8 
9 //********************************************************************
11  //********************************************************************
12 
13  // Read the systematic source parameters from the data files
14  _sigma[TPCPIDVariation::kMuon] = new BinnedParams("TPCPIDMuonSigRatio", BinnedParams::k2D_SYMMETRIC, versionUtils::Extension());
15  _sigma[TPCPIDVariation::kElectron] = new BinnedParams("TPCPIDEleSigRatio", BinnedParams::k2D_SYMMETRIC, versionUtils::Extension());
16  _sigma[TPCPIDVariation::kProton] = new BinnedParams("TPCPIDProtonSigRatio", BinnedParams::k2D_SYMMETRIC, versionUtils::Extension());
17 
18  _mean[TPCPIDVariation::kMuon] = new BinnedParams("TPCPIDMuonMeanDiff", BinnedParams::k2D_SYMMETRIC, versionUtils::Extension());
19  _mean[TPCPIDVariation::kElectron] = new BinnedParams("TPCPIDEleMeanDiff", BinnedParams::k2D_SYMMETRIC, versionUtils::Extension());
20  _mean[TPCPIDVariation::kProton] = new BinnedParams("TPCPIDProtonMeanDiff", BinnedParams::k2D_SYMMETRIC, versionUtils::Extension());
21 
22  // Offset for mean_err vars
23  _offset = 0;
24 
25  UInt_t nbins = 0;
26 
27  for (UInt_t i = 0; i < TPCPIDVariation::kNPULLS; i++){
28  if (!_mean[i] && !_sigma[i]) continue;
29 
30  // For the moment want both to exist
31  assert(_mean[i]);
32  assert(_sigma[i]);
33 
34  nbins += _mean[i]->GetNBins();
35 
36  _offset += _sigma[i]->GetNBins();
37  nbins += _sigma[i]->GetNBins();
38  }
39 
40  SetNParameters(nbins);
41 
42  _full_correlations = ND::params().GetParameterI("psycheSystematics.Tracker.FullCorrelations");
43 }
44 
45 //********************************************************************
47  //********************************************************************
48 
49  // Get the SystBox for this event
50  SystBoxB* box = GetSystBox(event);
51 
52 #ifdef DEBUG
53  std::cout << "TPCPIDSystematics::ApplyVariation(): tracks size " << box->nRelevantRecObjects << std::endl;
54 #endif
55 
56  // Loop over tracks and save the relevent ones for this systematic (all for the moment)
57  for (Int_t itrk = 0; itrk < box->nRelevantRecObjects; itrk++){
58 
59  AnaTrackB* track = static_cast<AnaTrackB*>(box->RelevantRecObjects[itrk]);
60 
61  ApplyVariation(track, toy);
62  }
63 }
64 
65 //********************************************************************
67  //********************************************************************
68 
69  // Get the SystBox for this event
70  SystBoxB* box = GetSystBox(event);
71 
72  for (Int_t itrk=0;itrk<box->nRelevantRecObjects;itrk++){
73  for (Int_t k = 0; k < static_cast<AnaTrackB*>(box->RelevantRecObjects[itrk])->nTPCSegments; k++) {
74  // The new TPC track
75  AnaTPCParticleB* tpcTrack = static_cast<AnaTrackB*>(box->RelevantRecObjects[itrk])->TPCSegments[k];
76  // The corrected TPC track
77  const AnaTPCParticleB* original = static_cast<const AnaTPCParticleB*>(tpcTrack->Original);
78  if (!original) continue;
79  if (original->dEdxMeas == -99999) continue;
80 
81  tpcTrack->dEdxMeas = original->dEdxMeas;
82  }
83  }
84 
85  // Don't reset the spill to corrected
86  return false;
87 }
88 
89 //********************************************************************
91  Float_t& mean_var, Float_t& sigma_var,
92  const AnaTrackB& track, const ToyExperiment& toy){
93  //********************************************************************
94 
95  // Get the TPC
96  int tpc = SubDetId::GetTPC(tpcTrack.Detector);
97 
98  // Need true particle
99  if (!track.GetTrueParticle()) return false;
100 
101  // Get the expected dEdx and error on the dEdx depending on the true particle of the
102  // (global) track
103 
104  Int_t mean_index;
105  Int_t sigma_index;
106 
107  Int_t PDG = abs(track.GetTrueParticle()->PDG);
108 
110 
111  switch (PDG){
112  case 13: // Muon
113  part = TPCPIDVariation::kMuon;
114  break;
115  case 211: // Pion
116  part = TPCPIDVariation::kMuon; // treated with same errors as muon
117  break;
118  case 11: // Electron
119  part = TPCPIDVariation::kElectron;
120  break;
121  case 2212: // Proton
122  part = TPCPIDVariation::kProton;
123  break;
124  default:
125  return false;
126  break;
127  }
128 
129  if (!_mean[part] || !_sigma[part]) return false;
130 
131  // We need the errors part of the data file but as well the relative uncertainty for sigma
132  Float_t mean_corr, sigma_corr;
133 
134  // Note that the momentum changes if the mom resoltion, scale and bfield are also anabled.
135  if (!_mean[part]->GetBinValues(track.Momentum, (Float_t)tpc, mean_corr, mean_var, mean_index)) return false;
136  if (!_sigma[part]->GetBinValues(track.Momentum, (Float_t)tpc, sigma_corr, sigma_var, sigma_index)) return false;
137 
138  // Offset for the mean variations
139  mean_index += _offset;
140 
141  for (Int_t i = 0; i < part; i++){
142  if (_mean[i])
143  mean_index += _mean[i]->GetNBins();
144  if (_sigma[i])
145  sigma_index += _sigma[i]->GetNBins();
146  }
147 
148  // override to ensure same variations for all params (see bugzilla 1271)
149  // except for particles bin (as of TN 212)
150  if (_full_correlations) {
151  mean_index = part;
152  sigma_index = part;
153  }
154 
155  mean_var *= toy.GetToyVariations(_index)->Variations[mean_index];
156  sigma_var *= toy.GetToyVariations(_index)->Variations[sigma_index];
157  sigma_var *= (sigma_corr != 0) ? 1. / sigma_corr : 0.;
158 
159  //TMP
160  //mean_var += -1. * mean_corr * sigma_var;
161 
162  sigma_var += 1.;
163 
164  return true;
165 }
166 
167 
168 
169 //**************************************************
170 bool TPCPIDSystematics::IsRelevantRecObject(const AnaEventC& event, const AnaRecObjectC& track) const{
171  //**************************************************
172 
173  (void)event;
174 
175  // True track should always exist
176  if (!track.TrueObject) return false;
177 
178  AnaTrueParticleB* truePart = static_cast<AnaTrueParticleB*>(track.TrueObject);
179 
180  // only consider true protons, pions, muons and electrons
181  if (abs(truePart->PDG) == 211 ) return true;
182  else if (abs(truePart->PDG) == 2212) return true;
183  else if (abs(truePart->PDG) == 13) return true;
184  else if (abs(truePart->PDG) == 11) return true;
185 
186  return false;
187 }
188 
189 //********************************************************************
191  //********************************************************************
192 
193  Int_t ngroups=0;
194  for (UInt_t b=0; b<sel.GetNBranches(); b++){
195  SubDetId_h det = sel.GetDetectorFV(b);
196  if (det == SubDetId::kFGD1 || det == SubDetId::kFGD){
197  IDs[ngroups++] = EventBoxTracker::kTracksWithTPCInFGD1FV;
198  }
199  if (det == SubDetId::kFGD2 || det == SubDetId::kFGD){
200  IDs[ngroups++] = EventBoxTracker::kTracksWithTPCInFGD2FV;
201  }
202  if (det == SubDetId::kFGD2 || det == SubDetId::kFGD){
203  IDs[ngroups++] = EventBoxTracker::kTracksWithTPCInP0DFV;
204  }
205  }
206 
207  return ngroups;
208 }
209 
Int_t _index
The index of this systematic (needed by SystematicsManager);.
bool GetVariation(const AnaTPCParticleB &tpcTrack, Float_t &mean_var, Float_t &sigma_var, const AnaTrackB &track, const ToyExperiment &toy)
Get the variation for a given TPC object.
unsigned long Detector
Float_t * Variations
the vector of Variations, one for each of the systematic parameters
int GetParameterI(std::string)
Get parameter. Value is returned as integer.
Definition: Parameters.cxx:217
virtual void ApplyVariation(AnaTrackB *track, const ToyExperiment &exp)
Apply variation for a track, the most general case given a certain ToyExperiment. ...
SystBoxB * GetSystBox(const AnaEventC &event, Int_t isel=0, Int_t ibranch=0) const
Get the SystBox corresponding to a selection, branch and event.
void SetNParameters(int N)
Set the number of systematic parameters associated to this systematic.
AnaTrueObjectC * TrueObject
The link to the true oject that most likely generated this reconstructed object.
bool IsRelevantRecObject(const AnaEventC &event, const AnaRecObjectC &track) const
Is this track relevant for this systematic ?
BinnedParams * _mean[kNPULLS]
Int_t GetRelevantRecObjectGroups(const SelectionBase &sel, Int_t *IDs) const
Get the TrackGroup IDs array for this systematic.
HypEnum
Enum for particle hypothesis.
Float_t Momentum
The reconstructed momentum of the particle, at the start position.
Representation of a true Monte Carlo trajectory/particle.
const AnaParticleB * Original
Int_t PDG
The PDG code of this particle.
bool UndoSystematic(AnaEventC &event)
Undo the systematic variations done by ApplyVariation. This is faster tha reseting the full Spill...
Float_t dEdxMeas
dE/dx as measured by the TPC.
Representation of a global track.
Representation of a TPC segment of a global track.
ToyVariations * GetToyVariations(UInt_t index) const
returns the variations for a given systematic (index)
virtual void Apply(const ToyExperiment &toy, AnaEventC &event)
Apply the systematic.
UInt_t GetNBranches() const
Return the number of branches.
bool _full_correlations
value of psycheSystematics.Tracker.FullCorrelations parameter
static int GetTPC(unsigned long BitField)
Definition: SubDetId.cxx:74
Int_t nRelevantRecObjects
----—— Relevant rec objects and true objects for each systematic ------------—— ...
Definition: SystBoxB.hxx:20
AnaTrueParticleB * GetTrueParticle() const
Return a casted version of the AnaTrueObject associated.
Int_t GetNBins()
Get the number of bins.
SubDetId_h GetDetectorFV(Int_t ibranch=0) const
Get the detector in which the Fiducial Volume is defined.