HighLAND
OOFVSystematics.cxx
1 #include "OOFVSystematics.hxx"
2 #include "ND280AnalysisUtils.hxx"
3 #include "ToyBoxTracker.hxx"
4 #include "BasicUtils.hxx"
5 #include "SubDetId.hxx"
6 #include "FiducialVolumeDefinition.hxx"
7 #include "VersioningUtils.hxx"
8 #include "SystId.hxx"
9 
10 //********************************************************************
11 OOFVSystematics::OOFVSystematics():EventWeightBase(1){
12 //********************************************************************
13 
14  int npars=0;
15  if (!versionUtils::prod6_systematics){
16  _fgd1 = new BinnedParams("OOFVFGD1",BinnedParams::k2D_SYMMETRIC,versionUtils::Extension());
17  npars=_fgd1->GetNBins();
18  _fgd2 = new BinnedParams("OOFVFGD2",BinnedParams::k2D_SYMMETRIC,versionUtils::Extension());
19  npars+=_fgd2->GetNBins();
20  SetNParameters(npars);
21  }
22  else{
23  _fgd1 = new BinnedParams("OOFV_reco_fgd1",BinnedParams::k1D_SYMMETRIC,versionUtils::Extension());
24  npars=_fgd1->GetNBins();
25  _fgd2 = new BinnedParams("OOFV_reco_fgd2",BinnedParams::k1D_SYMMETRIC,versionUtils::Extension());
26  npars+=_fgd2->GetNBins();
27  _rate = new BinnedParams("OOFV_rate",BinnedParams::k2D_SYMMETRIC,versionUtils::Extension());
28  npars+=_rate->GetNBins();
29  SetNParameters(npars);
30  }
31 
32  if (!versionUtils::prod6_systematics){
33  for (int i=0;i<9;i++){
34  if (!_fgd1->GetBinValues(i, 0, _rate_corr[0][i], _rate_error[0][i],_rate_index[0][i])) _reco_index[0][i]=-1;
35  if (!_fgd1->GetBinValues(i, 1, _reco_corr[0][i], _reco_error[0][i],_reco_index[0][i])) _rate_index[0][i]=-1;
36  if (!_fgd2->GetBinValues(i, 0, _rate_corr[1][i], _rate_error[1][i],_rate_index[1][i])) _reco_index[1][i]=-1;
37  if (!_fgd2->GetBinValues(i, 1, _reco_corr[1][i], _reco_error[1][i],_reco_index[1][i])) _rate_index[1][i]=-1;
38  }
39  }
40  else{
41  for (int i=0;i<9;i++){
42  if (!_fgd1->GetBinValues(i, _reco_corr[0][i], _reco_error[0][i],_reco_index[0][i])) _reco_index[0][i]=-1;
43  if (!_fgd2->GetBinValues(i, _reco_corr[1][i], _reco_error[1][i],_reco_index[1][i])) _reco_index[1][i]=-1;
44 
45  if (_reco_index[0][i]>=0) _reco_index[0][i] += _rate->GetNBins();
46  if (_reco_index[1][i]>=0) _reco_index[1][i] += _rate->GetNBins();
47  }
48  }
49 
50 }
51 
52 //********************************************************************
53 Int_t OOFVSystematics::GetBeamNumber(Int_t runperiod,AnaTrackB *maintrack){
54 //********************************************************************
55  if(runperiod==8){
56  if(maintrack->Charge<0)
57  return 1;
58  else
59  return 2;
60  }else
61  return 0;
62 }
63 //********************************************************************
64 Int_t OOFVSystematics::GetDetNumber(SubDetId::SubDetEnum det){
65 //********************************************************************
66 
68  return 0;
69  else if(SubDetId::IsECALDetector(det))
70  return 1;
71  else if(SubDetId::IsTECALDetector(det))
72  return 1;
73  else if(SubDetId::IsPECALDetector(det))
74  return 1;
75  else if(SubDetId::IsSMRDDetector(det))
76  return 2;
77  else if(SubDetId::IsTPCDetector(det))
78  return -1;
79  else if(SubDetId::IsFGDDetector(det))
80  return -1;
81  else
82  return 3;
83 }
84 
85 //********************************************************************
86 Weight_h OOFVSystematics::ComputeWeight(const ToyExperiment& toy, const AnaEventC& eventBB, const ToyBoxB& boxB, const SelectionBase& sel){
87 //********************************************************************
88 
89  const AnaEventB& event = *static_cast<const AnaEventB*>(&eventBB);
90 
91  // Cast the ToyBox to the appropriate type
92  const ToyBoxTracker& box = *static_cast<const ToyBoxTracker*>(&boxB);
93 
94  (void)event;
95 
96  Weight_h eventWeight=1;
97  if (!box.MainTrack) return eventWeight; // HMN track should exist
98  if (!box.MainTrack->TrueObject) return eventWeight; // True track associated to HMN track should exist
99  if (!box.MainTrack->GetTrueParticle()->TrueVertex) return eventWeight; // True vertex associated to HMN track should exist
100 
101  // Get the true vertex position
102  Float_t* tvertex = box.MainTrack->GetTrueParticle()->TrueVertex->Position;
103 
104  // if the true vertex is inside the FGD FV this is not OOFV (RETURN EVENTWEIGHT=1)
105  if(anaUtils::InFiducialVolume(static_cast<SubDetId::SubDetEnum>(box.DetectorFV), tvertex)) return eventWeight;
106 
107  // Get the true track direction and position
108  Float_t* tdir = box.MainTrack->GetTrueParticle()->Direction;
109  Float_t* pos = box.MainTrack->GetTrueParticle()->Position;
110 
111  Float_t* fgd_det_min;
112  Float_t* fgd_det_max;
113  double Zmin_lastmodule,zup,zdown;
114  bool lastmodule;
115  Int_t fgd;
116  if(box.DetectorFV == SubDetId::kFGD1){
117  fgd=0;
118  fgd_det_min = DetDef::fgd1min;
119  fgd_det_max = DetDef::fgd1max;
120  Zmin_lastmodule = fgd_det_max[2]-DetDef::fgdXYModuleWidth;
121  zup =-60;
122  zdown = 623;
123  }
124  else if(box.DetectorFV == SubDetId::kFGD2){
125  fgd=1;
126  fgd_det_min = DetDef::fgd2min;
127  fgd_det_max = DetDef::fgd2max;
128  Zmin_lastmodule = 1780;
129  zup = 1298;
130  zdown = 1983.095;
131  }
132  else return eventWeight;
133 
134  lastmodule = ( pos[2] >Zmin_lastmodule && pos[2] < fgd_det_max[2] );
135 
136  //sense of the track defined by the selection (true=FWD/HAFWD or false=BWD/HABWD).
137  bool fwdsense = sel.IsRelevantRecObjectForSystematicInToy(eventBB, boxB, box.MainTrack, SystId::kOOFV, boxB.SuccessfulBranch);
138 
139  Int_t categ =-1;
140 
141  if (fwdsense) {
142  //inFGD1scint. Put z condition first to be faster
143  if ((tvertex[2] > fgd_det_min[2] && tvertex[2] < fgd_det_max[2]) &&
144  (tvertex[0] > fgd_det_min[0] && tvertex[0] < fgd_det_max[0]) &&
145  (tvertex[1] > fgd_det_min[1] && tvertex[1] < fgd_det_max[1]))
146  categ=0;
147  //upstreamFGD1scint
148  else if ((tvertex[2] > zup && tvertex[2] < fgd_det_min[2]) &&
149  (tvertex[0] > -1335 && tvertex[0] < 1335) &&
150  (tvertex[1] > -1280.5 && tvertex[1] < 1280.5))
151  categ=1;
152  //downstreamFGD1scint
153  else if((tvertex[2] > fgd_det_max[2] && tvertex[2] < zdown) &&
154  (tvertex[0] > -1335 && tvertex[0] < 1335) &&
155  (tvertex[1] > -1280.5 && tvertex[1] < 1280.5))
156  categ=2;
157  //neutralparent
158  else if( box.MainTrack->GetTrueParticle()->ParentPDG == 2112 || box.MainTrack->GetTrueParticle()->ParentPDG == 22 ||
159  box.MainTrack->GetTrueParticle()->GParentPDG == 2112 || box.MainTrack->GetTrueParticle()->GParentPDG == 22)
160  categ=3;
161  //backwards
162  else if(tdir[2]<=-0.5)
163  categ=4;
164  //highangle
165  else if(tdir[2]>-0.5 && tdir[2]<0.5)
166  categ=5;
167  //verylowangle
168  else if(fabs(tdir[0]/tdir[2])<0.07 || fabs(tdir[1]/tdir[2])<0.07)
169  categ=6;
170  //lastmodule
171  else if ( lastmodule && anaUtils::InDetVolume(static_cast<SubDetId::SubDetEnum>(box.DetectorFV), box.MainTrack->PositionStart))
172  categ=7;
173  else
174  categ=8;
175  }
176  else {
177  //inFGD1scint. Put z condition first to be faster
178  if ((tvertex[2] > fgd_det_min[2] && tvertex[2] < fgd_det_max[2]) &&
179  (tvertex[0] > fgd_det_min[0] && tvertex[0] < fgd_det_max[0]) &&
180  (tvertex[1] > fgd_det_min[1] && tvertex[1] < fgd_det_max[1]))
181  categ=0;
182  //downstreamFGD1scint
183  else if((tvertex[2] > fgd_det_max[2] && tvertex[2] < zdown) &&
184  (tvertex[0] > -1335 && tvertex[0] < 1335) &&
185  (tvertex[1] > -1280.5 && tvertex[1] < 1280.5))
186  categ=1;
187  //upstreamFGD1scint
188  else if ((tvertex[2] > zup && tvertex[2] < fgd_det_min[2]) &&
189  (tvertex[0] > -1335 && tvertex[0] < 1335) &&
190  (tvertex[1] > -1280.5 && tvertex[1] < 1280.5))
191  categ=2;
192  //neutralparent
193  else if( box.MainTrack->GetTrueParticle()->ParentPDG == 2112 || box.MainTrack->GetTrueParticle()->ParentPDG == 22 ||
194  box.MainTrack->GetTrueParticle()->GParentPDG == 2112 || box.MainTrack->GetTrueParticle()->GParentPDG == 22)
195  categ=3;
196  //forwards
197  else if(tdir[2]>=0.5)
198  categ=4;
199  //highangle
200  else if(tdir[2]>-0.5 && tdir[2]<0.5)
201  categ=5;
202  //verylowangle
203  else if(fabs(tdir[0]/tdir[2])<0.07 || fabs(tdir[1]/tdir[2])<0.07)
204  categ=6;
205  //lastmodule
206  else if ( lastmodule && anaUtils::InDetVolume(static_cast<SubDetId::SubDetEnum>(box.DetectorFV), box.MainTrack->PositionStart))
207  categ=7;
208  else
209  categ=8;
210  }
211 
212  // Note that this is wrong for layer28-29, veryhighangle and veryforward,
213  // for those one matching reco should be rerun to understand how reco is changed, by now it is applied as a rate uncertainty... but....
214  if(categ>=0 ){
215  if (!versionUtils::prod6_systematics){
216  eventWeight.Systematic *= (1+ _reco_corr[fgd][categ] + _reco_error[fgd][categ]*toy.GetToyVariations(_index)->Variations[_reco_index[fgd][categ]]);
217  eventWeight.Systematic *= (1+ _rate_corr[fgd][categ] + _rate_error[fgd][categ]*toy.GetToyVariations(_index)->Variations[_rate_index[fgd][categ]]);
218 
219  eventWeight.Correction *= (1+ _reco_corr[fgd][categ]);
220  eventWeight.Correction *= (1+ _rate_corr[fgd][categ]);
221 
222  }
223  else{
225  Int_t runPeriod = anaUtils::GetRunPeriod(event.EventInfo.Run);
226 
227  if (!_rate->GetBinValues(GetBeamNumber(runPeriod,box.MainTrack), GetDetNumber(detector), _rate_corr[0][0], _rate_error[0][0],_rate_index[0][0])) _rate_index[0][0]=-1;
228 
229  if (_reco_index[fgd][categ]>=0){
230  eventWeight.Systematic *= (1+ _reco_corr[fgd][categ] + _reco_error[fgd][categ]*toy.GetToyVariations(_index)->Variations[_reco_index[fgd][categ]]);
231  eventWeight.Correction *= (1+ _reco_corr[fgd][categ]);
232  }
233  if (_rate_index[0][0]>=0){
234  eventWeight.Systematic *= (1+ _rate_corr[0][0] + _rate_error[0][0] * toy.GetToyVariations(_index)->Variations[_rate_index[0][0]]);
235  eventWeight.Correction *= (1+ _rate_corr[0][0]);
236  }
237  }
238 
239  }
240 
241 
242  if (eventWeight.Systematic < 0) eventWeight.Systematic = 0;
243 
244  return eventWeight;
245 }
246 
Int_t _index
The index of this systematic (needed by SystematicsManager);.
AnaTrueVertexB * TrueVertex
Pointer to the AnaTrueVertexB of the interaction that created this AnaTrueParticleB.
Float_t PositionStart[4]
The reconstructed start position of the particle.
Float_t * Variations
the vector of Variations, one for each of the systematic parameters
static bool IsFGDDetector(SubDetId::SubDetEnum det)
Check if a detector enumeration refers to a FGD or not.
Definition: SubDetId.cxx:126
static bool IsP0DDetector(SubDetId::SubDetEnum det)
Check if a detector enumeration refers to a SMRDP0D or not.
Definition: SubDetId.cxx:146
SubDetId_h DetectorFV
Indicate the FV we are interested in.
Definition: ToyBoxB.hxx:52
Int_t GParentPDG
The PDG code of this particle&#39;s grandparent, or 0 if there is no grandparent.
AnaTrackB * MainTrack
For storing the Main Track (The lepton candidate in geranal: HMN or HMP track)
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.
SubDetId::SubDetEnum GetDetector(const Float_t *pos)
Return the detector in which the position is.
static bool IsPECALDetector(SubDetId::SubDetEnum det)
Check if a detector enumeration refers to a P0D ECAL or not.
Definition: SubDetId.cxx:138
Float_t Charge
The reconstructed charge of the particle.
bool GetBinValues(Float_t value, Float_t &mean, Float_t &sigma)
Gets the bin values for a 1D source.
virtual bool IsRelevantRecObjectForSystematicInToy(const AnaEventC &, const ToyBoxB &, AnaRecObjectC *, SystId_h syst_index, Int_t branch=0) const
Is this track relevant for a given systematic (after selection, called for each toy) ...
Float_t Position[4]
The position the true interaction happened at.
static bool IsTPCDetector(SubDetId::SubDetEnum det)
Check if a detector enumeration refers to a TPC or not.
Definition: SubDetId.cxx:122
int GetRunPeriod(int run, int subrun=-1)
Returns the run period (sequentially: 0,1,2,3,4,5 ...)
Int_t SuccessfulBranch
The branch that is successful for this toy in the selection this ToyBox belongs to.
Definition: ToyBoxB.hxx:46
static bool IsECALDetector(SubDetId::SubDetEnum det)
Check if a detector enumeration refers to a ECAL or not.
Definition: SubDetId.cxx:130
SubDetEnum
Enumeration of all detector systems and subdetectors.
Definition: SubDetId.hxx:25
Int_t ParentPDG
The PDG code of this particle&#39;s immediate parent, or 0 if there is no parent.
Representation of a global track.
static bool IsTECALDetector(SubDetId::SubDetEnum det)
Check if a detector enumeration refers to a Tracker ECAL or not.
Definition: SubDetId.cxx:134
ToyVariations * GetToyVariations(UInt_t index) const
returns the variations for a given systematic (index)
bool InDetVolume(SubDetId::SubDetEnum det, const Float_t *pos)
Weight_h ComputeWeight(const ToyExperiment &toy, const AnaEventC &event, const ToyBoxB &box)
Float_t Position[4]
The initial position of the true particle.
AnaTrueParticleB * GetTrueParticle() const
Return a casted version of the AnaTrueObject associated.
bool InFiducialVolume(SubDetId::SubDetEnum det, const Float_t *pos, const Float_t *FVdefmin, const Float_t *FVdefmax)
Float_t Direction[3]
The initial direction of the true particle.
Int_t GetNBins()
Get the number of bins.
static bool IsSMRDDetector(SubDetId::SubDetEnum det)
Check if a detector enumeration refers to a SMRD or not.
Definition: SubDetId.cxx:142