HighLAND
tutorialVariationSystematics.cxx
1 #include "tutorialVariationSystematics.hxx"
2 #include "EventBoxTracker.hxx"
3 #include "ND280AnalysisUtils.hxx"
4 
5 /*
6  A variation systematic is run before the selection, modifying the input data. The selection will then proceed
7  on that modified data, such that we can test the effect of any source variation on the final number of selected events and on
8  the distributions of any observable. As advanced, a variation systematic could have two effects:
9  - Change the number of selected events because a variable we can on (or depends on it) is varied.
10  - Change the value of an observable.
11 
12  In this particular example we scale the momentum of all tracks, this will have a small effect on the number of selected events
13  (through the PID cut, which depends on momentum), because there is no direct cut on momentum, but a large effect on the distribution of
14  the muon candidate momentum
15 
16 */
17 
18 
19 //********************************************************************
21 //********************************************************************
22 
23  /* A variation systematic inherits from two base classes:
24  - EventVariationBase: The 1 in the constructor is the number of systematic parameters (one in this case).
25  - BinnedParams: Allows dealing with the source parameters to be propagated (reads the .dat file, etc). In this particular case the
26  .dat file will be MomentumScale.dat under psycheSystematics/vXrY/data. The name of the systematic data file is the one given to the
27  BinnedParams constructor (above). The second argument in the constructor is the type of systematics source. Thse are the available types:
28 
29  For standard weight systematics:
30  k1D_SYMMETRIC: the systematic depends on a single observable (i.e. momentum) and its error is symmetric
31  k2D_SYMMETRIC: the systematic depends on two observables (i.e. momentum and angle) and its error is symmetric
32  k3D_SYMMETRIC: the systematic depends on three observables and its error is symmetric
33  k1D_SYMMETRIC_NOMEAN: the systematic depends on a single observable (i.e. momentum) and its error is symmetric. The correction is not specified
34  k2D_SYMMETRIC_NOMEAN: the systematic depends on two observables (i.e. momentum and angle) and its error is symmetric. The correction is not specified
35  k3D_SYMMETRIC_NOMEAN: the systematic depends on three observables and its error is symmetric. The correction is not specified
36 
37 
38  For efficiency-like weight systematics:
39  k1D_EFF_SYMMETRIC: the systematic depends on a single observable (i.e. momentum) and its error is symmetric
40  k2D_EFF_SYMMETRIC, the systematic depends on two observables (i.e. momentum and angle) and its error is symmetric
41  k3D_EFF_SYMMETRIC, the systematic depends on three observables and its error is symmetric
42  k1D_EFF_ASSYMMETRIC: the systematic depends on a single observable (i.e. momentum) and its error is assymmetric
43  k2D_EFF_ASSYMMETRIC: the systematic depends on two observables (i.e. momentum and angle) and its error is assymmetric
44  k3D_EFF_ASSYMMETRIC, the systematic depends on three observables and its error is assymmetric
45 
46  Efficiency-like weight systematics need the values for data and MC control samples (with errors) and also the nominal for the MC analysis sample
47  */
48 
49  // Read the parameters for bin 0. This particular systematic has only one bin, becaouse we consider the same
50  // momentum scale systematic for all phase space (momentum, angle, etc)
51  // Since this is a 1D sysmetric systematisc source only two parameters should be read for each bin:
52  // the mean (_scale) and its error (_scaleError).
54 }
55 
56 //********************************************************************
58 //********************************************************************
59 
60  /*
61  This method takes two arguments:
62  - ToyExperiment: Contains the variation to be applied (in number of sigmas)
63  - AnaEventC: Sometimes we need extra information not present in the systematics box
64 
65 
66  In addition we need the SystBox:
67  - SystBoxB: Sometimes the information needed to compute the systematic is the same for all toys (in this case all tracks with a TPC segment
68  and starting in the FGD1 FV). The operation of getting that set of tracks is time comsuming. In order not to repeat that opperation for each toy
69  it is better to do it only once for each event. This information is stored in this systematics box
70  */
71 
72 
73  // Get the SystBox for this event
74  SystBoxB* box = GetSystBox(event);
75 
76 #ifdef DEBUG
77  std::cout << "MomentumScaleSystematics::ApplyVariation(): " << box->nRelevantRecObjects <<" and? "<<box->nRelevantRecObjects<< std::endl;
78 #endif
79 
80  // loop over the relevant tracks for this systematic
81  for (Int_t itrk=0;itrk<box->nRelevantRecObjects;itrk++){
82  AnaTrackB* track = static_cast<AnaTrackB*>(box->RelevantRecObjects[itrk]);
83 
84 #ifdef DEBUG
85  std::cout << itrk << " --> p0 = " << track->Momentum << std::endl;
86 #endif
87 
88  // Apply the momentum scale factor
90 
91 #ifdef DEBUG
92  std::cout << "p = " << p << std::endl;
93 #endif
94  }
95 }
96 
97 //********************************************************************
99 //********************************************************************
100 
101  /* This method is very important. When this method returns true, the whole AnaSpill class is reseted such the all possible variations
102  are undone (we get the spill as it was after corrections). This is however very time consuming since the AnaSpill class is a very big one.
103 
104  To speed up this action we can move back to its original value only the variables that were modified. Each systematic variation will do that operation
105  for the variables modified by it.
106  */
107 
108  // Get the SystBox for this event
109  SystBoxB* box = GetSystBox(event);
110 
111  // Get all relevant tracks for this systematic
112  AnaRecObjectC** recObjs = box->RelevantRecObjects;
113 
114  for (Int_t itrk=0;itrk<box->nRelevantRecObjects;itrk++){
115  AnaTrackB* track = static_cast<AnaTrackB*>(recObjs[itrk]);
116 
117  // Go back to the corrected momentum
118  track->Momentum = track->GetOriginalTrack()->Momentum;
119  }
120 
121  // Don't reset the spill to corrected
122  return false;
123 
124 }
125 
126 //********************************************************************
128 //********************************************************************
129 
130  /*
131  In this method we specify what are the trackGroups that are relevant for this systematic. This depends on the detectorFV specified in the selection
132  Using track groups we avoid looping over all tracks to select a portion of them for every toy, which would be very expensive in CPU time
133  */
134 
135  SubDetId_h det = sel.GetDetectorFV();
136 
137  if (det == SubDetId::kFGD1){
138  IDs[0] = EventBoxTracker::kTracksWithTPCInFGD1FV;
139  return 1;
140  }
141  else if (det == SubDetId::kFGD2){
142  IDs[0] = EventBoxTracker::kTracksWithTPCInFGD2FV;
143  return 1;
144  }
145  else if (det == SubDetId::kFGD){
146  IDs[0] = EventBoxTracker::kTracksWithTPCInFGD1FV;
147  IDs[1] = EventBoxTracker::kTracksWithTPCInFGD2FV;
148  return 2;
149  }
150 
151  return 0;
152 }
Int_t _index
The index of this systematic (needed by SystematicsManager);.
const AnaTrackB * GetOriginalTrack() const
Return a casted version of the original AnaParticleB associated.
Int_t GetRelevantRecObjectGroups(const SelectionBase &sel, Int_t *IDs) const
Get the TrackGroup IDs array for this systematic.
Float_t * Variations
the vector of Variations, one for each of the systematic parameters
Float_t _scaleError
Width of the scale distribution.
SystBoxB * GetSystBox(const AnaEventC &event, Int_t isel=0, Int_t ibranch=0) const
Get the SystBox corresponding to a selection, branch and event.
Float_t Momentum
The reconstructed momentum of the particle, at the start position.
Representation of a global track.
ToyVariations * GetToyVariations(UInt_t index) const
returns the variations for a given systematic (index)
virtual bool UndoSystematic(AnaEventC &event)
Undo the systematic variations done by ApplyVariation. This is faster tha reseting the full Spill...
Int_t nRelevantRecObjects
----—— Relevant rec objects and true objects for each systematic ------------—— ...
Definition: SystBoxB.hxx:20
bool GetParametersForBin(Int_t index, Float_t &mean, Float_t &sigma)
Gets the bin values for a source provided the bin index.
Float_t _scale
Mean of the scale distribution.
virtual void Apply(const ToyExperiment &toy, AnaEventC &event)
Apply the systematic.
SubDetId_h GetDetectorFV(Int_t ibranch=0) const
Get the detector in which the Fiducial Volume is defined.