HighLAND
AnalysisManager.cxx
1 #include "AnalysisManager.hxx"
2 #include "Parameters.hxx"
3 #include "ND280AnalysisUtils.hxx"
4 #include "VersioningUtils.hxx"
5 #include "SystematicUtils.hxx"
6 // Include converters
7 #include "RedoTreeConverter.hxx"
8 #include "MiniTreeConverter.hxx"
9 
10 // Include systematics
11 #include "BFieldDistortionSystematics.hxx"
12 #include "MomentumScaleSystematics.hxx"
13 #include "MomentumResolSystematics.hxx"
14 #include "ToFResolSystematics.hxx"
15 #include "ECalEMEnergyResolSystematics.hxx"
16 #include "ECalEMEnergyScaleSystematics.hxx"
17 
18 // Efficiency
19 #include "ChargeIDEffSystematics.hxx"
20 #include "TPCTrackEffSystematics.hxx"
21 #include "FGDTrackEffSystematics.hxx"
22 #include "FGDHybridTrackEffSystematics.hxx"
23 #include "TPCClusterEffSystematics.hxx"
24 #include "MichelElectronEffSystematics.hxx"
25 
26 // Matching
27 #include "TPCFGDMatchEffSystematics.hxx"
28 #include "TPCECalMatchEffSystematics.hxx"
29 
30 // PID syst
31 #include "TPCPIDSystematics.hxx"
32 #include "FGDPIDSystematics.hxx"
33 #include "ECalEmHipPIDSystematics.hxx"
34 #include "ECalPIDSystematics.hxx"
35 
36 // Weight
37 #include "PileUpSystematics.hxx"
38 #include "FGDMassSystematics.hxx"
39 #include "OOFVSystematics.hxx"
40 #include "SandMuonsSystematics.hxx"
41 #include "SIPionSystematics.hxx"
42 #include "SIProtonSystematics.hxx"
43 #include "FluxWeightSystematics.hxx"
44 
45 // Nue
46 #include "FGD2ShowerSystematics.hxx"
47 #include "nueECalPileUpSystematics.hxx"
48 #include "nueOOFVSystematics.hxx"
49 #include "nueP0DPileUpSystematics.hxx"
50 #include "nueTPCPileUpSystematics.hxx"
51 
52 // Include selections
53 // Numu FGD1
54 #include "numuCCSelection.hxx"
55 #include "numuCC4piSelection.hxx"
56 #include "numuCCMultiPiSelection.hxx"
57 #include "numuCC4piMultiPiSelection.hxx"
58 
59 
60 // Numu FGD2
61 #include "numuCCFGD2Selection.hxx"
62 #include "numuCC4piFGD2Selection.hxx"
63 #include "numuCCMultiPiFGD2Selection.hxx"
64 #include "numuCC4piMultiPiFGD2Selection.hxx"
65 
66 // Numu Bkg in anti-numu, FGD1
67 #include "numuBkgInAntiNuModeCCSelection.hxx"
68 #include "numuBkgInAntiNuModeCCMultiTrackSelection.hxx"
69 #include "numuBkgInAntiNuModeCCMultiPiSelection.hxx"
70 
71 // Numu Bkg in anti-numu, FGD2
72 #include "numuBkgInAntiNuModeCCFGD2Selection.hxx"
73 #include "numuBkgInAntiNuModeCCMultiTrackFGD2Selection.hxx"
74 #include "numuBkgInAntiNuModeCCMultiPiFGD2Selection.hxx"
75 
76 // Anti-numu FGD1
77 #include "antiNumuCCSelection.hxx"
78 #include "antiNumuCCMultiTrackSelection.hxx"
79 #include "antiNumuCCMultiPiSelection.hxx"
80 
81 // Anti-numu FGD2
82 #include "antiNumuCCFGD2Selection.hxx"
83 #include "antiNumuCCMultiTrackFGD2Selection.hxx"
84 #include "antiNumuCCMultiPiFGD2Selection.hxx"
85 
86 // nue FGD1
87 #include "nueCCSelection.hxx"
88 
89 // nue FGD2
90 #include "nueCCFGD2Selection.hxx"
91 
92 // gamma FGD1
93 #include "gammaSelection.hxx"
94 
95 // gamma FGD2
96 #include "gammaFGD2Selection.hxx"
97 
98 // Anti-nue FGD1
99 #include "antiNueCCSelection.hxx"
100 
101 // Anti-nue FGD2
102 #include "antiNueCCFGD2Selection.hxx"
103 
104 
105 //******************************************************************
106 AnalysisManager::AnalysisManager(){
107 //******************************************************************
108 
110 
111  DefineProduction();
112  DefineInputConverters();
113  DefineSelections();
114  DefineSystematics();
115 
116  // ND::params().LoadParametersFile("psycheSelections");
117  // ND::params().LoadParametersFile("psycheSteering");
118  // ND::params().LoadParametersFile("psycheSystematics");
119 
120  _applyEventVariations = (bool)ND::params().GetParameterI("psycheSteering.Systematics.ApplyVariationSystematics");
121  _applyEventWeights = (bool)ND::params().GetParameterI("psycheSteering.Systematics.ApplyWeightSystematics");
122  _applyFluxWeightSystematic = (bool)ND::params().GetParameterI("psycheSteering.Systematics.ApplyFluxWeightSystematic");
123 
124  _flux= NULL;
125 
126  // enable/disable flux weighting.
127  _applyFluxWeight = ND::params().GetParameterI("psycheSteering.FluxWeighting.Enable");
128 
129  // flux weight file and option
130  _fluxFile = ND::params().GetParameterS("psycheSteering.FluxWeighting.Folder");
131  _fluxVersion = ND::params().GetParameterS("psycheSteering.FluxWeighting.Version");
132  _fluxTuning = ND::params().GetParameterS("psycheSteering.FluxWeighting.Tuning");
133 
134  if (_applyFluxWeight)
135  _flux = new FluxWeighting(_fluxFile, _fluxVersion, _fluxTuning);
136 
137 
138  _currentEvent=-1;
139  _exp = NULL;
140  _eventArray = NULL;
141  _samples.clear();
142  _currentSampleGroup = NULL;
143  _nEventsToProcess = -1;
144  _nEventsProcessed = 0;
145  _nEventsInArray = 0;
146 
147  _mcEventArray = NULL;
148  _dataEventArray = NULL;
149  _nEventsInMCArray = 0;
151  _doFGD1=true;
152  _doFGD2=false;
153 
154 }
155 
156 //******************************************************************
157 void AnalysisManager::DefineInputConverters(){
158  //******************************************************************
159  input().AddConverter("kHighlandTree", new RedoTreeConverter());
160  input().AddConverter("MiniTree", new MiniTreeConverter());
161  // input().AddConverter(kHighlandTree, new HighlandTreeConverter());
162  // input().AddConverter(kP0d, new P0dTreeConverter());
163 }
164 
165 //******************************************************************
166 void AnalysisManager::DefineProduction(){
167  //********************************************************************
168 
169  // Select the production from the parameters file. This will be used for POT, ...
170  if (ND::params().GetParameterI("psycheSteering.POT.Production")==6){
171  versionUtils::prod6_POT = true;
172  std::cout << "WARNING: production has been overwritten by parameter psycheSteering.POT.Production == 6" << std::endl;
173  }
174  else if (ND::params().GetParameterI("psycheSteering.POT.Production")==5){
175  versionUtils::prod6_POT = false;
176  std::cout << "WARNING: production has been overwritten by parameter psycheSteering.POT.Production == 5" << std::endl;
177  }
178 
179  // Select the production from the parameters file. This will be used for bunching, ...
180  if (ND::params().GetParameterI("psycheSteering.Bunching.Production")==6){
181  versionUtils::prod6_bunching = true;
182  std::cout << "WARNING: production has been overwritten by parameter psycheSteering.Bunching.Production == 6" << std::endl;
183  }
184  else if (ND::params().GetParameterI("psycheSteering.Bunching.Production")==5){
185  versionUtils::prod6_bunching = false;
186  std::cout << "WARNING: production has been overwritten by parameter psycheSteering.Bunching.Production == 5" << std::endl;
187  }
188 
189  // Select the production from the parameters file. This will be used for corrections
190  if (ND::params().GetParameterI("psycheSteering.Corrections.Production")==6){
191  versionUtils::prod6_corrections = true;
192  std::cout << "WARNING: production has been overwritten by parameter psycheSteering.Corrections.Production == 6" << std::endl;
193  }
194  else if (ND::params().GetParameterI("psycheSteering.Corrections.Production")==5){
195  versionUtils::prod6_corrections = false;
196  std::cout << "WARNING: production has been overwritten by parameter psycheSteering.Corrections.Production == 5" << std::endl;
197  }
198 
199  // Select the production from the parameters file. This will be used for systematics
200  if (ND::params().GetParameterI("psycheSteering.Systematics.Production")==6){
201  versionUtils::prod6_systematics = true;
202  std::cout << "WARNING: production has been overwritten by parameter psycheSteering.Systematics.Production == 6" << std::endl;
203  }
204  else if (ND::params().GetParameterI("psycheSteering.Systematics.Production")==5){
205  versionUtils::prod6_systematics = false;
206  std::cout << "WARNING: production has been overwritten by parameter psycheSteering.Systematics.Production == 5" << std::endl;
207  }
208 
209 }
210 
211 //******************************************************************
212 void AnalysisManager::DefineSelections(){
213 //******************************************************************
214  // Numu FGD1
215  sel().AddSelection("kTrackerNumuCC", "inclusive numuCC selection", new numuCCSelection());
216  sel().AddSelection("kTrackerNumuCCMultiPi", "numuCC MultiPi selection", new numuCCMultiPiSelection());
217  sel().AddSelection("kTrackerNumuCC4pi", "inclusive 4pi numuCC selection", new numuCC4piSelection());
218  sel().AddSelection("kTrackerNumuCC4piMultiPi", "numuCC 4pi MultiPi selection", new numuCC4piMultiPiSelection());
219 
220  // Anti-numu FGD1
221  sel().AddSelection("kTrackerAntiNumuCC", "antiNumuCC selection", new antiNumuCCSelection());
222  sel().AddSelection("kTrackerAntiNumuCCMultiTrack", "antiNumuCC MultiTrack selection", new antiNumuCCMultiTrackSelection());
223  sel().AddSelection("kTrackerAntiNumuCCMultiPi", "antiNumuCC MultiPi selection", new antiNumuCCMultiPiSelection());
224 
225  // Numu Bkg in anti-numu, FGD1
226  sel().AddSelection("kTrackerNumuInAntiNuModeCC", "numuBkg In AntiNuMode CC selection", new numuBkgInAntiNuModeCCSelection());
227  sel().AddSelection("kTrackerNumuInAntiNuModeCCMultiTrack", "numuBkg In AntiNuMode CCMultiTrack selection", new numuBkgInAntiNuModeCCMultiTrackSelection());
228  sel().AddSelection("kTrackerNumuInAntiNuModeCCMultiPi", "numuBkg In AntiNuMode CCMultiPi selection", new numuBkgInAntiNuModeCCMultiPiSelection());
229 
230  // Numu FGD2
231  sel().AddSelection("kTrackerNumuCCFGD2", "inclusive numuCC selection in FGD2", new numuCCFGD2Selection());
232  sel().AddSelection("kTrackerNumuCCMultiPiFGD2", "numuCC MultiPi selection in FGD2", new numuCCMultiPiFGD2Selection());
233  sel().AddSelection("kTrackerNumuCC4piFGD2", "inclusive 4pi numuCC selection in FGD2", new numuCC4piFGD2Selection());
234  sel().AddSelection("kTrackerNumuCC4piMultiPiFGD2", "numuCC 4pi MultiPi selection in FGD2", new numuCC4piMultiPiFGD2Selection());
235 
236  // Anti-numu FGD2
237  sel().AddSelection("kTrackerAntiNumuCCFGD2", "antiNumuCC selection in FGD2", new antiNumuCCFGD2Selection());
238  sel().AddSelection("kTrackerAntiNumuCCMultiTrackFGD2", "antiNumuCC MultiTrack selection in FGD2", new antiNumuCCMultiTrackFGD2Selection());
239  sel().AddSelection("kTrackerAntiNumuCCMultiPiFGD2", "antiNumuCC MultiPi selection in FGD2", new antiNumuCCMultiPiFGD2Selection());
240 
241  // Numu Bkg in anti-numu, FGD2
242  sel().AddSelection("kTrackerNumuInAntiNuModeCCFGD2", "numuBkg In AntiNuMode CC selection in FGD2", new numuBkgInAntiNuModeCCFGD2Selection());
243  sel().AddSelection("kTrackerNumuInAntiNuModeCCMultiTrackFGD2", "numuBkg In AntiNuMode CCMultiTrack selection in FGD2", new numuBkgInAntiNuModeCCMultiTrackFGD2Selection());
244  sel().AddSelection("kTrackerNumuInAntiNuModeCCMultiPiFGD2", "numuBkg In AntiNuMode CCMultiPi selection in FGD2", new numuBkgInAntiNuModeCCMultiPiFGD2Selection());
245 
246  // FGD1 nue
247  sel().AddSelection("kTrackerNuECC", "NuECC inclusive selection", new nueCCSelection ());
248  sel().AddSelection("kTrackerAntiNuECC", "AntiNuECC inclusive selection", new antiNueCCSelection());
249  sel().AddSelection("kTrackerGamma", "GammaBkg selection", new gammaSelection ());
250 
251  // FGD2 nue
252  sel().AddSelection("kTrackerNuECCFGD2", "NuECC inclusive selection in FGD2", new nueCCFGD2Selection ());
253  sel().AddSelection("kTrackerAntiNuECCFGD2", "AntiNuECC inclusive selection in FGD2", new antiNueCCFGD2Selection());
254  sel().AddSelection("kTrackerGammaFGD2", "GammaBkg selection in FGD2", new gammaFGD2Selection ());
255 
256  // Set which run range the selections are valid for using the parameters file
257  // If this is not called the selections are applied to all run periods
258  // Numu FGD1
259  sel().SetValidRunPeriods("kTrackerNumuCC", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuCC.ValidRunPeriods"));
260  sel().SetValidRunPeriods("kTrackerNumuCC4piMultiPi", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuCC4piMultiPi.ValidRunPeriods"));
261  sel().SetValidRunPeriods("kTrackerNumuCC4pi", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuCC4pi.ValidRunPeriods"));
262  sel().SetValidRunPeriods("kTrackerNumuCCMultiPi", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuCCMultiPi.ValidRunPeriods"));
263 
264  // Anti-numu FGD1
265  sel().SetValidRunPeriods("kTrackerAntiNumuCC", ND::params().GetParameterS("psycheSteering.Selections.TrackerAntiNumuCC.ValidRunPeriods"));
266  sel().SetValidRunPeriods("kTrackerAntiNumuCCMultiTrack", ND::params().GetParameterS("psycheSteering.Selections.TrackerAntiNumuCCMultiTrack.ValidRunPeriods"));
267  sel().SetValidRunPeriods("kTrackerAntiNumuCCMultiPi", ND::params().GetParameterS("psycheSteering.Selections.TrackerAntiNumuCCMultiPi.ValidRunPeriods"));
268 
269  // Numu Bkg in anti-numu, FGD1
270  sel().SetValidRunPeriods("kTrackerNumuInAntiNuModeCC", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuInAntiNuModeCC.ValidRunPeriods"));
271  sel().SetValidRunPeriods("kTrackerNumuInAntiNuModeCCMultiTrack", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuInAntiNuModeCCMultiTrack.ValidRunPeriods"));
272  sel().SetValidRunPeriods("kTrackerNumuInAntiNuModeCCMultiPi", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuInAntiNuModeCCMultiPi.ValidRunPeriods"));
273 
274 
275  // Same periods as for FGD1
276  // Numu FGD2
277  sel().SetValidRunPeriods("kTrackerNumuCCFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuCC.ValidRunPeriods"));
278  sel().SetValidRunPeriods("kTrackerNumuCCMultiPiFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuCCMultiPi.ValidRunPeriods"));
279  sel().SetValidRunPeriods("kTrackerNumuCC4piFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuCC4pi.ValidRunPeriods"));
280  sel().SetValidRunPeriods("kTrackerNumuCC4piMultiPiFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuCC4piMultiPi.ValidRunPeriods"));
281 
282  // Anti-numu FGD2
283  sel().SetValidRunPeriods("kTrackerAntiNumuCCFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerAntiNumuCC.ValidRunPeriods"));
284  sel().SetValidRunPeriods("kTrackerAntiNumuCCMultiTrackFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerAntiNumuCCMultiTrack.ValidRunPeriods"));
285  sel().SetValidRunPeriods("kTrackerAntiNumuCCMultiPiFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerAntiNumuCCMultiPi.ValidRunPeriods"));
286 
287  // Numu Bkg in anti-numu, FGD2
288  sel().SetValidRunPeriods("kTrackerNumuInAntiNuModeCCFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuInAntiNuModeCC.ValidRunPeriods"));
289  sel().SetValidRunPeriods("kTrackerNumuInAntiNuModeCCMultiTrackFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuInAntiNuModeCCMultiTrack.ValidRunPeriods"));
290  sel().SetValidRunPeriods("kTrackerNumuInAntiNuModeCCMultiPiFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerNumuInAntiNuModeCCMultiPi.ValidRunPeriods"));
291 
292  // FGD1 nue
293  sel().SetValidRunPeriods("kTrackerNuECC", ND::params().GetParameterS("psycheSteering.Selections.TrackerNuECC.ValidRunPeriods"));
294  sel().SetValidRunPeriods("kTrackerAntiNuECC", ND::params().GetParameterS("psycheSteering.Selections.TrackerAntiNuECC.ValidRunPeriods"));
295  sel().SetValidRunPeriods("kTrackerGamma", ND::params().GetParameterS("psycheSteering.Selections.TrackerGamma.ValidRunPeriods"));
296 
297  // FGD2 nue
298  sel().SetValidRunPeriods("kTrackerNuECCFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerNuECC.ValidRunPeriods"));
299  sel().SetValidRunPeriods("kTrackerAntiNuECCFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerAntiNuECC.ValidRunPeriods"));
300  sel().SetValidRunPeriods("kTrackerGammaFGD2", ND::params().GetParameterS("psycheSteering.Selections.TrackerGamma.ValidRunPeriods"));
301 
302  // Numu FGD1
303  if (!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuCC"))
304  sel().DisableSelection("kTrackerNumuCC");
305 
306  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuCCMultiPi"))
307  sel().DisableSelection("kTrackerNumuCCMultiPi");
308 
309  if (!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuCC4pi"))
310  sel().DisableSelection("kTrackerNumuCC4pi");
311 
312  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuCC4piMultiPi"))
313  sel().DisableSelection("kTrackerNumuCC4piMultiPi");
314 
315  // Anti-numu FGD1
316  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerAntiNumuCC"))
317  sel().DisableSelection("kTrackerAntiNumuCC");
318 
319  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerAntiNumuCCMultiTrack"))
320  sel().DisableSelection("kTrackerAntiNumuCCMultiTrack");
321 
322  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerAntiNumuCCMultiPi"))
323  sel().DisableSelection("kTrackerAntiNumuCCMultiPi");
324 
325  // Numu Bkg in anti-numu, FGD1
326  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuInAntiNuModeCC"))
327  sel().DisableSelection("kTrackerNumuInAntiNuModeCC");
328 
329  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuInAntiNuModeCCMultiTrack"))
330  sel().DisableSelection("kTrackerNumuInAntiNuModeCCMultiTrack");
331 
332  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuInAntiNuModeCCMultiPi"))
333  sel().DisableSelection("kTrackerNumuInAntiNuModeCCMultiPi");
334 
335 
336  // Numu FGD2
337  if (!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuCCFGD2"))
338  sel().DisableSelection("kTrackerNumuCCFGD2");
339 
340  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuCCMultiPiFGD2"))
341  sel().DisableSelection("kTrackerNumuCCMultiPiFGD2");
342 
343  if (!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuCC4piFGD2"))
344  sel().DisableSelection("kTrackerNumuCC4piFGD2");
345 
346  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuCC4piMultiPiFGD2"))
347  sel().DisableSelection("kTrackerNumuCC4piMultiPiFGD2");
348 
349 
350  // Anti-numu FGD2
351  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerAntiNumuCCFGD2"))
352  sel().DisableSelection("kTrackerAntiNumuCCFGD2");
353 
354  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerAntiNumuCCMultiTrackFGD2"))
355  sel().DisableSelection("kTrackerAntiNumuCCMultiTrackFGD2");
356 
357  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerAntiNumuCCMultiPiFGD2"))
358  sel().DisableSelection("kTrackerAntiNumuCCMultiPiFGD2");
359 
360  // Numu Bkg in anti-numu, FGD2
361  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuInAntiNuModeCCFGD2"))
362  sel().DisableSelection("kTrackerNumuInAntiNuModeCCFGD2");
363 
364  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuInAntiNuModeCCMultiTrackFGD2"))
365  sel().DisableSelection("kTrackerNumuInAntiNuModeCCMultiTrackFGD2");
366 
367  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNumuInAntiNuModeCCMultiPiFGD2"))
368  sel().DisableSelection("kTrackerNumuInAntiNuModeCCMultiPiFGD2");
369 
370  // FGD1 nue
371  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNuECC"))
372  sel().DisableSelection("kTrackerNuECC");
373 
374  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerAntiNuECC"))
375  sel().DisableSelection("kTrackerAntiNuECC");
376 
377  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerGamma"))
378  sel().DisableSelection("kTrackerGamma");
379 
380  // FGD2 nue
381  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerNuECCFGD2"))
382  sel().DisableSelection("kTrackerNuECCFGD2");
383 
384  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerAntiNuECCFGD2"))
385  sel().DisableSelection("kTrackerAntiNuECCFGD2");
386 
387  if(!ND::params().GetParameterI("psycheSteering.Selections.EnableTrackerGammaFGD2"))
388  sel().DisableSelection("kTrackerGammaFGD2");
389 
390 }
391 
392 //******************************************************************
393 void AnalysisManager::DefineSystematics(){
394 //******************************************************************
395 
396  //--------------- Add Systematic Variations specifying the number of bins in the PDF
397  evar().AddEventVariation(SystId::kBFieldDist, "BFieldDist", new BFieldDistortionSystematics());
398  evar().AddEventVariation(SystId::kMomScale, "MomScale", new MomentumScaleSystematics());
399  evar().AddEventVariation(SystId::kMomResol, "MomResol", new MomentumResolSystematics());
400  evar().AddEventVariation(SystId::kTpcPid, "TpcPid", new TPCPIDSystematics());
401  evar().AddEventVariation(SystId::kFgdPid, "FgdPid", new FGDPIDSystematics());
402  evar().AddEventVariation(SystId::kToFResol, "ToFResol", new ToFResolSystematics());
403  evar().AddEventVariation(SystId::kECalEMScale, "ECalEMScale", new ECalEMEnergyScaleSystematics());
404  evar().AddEventVariation(SystId::kECalEMResol, "ECalEMResol", new ECalEMEnergyResolSystematics());
405 
406  //-------------- Add Systematics Weights
407  eweight().AddEventWeight(SystId::kChargeIDEff, "ChargeIDEff", new ChargeIDEffSystematics());
408  eweight().AddEventWeight(SystId::kTpcClusterEff, "TpcClusterEff", new TPCClusterEffSystematics());
409  eweight().AddEventWeight(SystId::kTpcTrackEff, "TpcTrackEff", new TPCTrackEffSystematics());
410  eweight().AddEventWeight(SystId::kTpcFgdMatchEff, "TpcFgdMatchEff", new TPCFGDMatchEffSystematics());
411  eweight().AddEventWeight(SystId::kFgdTrackEff, "FgdTrackEff", new FGDTrackEffSystematics());
412  eweight().AddEventWeight(SystId::kFgdHybridTrackEff, "FgdHybridTrackEff", new FGDHybridTrackEffSystematics());
413  eweight().AddEventWeight(SystId::kMichelEleEff, "MichelEleEff", new MichelElectronEffSystematics());
414  eweight().AddEventWeight(SystId::kPileUp, "PileUp", new PileUpSystematics());
415  eweight().AddEventWeight(SystId::kFgdMass, "FgdMass", new FGDMassSystematics());
416  eweight().AddEventWeight(SystId::kOOFV, "OOFV", new OOFVSystematics());
417  eweight().AddEventWeight(SystId::kSIPion, "SIPion", new SIPionSystematics());
418  eweight().AddEventWeight(SystId::kSIProton, "SIProton", new SIProtonSystematics());
419  eweight().AddEventWeight(SystId::kSandMu, "SandMu", new SandMuonsSystematics());
420  eweight().AddEventWeight(SystId::kECalPID, "ECalPID", new ECalPIDSystematics());
421  eweight().AddEventWeight(SystId::kTpcECalMatchEff, "TpcECalMatchEff", new TPCECalMatchEffSystematics());
422 
423  // Nue specific
424  eweight().AddEventWeight(SystId::kECalEmHipPID, "ECalEmHipPID", new ECalEmHipPIDSystematics());
425  eweight().AddEventWeight(SystId::kFGD2Shower, "FGD2Shower", new FGD2ShowerSystematics());
426  eweight().AddEventWeight(SystId::kNuETPCPileUp, "NuETPCPileUp", new nueTPCPileUpSystematics());
427  eweight().AddEventWeight(SystId::kNuEP0DPileUp, "NuEP0DPileUp", new nueP0DPileUpSystematics());
428  eweight().AddEventWeight(SystId::kNuEECalPileUp, "NuEECalPileUp", new nueECalPileUpSystematics());
429  eweight().AddEventWeight(SystId::kNuEOOFV, "NuEOOFV", new nueOOFVSystematics());
430 
431  syst().AddFluxSystematic(SystId::kFluxWeightNu, "kFluxWeightNeutrino", new FluxWeightSystematicsNeutrino());
432  syst().AddFluxSystematic(SystId::kFluxWeightAntiNu, "kFluxWeightAntiNeutrino", new FluxWeightSystematicsAntiNeutrino());
433 
434 
435  //-------------- Enable systematics in the different event selections
436 
437  // First disable all systematics
439 
440  _initialisePionSystematics = (bool) ND::params().GetParameterI("psycheSteering.Weights.EnableSIPion");
441 
442  // Then enable the ones specified in the parameters file
443  if(ND::params().GetParameterI("psycheSteering.Variations.EnableBFieldDist")) evar().EnableEventVariation(SystId::kBFieldDist);
444  if(ND::params().GetParameterI("psycheSteering.Variations.EnableMomScale")) evar().EnableEventVariation(SystId::kMomScale);
445  if(ND::params().GetParameterI("psycheSteering.Variations.EnableMomResol")) evar().EnableEventVariation(SystId::kMomResol);
446  if(ND::params().GetParameterI("psycheSteering.Variations.EnableTpcPid")) evar().EnableEventVariation(SystId::kTpcPid);
447  if(ND::params().GetParameterI("psycheSteering.Variations.EnableFgdPid")) evar().EnableEventVariation(SystId::kFgdPid);
448  if(ND::params().GetParameterI("psycheSteering.Variations.EnableToFResol")) evar().EnableEventVariation(SystId::kToFResol);
449  if(ND::params().GetParameterI("psycheSteering.Variations.EnableECalMomScale")) evar().EnableEventVariation(SystId::kECalEMScale);
450  if(ND::params().GetParameterI("psycheSteering.Variations.EnableECalMomResol")) evar().EnableEventVariation(SystId::kECalEMResol);
451 
452  if(ND::params().GetParameterI("psycheSteering.Weights.EnableChargeConf")) eweight().EnableEventWeight(SystId::kChargeIDEff);
453  if(ND::params().GetParameterI("psycheSteering.Weights.EnableTpcClusterEff")) eweight().EnableEventWeight(SystId::kTpcClusterEff);
454  if(ND::params().GetParameterI("psycheSteering.Weights.EnableTpcTrackEff")) eweight().EnableEventWeight(SystId::kTpcTrackEff);
455  if(ND::params().GetParameterI("psycheSteering.Weights.EnableTpcFgdMatchEff")) eweight().EnableEventWeight(SystId::kTpcFgdMatchEff);
456  if(ND::params().GetParameterI("psycheSteering.Weights.EnableFgdTrackEff")) eweight().EnableEventWeight(SystId::kFgdTrackEff);
457  if(ND::params().GetParameterI("psycheSteering.Weights.EnableFgdHybridTrackEff")) eweight().EnableEventWeight(SystId::kFgdHybridTrackEff);
458  if(ND::params().GetParameterI("psycheSteering.Weights.EnableMichelEleEff")) eweight().EnableEventWeight(SystId::kMichelEleEff);
459  if(ND::params().GetParameterI("psycheSteering.Weights.EnablePileUp")) eweight().EnableEventWeight(SystId::kPileUp);
460  if(ND::params().GetParameterI("psycheSteering.Weights.EnableFgdMass")) eweight().EnableEventWeight(SystId::kFgdMass);
461  if(ND::params().GetParameterI("psycheSteering.Weights.EnableOOFV")) eweight().EnableEventWeight(SystId::kOOFV);
462  if(ND::params().GetParameterI("psycheSteering.Weights.EnableSIPion")) eweight().EnableEventWeight(SystId::kSIPion);
463  if(ND::params().GetParameterI("psycheSteering.Weights.EnableSIProton")) eweight().EnableEventWeight(SystId::kSIProton);
464  if(ND::params().GetParameterI("psycheSteering.Weights.EnableSandMuons")) eweight().EnableEventWeight(SystId::kSandMu);
465  if(ND::params().GetParameterI("psycheSteering.Weights.EnableECalPID")) eweight().EnableEventWeight(SystId::kECalPID);
466  if(ND::params().GetParameterI("psycheSteering.Weights.EnableTpcECalMatchEff")) eweight().EnableEventWeight(SystId::kTpcECalMatchEff);
467 
468  if(ND::params().GetParameterI("psycheSteering.Weights.EnableECalEmHipPID")) eweight().EnableEventWeight(SystId::kECalEmHipPID);
469  if(ND::params().GetParameterI("psycheSteering.Weights.EnableFGD2Shower")) eweight().EnableEventWeight(SystId::kFGD2Shower);
470  if(ND::params().GetParameterI("psycheSteering.Weights.EnableNuETPCPileUp")) eweight().EnableEventWeight(SystId::kNuETPCPileUp);
471  if(ND::params().GetParameterI("psycheSteering.Weights.EnableNuEP0DPileUp")) eweight().EnableEventWeight(SystId::kNuEP0DPileUp);
472  if(ND::params().GetParameterI("psycheSteering.Weights.EnableNuEECalPileUp")) eweight().EnableEventWeight(SystId::kNuEECalPileUp);
473  if(ND::params().GetParameterI("psycheSteering.Weights.EnableNuEOOFV")) eweight().EnableEventWeight(SystId::kNuEOOFV);
474 
475  if(ND::params().GetParameterI("psycheSteering.Weights.EnableFluxNeutrino")) syst().EnableSystematic(SystId::kFluxWeightNu);
476  if(ND::params().GetParameterI("psycheSteering.Weights.EnableFluxAntiNeutrino")) syst().EnableSystematic(SystId::kFluxWeightAntiNu);
477 
478  // Construct the Cov. matrix
480 
481  // Temporary: We need to add weights and variations to the systematic manager. TODO
482  for (UInt_t i=0;i<eweight().GetNEventWeights();i++){
484  syst().AddWeightSystematic(it->GetIndex(), it->GetName(), it);
485  }
486  for (UInt_t i=0;i<evar().GetNEventVariations();i++){
488  syst().AddVariationSystematic(it->GetIndex(), it->GetName(), it);
489  }
490 
491 }
492 
493 //******************************************************************
494 bool AnalysisManager::ProcessEvent(const ToyExperiment& toy, AnaEventB& event, std::vector<Weight_h>& totalWeightSystVector, std::vector<Weight_h>& fluxWeightSystVector){
495  //******************************************************************
496 
497  // Method not computing the POTweight
498  Float_t POTweight;
499  return ProcessEvent(toy,event,totalWeightSystVector,fluxWeightSystVector,POTweight);
500 }
501 
502 //******************************************************************
504  //******************************************************************
505 
506  // Configure the toy experiment. variation for each of the systematics
507  Float_t POTweight;
508  // Process the current event (bunch). That means applying the systematics, the selections and computing the weights
509  return ProcessEvent(event, POTweight);
510 }
511 
512 //******************************************************************
513 bool AnalysisManager::ProcessEvent(const ToyExperiment& toy, AnaEventB& event, std::vector<Weight_h>& totalWeightSystVector, std::vector<Weight_h>& fluxWeightSystVector, Float_t& POTweight){
514  //******************************************************************
515 
516  /*
517  This Method Process one event. That means:
518 
519  1. Apply systematic variations
520  2. Loop over selections. And for each selection:
521  2a. Apply all steps
522  2b. Compute the systematic weight when all cuts are passed
523 
524  Input:
525  - AnaEventB, but the one to be modified (event). Contains the SystBox
526 
527  - ToyExperiment, the toy experiment definition with the variation for each systematic (toy)
528  Output:
529  - EventSummary (part of the AnaEventB) containing high level info (candidate, etc)
530  - The vector of weights for each selection
531 
532  Return value:
533  - true when at least one selection has been passed
534 
535  */
536  bool passed=false;
537 
538  /// The vector of systematic weights (one entry per systematic)
539  Int_t nWeightSyst= eweight().GetNEnabledEventWeights();
540  Weight_h* weightSystDummy;
541  anaUtils::CreateArray(weightSystDummy, nWeightSyst);
542 
543  /// Clear the vector of weight and flux systematics (a entry per selection)
544  totalWeightSystVector.clear();
545  fluxWeightSystVector.clear();
546 
547  /// Apply the systematics variations (for all selections)
548  /// The event will be modified. All actions below should proceed on the modified event
549  if (_applyEventVariations && event.GetIsMC()){
550  // First, reset the event to the original data
551  // if (syst().UndoEventVariations(event)) _input.ResetSpillToRaw();
552  // Now apply variations to original spill
553  evar().ApplyEventVariations(toy, event);
554  }
555 
556 
557  // Reset event summary sample before applying all selections
558  static_cast<AnaEventSummaryB*>(event.Summary)->ResetSummary();
559 
560  /// Loop over selections
561  for (std::vector<SelectionBase*>::iterator it=sel().GetSelections().begin();it!=sel().GetSelections().end();it++){
562  SelectionBase& selec = **it;
563 
564  // only enabled selections
565  if (!selec.IsEnabled()) continue;
566  // check we should apply this selection to this run period
567  if (!CheckSelectionAgainstRunPeriod(*it, anaUtils::GetRunPeriod(static_cast<AnaEventB*>(&event)->EventInfo.Run))) continue;
568 
569  /// Initialize the Flux Weight Systematic to 1 (in the case it is not applied)
570  Weight_h fluxWeightSyst=1.;
571 
572  /// Initialize the total weight of Weight Systematics to 1 (in the case they are not applied)
573  Weight_h totalWeightSyst = 1;
574  bool redo;
575  bool passed_temp = selec.Apply(event,redo);
576 
577  if (passed_temp && event.GetIsMC()){
578 
579  /// Apply the flux weight
580  if (_flux && event.Summary && !event.GetIsSandMC())
581  if (static_cast<AnaEventSummaryB*>(event.Summary)->TrueVertex[selec.GetSampleId()])
582  fluxWeightSyst *=_flux->GetWeight(static_cast<AnaEventSummaryB*>(event.Summary)->TrueVertex[selec.GetSampleId()], anaUtils::GetRunPeriod(static_cast<AnaEventB*>(&event)->EventInfo.Run));
583 
584  /// Compute the weight for this selection by applying the systematic weights.
586  // apply the detector weight systematics
587  if (_applyEventWeights){
588  totalWeightSyst = eweight().ComputeEventWeights(selec, toy, event, selec.GetPreviousToyBox(event), weightSystDummy);
589  }
590  // apply the Flux systematic independently
591  if (_applyFluxWeightSystematic && !event.GetIsSandMC()){
592  fluxWeightSyst *= syst().ApplyFluxSystematics(toy, event, selec.GetPreviousToyBox(event));
593  }
594  }
595  }
596 
597  // add the total weight and flux weight to the vector
598  totalWeightSystVector.push_back(totalWeightSyst);
599  fluxWeightSystVector.push_back(fluxWeightSyst);
600 
601  // If any of the selections is passed the overall passed is true
602  if (passed_temp) passed=true;
603  }
604 
605  // computes the POT normalization for the current event
606  if (_currentSampleGroup){
607  Float_t POTdata=0;
608  Float_t POTmc=0;
609  Float_t POTsand=0;
610  _currentSampleGroup->GetPOT(POTdata, POTmc, POTsand);
611  POTweight = POTdata/POTmc;
612  }
613 
614  delete [] weightSystDummy;
615 
616  return passed;
617 }
618 
619 //******************************************************************
620 bool AnalysisManager::ProcessEvent(const ToyExperiment& toy, AnaEventB& event, Weight_h& totalweight,Weight_h& fluxWeightSyst){
621  //******************************************************************
622 
623  /*
624  This Method Process one event. That means:
625 
626  1. Apply systematic variations
627  2. Loop over selections. And for each selection:
628  2a. Apply all steps
629  2b. Compute the systematic weight when all cuts are passed
630 
631  Input:
632  - AnaEventB, but the one to be modified (event). Contains the SystBox
633 
634  - ToyExperiment, the toy experiment definition with the variation for each systematic (toy)
635  Output:
636  - EventSummary (part of the AnaEventB) containing high level info (candidate, etc)
637  - The vector of weights for each selection
638 
639  Return value:
640  - true when at least one selection has been passed
641 
642  */
643  bool passed=false;
644 
645 
646  /// Apply the systematics variations (for all selections)
647  /// The event will be modified. All actions below should proceed on the modified event
648  if (evar().GetNEventVariations() && _applyEventVariations && event.GetIsMC()){
649  // First, reset the event to the original data
650  // if (syst().UndoEventVariations(event)) _input.ResetSpillToRaw();
651  //GetSystematicVariationIndex(const std::string& name)
652  // Now apply variations to original spill
653  evar().ApplyEventVariations(toy, event);
654  }
655 
656  // Reset event summary sample before applying all selections
657  static_cast<AnaEventSummaryB*>(event.Summary)->ResetSummary();
658 
659  Int_t nWeightSyst= eweight().GetNEnabledEventWeights();
660  Weight_h* weightSystDummy;
661  anaUtils::CreateArray(weightSystDummy, nWeightSyst);
662 
663  /// Loop over selections
664  for (std::vector<SelectionBase*>::iterator it=sel().GetSelections().begin();it!=sel().GetSelections().end();it++){
665  SelectionBase& selec = **it;
666 
667  // only enabled selections
668  if (!selec.IsEnabled()) continue;
669  // check we should apply this selection to this run period
670  if (!CheckSelectionAgainstRunPeriod(*it, anaUtils::GetRunPeriod(static_cast<AnaEventB*>(&event)->EventInfo.Run))) continue;
671 
672  /// Initialize the Flux Weight Systematic to 1 (in the case it is not applied)
673  fluxWeightSyst=1.;
674 
675  /// Initialize the total weight of Weight Systematics to 1 (in the case they are not applied)
676  totalweight = 1;
677 
678  bool redo;
679  bool passed_temp = selec.Apply(event,redo);
680 
681  if(passed_temp && passed) std::cout << "DOUBLE EVENT SELECTION ON selec.GetSampleId() = " << selec.GetSampleId() << std::endl;
682  if(passed_temp) passed = 1;
683 
684  if (passed_temp && event.GetIsMC()){
685 
686  /// Apply the flux weight
687  if (_flux && event.Summary)
688  if (static_cast<AnaEventSummaryB*>(event.Summary)->TrueVertex[selec.GetSampleId()] && !event.GetIsSandMC())
689  fluxWeightSyst *=_flux->GetWeight(static_cast<AnaEventSummaryB*>(event.Summary)->TrueVertex[selec.GetSampleId()], anaUtils::GetRunPeriod(static_cast<AnaEventB*>(&event)->EventInfo.Run));
690 
691  /// Compute the weight for this selection by applying the systematic weights.
693  // apply the detector weight systematics
694  if (_applyEventWeights){
695  totalweight = eweight().ComputeEventWeights(selec,toy, event, selec.GetPreviousToyBox(event),weightSystDummy);
696  }
697  // apply the Flux systematic independently
698  if (_applyFluxWeightSystematic && !event.GetIsSandMC()){
699  fluxWeightSyst *= syst().ApplyFluxSystematics(toy, event, selec.GetPreviousToyBox(event));
700  }
701  }
702  }
703 
704  //weights.push_back(weight);
705  // If any of the selections is passed the overall passed is true
706  if (passed_temp) return true;
707  }
708  return passed;
709 }
710 
711 //******************************************************************
712 bool AnalysisManager::ProcessEvent( AnaEventB& event, Float_t& POTweight){
713  //******************************************************************
714 
715  /*
716  This Method Process one event. That means:
717 
718  2. Loop over selections. And for each selection:
719  2a. Apply all steps
720 
721  Input:
722  - AnaEventB, but the one to be modified (event). Contains the SystBox
723 
724  Output:
725  - EventSummary (part of the AnaEventB) containing high level info (candidate, etc)
726 
727  Return value:
728  - true when at least one selection has been passed
729 
730  */
731  bool passed=false;
732  /// Loop over selections
733 
734  // Reset event summary sample before applying all selections
735  static_cast<AnaEventSummaryB*>(event.Summary)->ResetSummary();
736 
737  for (std::vector<SelectionBase*>::iterator it=sel().GetSelections().begin();it!=sel().GetSelections().end();it++){
738  SelectionBase& selec = **it;
739  // only enabled selections
740  if (!selec.IsEnabled()) continue;
741  if (!CheckSelectionAgainstRunPeriod(*it, anaUtils::GetRunPeriod(static_cast<AnaEventB*>(&event)->EventInfo.Run))) continue;
742 
743  /// Apply the selection
744  bool redo;
745  if (selec.Apply(event, redo)) passed=true;
746  }
747 
748  // computes the POT normalization for the current event
749  if (_currentSampleGroup){
750  Float_t POTdata=0;
751  Float_t POTmc=0;
752  Float_t POTsand=0;
753  _currentSampleGroup->GetPOT(POTdata, POTmc, POTsand);
754  POTweight = POTdata/POTmc;
755  }
756 
757  return passed;
758 }
759 
760 //******************************************************************
761 bool AnalysisManager::LoadEvent(Long64_t& entry){
762  //******************************************************************
763 
764  AnaSuperEventB* event = LoadSuperEvent(entry);
765  if(event==NULL) return false;
766 
767  return true;
768 }
769 //******************************************************************
770 bool AnalysisManager::Initialize(Int_t nmax_events){
771  //******************************************************************
772 
773  //Start by setting up the AnaSuperEventB* array to be large enough to take
774  //nmax_events events
775  CreateEventArray(nmax_events);
776 
777  // Create the array of PreviousToyBox
778  sel().CreateToyBoxArray(nmax_events);
779 
780  // Create the array of SystBox
781  evar().Initialize(nmax_events);
782  eweight().Initialize(sel(),nmax_events);
783  return true;
784 }
785 //******************************************************************
786 bool AnalysisManager::ReadEvents(Int_t nmax_events){
787  //******************************************************************
788 
789  // Fill the vector of samples only the first time
790  if (_samples.size() == 0){
791  std::map< std::string, SampleGroup >& sampleGroups = _exp->GetSampleGroups();
792  std::map< std::string, SampleGroup >::iterator sit;
793  for (sit = sampleGroups.begin(); sit != sampleGroups.end(); sit++) {
794  SampleGroup& sampleGroup = sit->second;
795 
796  std::vector<DataSample*>& dataSamples = sampleGroup.GetDataSamples();
797  std::vector<DataSample*>::iterator it;
798  for (it = dataSamples.begin(); it != dataSamples.end(); it++) {
799  DataSample* sample = *it;
800  _samples.push_back(sample);
801  }
802 
803  std::map< std::string, DataSample*>& mcSamples = sampleGroup.GetMCSamples();
804  std::map< std::string, DataSample*>::iterator it2;
805  for (it2 = mcSamples.begin(); it2 != mcSamples.end(); it2++) {
806  std::string name = it2->first;
807  DataSample* sample = it2->second;
808  _samples.push_back(sample);
809  }
810  }
811  _currentSample = _samples.begin();
812  }
813  else
814  _currentSample++;
815 
816 
817  _currentSampleGroup = &(_exp->GetSampleGroup("run1"));
818 
819  // Preload events from the current sample
820  return ReadEvents(*_currentSample, nmax_events);
821 
822 }
823 
824 //******************************************************************
825 bool AnalysisManager::ReadEvents(DataSample* sample, Int_t nmax_events){
826  //******************************************************************
827 
828  // Preload in _eventArray the events for the input sample
829 
830  if (!ReadEvents(sample->GetFilePath(),nmax_events)) return false;
831 
832  Int_t nentries = (Int_t)_input.GetEntries();
833  sample->SetNEntries(nentries);
834 
835  return true;
836 }
837 
838 //******************************************************************
839 bool AnalysisManager::ReadEvents(const std::string& inputFile, Int_t nmax_events){
840  //******************************************************************
841 
842  // Preload in _eventArray the events for the sample in the inputFile
843 
844  std::cout << "AnalysisManager::ReadEvents(). Reading file " << inputFile << std::endl;
845 
846  // Open the input file
847  if(!_input.Initialize(inputFile.c_str(),"")) return false;
848 
849  // Get the number of entries in the input tree
850  Int_t nentries = (Int_t)_input.GetEntries();
851 
852  // Get the number of events in the input tree
853  Int_t nEventsInTree = _input.GetNEvents();
854 
855  // just in case we don't run over all entries
856  if (nmax_events !=-1)
857  nmax_events = std::min(nmax_events-_nEventsProcessed, nEventsInTree);
858  else
859  nmax_events = nEventsInTree;
860 
861  std::cout << "AnalysisManager::ReadEvents(). Preload " << nmax_events << " events" << std::endl;
862 
863  Initialize(nmax_events);
864 
865  Long64_t entry=0;
866  _nEventsInArray=0;
867  while((Int_t)_nEventsInArray<nmax_events && entry<nentries){
868  // Fill the event structure for the current event (don't delete the previous event)
869  if (!_input.LoadEvent(entry,false)) continue;
870 
871  // Dump info about number of entries run
872  if (entry%10000==0 || entry == nentries)
873  std::cout << "entry: " << entry << " of " << nentries << " (" << (100*entry/nentries) << "%)" << std::endl;
874 
875  // Get the SuperEvent and save it into the array
876  AnaSuperEventB* event = &_input.GetSuperEvent();
877 
878  // Initialize the event for all enabled selections. That means filling the EventBox
879  sel().InitializeEvent(*(event->Event));
880 
881  // Initialize The SystBox for variation systematics
882  evar().InitializeEvent(sel(),*(event->Event));
883  eweight().InitializeEvent(sel(),*(event->Event));
884 
885  _eventArray[_nEventsInArray] = event;
886  _nEventsInArray++;
887  }
888 
889  std::cout << "AnalysisManager::ReadEvents(). Preloading done of " << _nEventsInArray << " events"<< std::endl;
890 
891  //Reset the currentEvent
892  _currentEvent=-1;
893 
894  return true;
895 }
896 
897 //******************************************************************
899  //******************************************************************
900 
901  // This function returns the next SuperEvent in the sample list
902 
903  // When we arrive to the last event
904  if (_nEventsProcessed == _nEventsToProcess) return NULL;
905 
906  // increase the current event number
907  _currentEvent++;
908 
909  // increase the number of events processed
911 
912  // Print the entry number
913  // Comment out for now - unless processing lots of events O(100000) this just prints out 0% complete repeatedly
914  // if (_currentEvent%10000==0 || _currentEvent == _nEventsToProcess)
915  // std::cout << "entry: " << _currentEvent << " of " << _nEventsToProcess << " (" << (100*_currentEvent/(_nEventsToProcess)) << "%)" << std::endl;
916  return _eventArray[_currentEvent];
917 }
918 
919 //******************************************************************
921  //******************************************************************
922 
923  AnaSuperEventB* sevent = GetNextSuperEvent();
924  if (sevent){
925  return static_cast<AnaEventB*>(sevent->Event);
926  }
927  else{
928  return NULL;
929  }
930 }
931 
932 //******************************************************************
934  //******************************************************************
935  // Fill the event structure for the current event
936  if (!_input.LoadEvent(entry,true)) return NULL;
937 
938  // Get the SuperEvent
939  AnaSuperEventB* event = &_input.GetSuperEvent();
940 
941  if(event->Event->GetIsMC()){
942  if(_POT_weights.size()) event->POTWeight = _POT_weights[_input.GetChain(_input.GetConverter().GetTreeName().c_str())->GetTreeNumber()];
943  }
944 
945  return event;
946 }
947 
948 
949 //******************************************************************
951  //******************************************************************
952 
953  AnaSuperEventB* sevent = GetSuperEvent(evtIndex);
954  if (sevent) return static_cast<AnaEventB*>(sevent->Event);
955  else return NULL;
956 }
957 
958 //******************************************************************
960  //******************************************************************
961  return _input.GetEntries();
962 }
963 
964 //******************************************************************
965 void AnalysisManager::CreateEventArray(Int_t nmax_events){
966  //******************************************************************
967 
968  if (_eventArray){
969  // Delete all AnaSuperEventB in the array
970  for (UInt_t ev = 0; ev<_nEventsInArray;ev++){
971  // The RawEvent must be reset manually (TODO)
972  delete _eventArray[ev]->RawEvent;
973  delete _eventArray[ev];
974  }
975  // Delete the array itself
976  delete _eventArray;
977  }
978 
979  // Create a new event array
980  _eventArray = new AnaSuperEventB*[nmax_events];
981 }
982 
983 
984 //******************************************************************
985 void AnalysisManager::PreloadEvents(bool preloadData, bool preloadMC){
986  //******************************************************************
987 
988  if(!_exp){
989  std::cout << "There is no Experiment associated with this AnalysisManager, cannot preload events this way. Use ReadEvents(int nevents) method instead." << std::endl;
990  return;
991  }
992 
993  // Fill the vector of samples only the first time
994  std::map< std::string, SampleGroup >& sampleGroups = _exp->GetSampleGroups();
995  std::map< std::string, SampleGroup >::iterator sit;
996  for (sit = sampleGroups.begin(); sit != sampleGroups.end(); sit++) {
997  SampleGroup& sampleGroup = sit->second;
998 
999  Float_t POTdata=0;
1000  Float_t POTmc=0;
1001  Float_t POTsand=0;
1002 
1003  sampleGroup.GetPOT(POTdata, POTmc, POTsand);
1004 
1005  float POT_weight = POTdata/POTmc;
1006  float POT_weight_sand = POTdata/POTsand;
1007 
1008  std::map< std::string, DataSample*>& mcSamples = sampleGroup.GetMCSamples();
1009  std::map< std::string, DataSample*>::iterator it2;
1010  for (it2 = mcSamples.begin(); it2 != mcSamples.end(); it2++) {
1011  if((it2->first).find("and")!=std::string::npos){
1012  _POT_weights.push_back(POT_weight_sand);
1013  }
1014  else{
1015  _POT_weights.push_back(POT_weight);
1016  }
1017  }
1018  }
1019 
1020  if(preloadMC) PreloadMCEvents(_exp->GetMCFileVector(), _POT_weights);
1021  if(preloadData) PreloadDataEvents(_exp->GetDataFileVector());
1022 }
1023 
1024 //******************************************************************
1025 bool AnalysisManager::PreloadMCEvents(std::vector<std::string> inputFiles, std::vector<float> POT_weights){
1026  //******************************************************************
1027 
1028  int nEntries = 0;
1029  int nmax_events = 0;
1030 
1031  for(std::vector<std::string>::iterator it = inputFiles.begin(); it != inputFiles.end(); ++it){
1032 
1033  std::string inputFile = *it;
1034  // Open the input file
1035  if(!_input.Initialize(inputFile.c_str(),"")) continue;
1036 
1037  // Get the number of entries in the input tree
1038  nEntries += (Int_t)_input.GetEntries();
1039 
1040  // Get the number of events in the input tree
1041  nmax_events += _input.GetNEvents();
1042  }
1043 
1044 
1045  //Start by setting up the AnaSuperEventB* array to be large enough to take
1046  //nmax entries.
1047  CreateMCEventArray(nmax_events);
1048 
1050 
1051  // Create the array of PreviousToyBox
1052  sel().CreateToyBoxArray(nmax_events);
1053 
1054  // Create the array of SystBox
1055  evar().Initialize(nmax_events);
1056  eweight().Initialize(sel(),nmax_events);
1057 
1058 
1059  for(int i = 0; i < (int)inputFiles.size(); ++i){
1060  std::string inputFile = inputFiles[i];
1061  float POT_weight = POT_weights[i];
1062 
1063  // Preload in _eventArray the events for the sample in the inputFile
1064  std::cout << "AnalysisManager::PreloadMCEvents(). Reading file " << inputFile << std::endl;
1065 
1066  // Open the input file
1067  if(!_input.Initialize(inputFile.c_str(),"")) continue;
1068 
1069  Long64_t entry=0;
1070 
1071  int nmax_entries = _input.GetEntries();
1072 
1073  while((int)entry < nmax_entries){
1074 
1075  // Fill the event structure for the current event (don't delete the previous event)
1076  if (!_input.LoadEvent(entry,false)) continue;
1077 
1078  // Dump info about number of entries run
1079  if (entry%10000==0 || entry == nmax_entries)
1080  std::cout << "entry: " << entry << " of " << nmax_entries << " (" << (100*entry/nmax_entries) << "%)" << std::endl;
1081 
1082  // Get the SuperEvent and save it into the array
1083  AnaSuperEventB* event = &_input.GetSuperEvent();
1084 
1085  // Initialize the event for all enabled selections. That means filling the EventBox
1086  sel().InitializeEvent(*(event->Event));
1087 
1088  // Initialize The SystBox for variation systematics
1089  evar().InitializeEvent(sel(),*(event->Event));
1090  eweight().InitializeEvent(sel(),*(event->Event));
1091 
1092  event->POTWeight = POT_weight;
1095  }
1096  }
1097 
1098  std::cout << "AnalysisManager::PreloadMCEvents(). Preloading done" << std::endl;
1099  return true;
1100 }
1101 
1102 //******************************************************************
1103 bool AnalysisManager::PreloadDataEvents(std::vector<std::string> inputFiles){
1104  //******************************************************************
1105 
1106  int nEntries = 0;
1107  int nmax_events = 0;
1108 
1109  for(std::vector<std::string>::iterator it = inputFiles.begin(); it != inputFiles.end(); ++it){
1110 
1111  std::string inputFile = *it;
1112 
1113  // Open the input file
1114  if(!_input.Initialize(inputFile.c_str(),"")) continue;
1115 
1116  // Get the number of entries in the input tree
1117  nEntries += (Int_t)_input.GetEntries();
1118 
1119  // Get the number of events in the input tree
1120  nmax_events += _input.GetNEvents();
1121  }
1122 
1123  //Start by setting up the AnaSuperEventB* array to be large enough to take
1124  //nmax entries.
1125  CreateDataEventArray(nmax_events);
1126 
1128 
1129  for(std::vector<std::string>::iterator it = inputFiles.begin(); it != inputFiles.end(); ++it){
1130 
1131  std::string inputFile = *it;
1132 
1133  // Preload in _eventArray the events for the sample in the inputFile
1134  std::cout << "AnalysisManager::PreloadDataEvents(). Reading file " << inputFile << std::endl;
1135 
1136  // Open the input file
1137  if(!_input.Initialize(inputFile.c_str(),"")) continue;
1138 
1139  Long64_t entry=0;
1140 
1141  int nmax_entries = _input.GetEntries();
1142 
1143  while((int)entry < nmax_entries){
1144 
1145  // Fill the event structure for the current event (don't delete the previous event)
1146  if (!_input.LoadEvent(entry,false)) continue;
1147 
1148  // Dump info about number of entries run
1149  if (entry%10000==0 || entry == nmax_entries)
1150  std::cout << "entry: " << entry << " of " << nmax_entries << " (" << (100*entry/nmax_entries) << "%)" << std::endl;
1151 
1152 
1153  // Get the SuperEvent and save it into the array
1154  AnaSuperEventB* event = &_input.GetSuperEvent();
1157  }
1158  }
1159 
1160  std::cout << "AnalysisManager::PreloadDataEvents(). Preloading done" << std::endl;
1161  return true;
1162 }
1163 
1164 //******************************************************************
1165 void AnalysisManager::InitialiseMCTChain(){
1166  //******************************************************************
1167 
1168  _input.Reset();
1169 
1171 
1172  std::map< std::string, SampleGroup >& sampleGroups = _exp->GetSampleGroups();
1173  std::map< std::string, SampleGroup >::iterator sit;
1174  for (sit = sampleGroups.begin(); sit != sampleGroups.end(); sit++) {
1175  SampleGroup& sampleGroup = sit->second;
1176 
1177  std::map< std::string, DataSample*>& mcSamples = sampleGroup.GetMCSamples();
1178  std::map< std::string, DataSample*>::iterator it2;
1179  for (it2 = mcSamples.begin(); it2 != mcSamples.end(); it2++){
1180  DataSample* sample = it2->second;
1181  if(!_input.AddDataFileToConverter(sample->GetFilePath().c_str(),"")) continue;
1182  }
1183  }
1184  std::cout << "AnalysisManager::InitialiseMCTChain(). TChain has all MC files added." << std::endl;
1185  _nEventsInMCArray = (Int_t)_input.GetNEvents();
1186 }
1187 
1188 //******************************************************************
1189 void AnalysisManager::InitialiseDataTChain(){
1190  //******************************************************************
1191  std::vector<std::string> inputFiles;
1192  inputFiles = _exp->GetDataFileVector();
1193 
1194 
1195  _input.Reset();
1196 
1198  for(std::vector<std::string>::iterator it = inputFiles.begin(); it != inputFiles.end(); ++it){
1199  std::string inputFile = *it;
1200  // Open the input file
1201  if(!_input.AddDataFileToConverter(inputFile.c_str(),"")) continue;
1202  }
1203 
1204  std::cout << "AnalysisManager::InitialiseDataTChain(). TChain has all data files added." << std::endl;
1206 }
1207 
1208 //******************************************************************
1209 AnaSuperEventB* AnalysisManager::GetPreloadedMCSuperEvent(int i){
1210  //******************************************************************
1211  if(i >= (int)_nEventsInMCArray){
1212  std::cout << "Requesting event " << i << " when the MC preloaded event array only has " << _nEventsInMCArray << " entries." << std::endl;
1213  return NULL;
1214  }
1215  return _mcEventArray[i];
1216 }
1217 
1218 //******************************************************************
1219 AnaSuperEventB* AnalysisManager::GetPreloadedDataSuperEvent(int i){
1220  //******************************************************************
1221  if(i >= (int)_nEventsInDataArray){
1222  std::cout << "Requesting event " << i << " when the Data preloaded event array only has " << _nEventsInDataArray << " entries." << std::endl;
1223 
1224  return NULL;
1225  }
1226  return _dataEventArray[i];
1227 }
1228 
1229 //******************************************************************
1230 void AnalysisManager::CreateMCEventArray(Int_t nmax){
1231  //******************************************************************
1232 
1233  if (_mcEventArray){
1234  // Delete all AnaSuperEventB in the array
1235  for (UInt_t ev = 0; ev<_nEventsInMCArray;ev++){
1236  delete _mcEventArray[ev]->RawEvent;
1237  delete _mcEventArray[ev];
1238  }
1239  // Delete the array itself
1240  delete _mcEventArray;
1241  }
1242 
1243  // Create a new event array
1244  _mcEventArray = new AnaSuperEventB*[nmax];
1245 }
1246 
1247 //******************************************************************
1248 void AnalysisManager::CreateDataEventArray(Int_t nmax){
1249  //******************************************************************
1250 
1251  if (_dataEventArray){
1252  // Delete all AnaSuperEventB in the array
1253  for (UInt_t ev = 0; ev<_nEventsInDataArray;ev++){
1254  delete _dataEventArray[ev]->RawEvent;
1255  delete _dataEventArray[ev];
1256  }
1257  // Delete the array itself
1258  delete _dataEventArray;
1259  }
1260  // Create a new event array
1261  _dataEventArray = new AnaSuperEventB*[nmax];
1262 }
1263 
1264 //******************************************************************
1265 void AnalysisManager::ResetEventsProcessed(){
1266  //******************************************************************
1267  _currentEvent = -1;
1268  _nEventsProcessed = 0;
1269 }
1270 
1271 //******************************************************************
1273  //******************************************************************
1274  return selec->IsValidRun(RunPeriod);
1275 }
1276 
1277 
1278 
bool ReadEvents(const std::string &inputFile, Int_t nmax=-1)
Read the specified number of events from the file and save them into an array. Default(-1) is all eve...
void AddWeightSystematic(Int_t index, EventWeightBase *sys)
Register an SystematicBase as a weight systematic.
const std::string & GetFilePath()
returns the filePath
Definition: DataSample.hxx:43
Int_t GetIndex() const
Return the index of this systematic.
int GetParameterI(std::string)
Get parameter. Value is returned as integer.
Definition: Parameters.cxx:217
UInt_t GetNEnabledEventWeights()
Returns the number of enabled EventWeights.
bool _applyEventVariations
whether to apply detector event variation or not
virtual void Reset()
Reset the converters and the UniqueID.
AnaEventC * Event
The modified event.
void CreateToyBoxArray(Int_t nevents)
Create the array of PreviousToyBox for all enabled selections.
TN-152 for a longer explanaition.
UInt_t GetNEventVariations() const
Returns the number of EventVariations.
bool GetIsSandMC() const
Return whether this event is from Sand Monte Carlo or not.
std::vector< EventVariationBase * > & GetEventVariations()
Get the vector of EventVariations.
InputManager _input
An instance of the input manager.
std::vector< Float_t > _POT_weights
The POT weight for each MC file loaded into the Analysis Manager.
bool _applyFluxWeightSystematic
whether to apply Flux weight systematic or not
virtual SampleId_h GetSampleId()
Return the sample type, needed by fitters.
std::vector< DataSample * > & GetDataSamples()
Get all MC samples in a group.
Definition: Experiment.hxx:47
bool LoadEvent(Long64_t &entry, bool delPrevious=true)
UInt_t GetNEventWeights() const
Returns the number of EventWeights.
void InitializeEvent(SelectionManager &sel, AnaEventC &event)
Fill the SystBox for the enabled EventWeights.
bool CheckSelectionAgainstRunPeriod(SelectionBase *selec, int RunPeriod)
void EnableSystematic(Int_t index)
Enable the systematic registered with the given index.
void AddFluxSystematic(Int_t index, EventWeightBase *sys)
Register an SystematicBase as a flux systematic.
UInt_t _nEventsInArray
the number of events in the array
const ToyBoxB & GetPreviousToyBox(const AnaEventC &event) const
Get the ToyBox of the last processed toy for a particular event.
void GetPOT(Float_t &POTdata, Float_t &POTmc)
Get the good data and MC POT for this sample group.
Definition: Experiment.cxx:98
std::vector< SelectionBase * > & GetSelections()
Return the map of selections.
InputManager & input()
Returns the input manager.
bool _applyFluxWeight
Flag to enable/disable flux weight.
std::string GetParameterS(std::string)
Get parameter. Value is returned as string.
Definition: Parameters.cxx:242
AnaEventB * GetNextEvent()
Returns The next Event.
void InitializeEvent(SelectionManager &sel, AnaEventC &event)
Fill the SystBox for the enabled EventVariations.
Weight_h ApplyFluxSystematics(const ToyExperiment &toy, const AnaEventC &event, const ToyBoxB &ToyBox)
Apply all fluxSystematics. Returns the total event Weight.
Int_t GetNEvents(Int_t entries=-1)
Return the number of events for a given number of entries in the input file tree(s).
const std::string & GetTreeName()
returns the name of the tree to convert
AnaSuperEventB ** _dataEventArray
The array of preloaded Data events.
SampleGroup & GetSampleGroup(const std::string &name)
Get a single sample group by name.
Definition: Experiment.hxx:118
bool LoadEvent(Long64_t &entry)
std::map< std::string, DataSample * > & GetMCSamples()
Get all MC samples in a group.
Definition: Experiment.hxx:50
UInt_t _nEventsInMCArray
the number of events in the preloaded MC array
SelectionManager & sel()
Returns the selection manager.
void ConstructCovarianceMatrix()
Make the covariance.
int GetRunPeriod(int run, int subrun=-1)
Returns the run period (sequentially: 0,1,2,3,4,5 ...)
Experiment * _exp
The Experiment.
TChain * GetChain(const std::string &name)
Get a TChain with a specific name from the selected converter.
void DisableAllSystematics()
Disable all Systematics.
EventVariationManager & evar()
Returns the EventVariation manager.
std::vector< std::string > GetDataFileVector()
Return pointer to vector of data files.
Definition: Experiment.hxx:157
This is a normalization systematic. It takes into account the uncertainty on the FGD mass introduced ...
bool ProcessEvent(const ToyExperiment &toy, AnaEventB &event, std::vector< Weight_h > &totalWeightSystVector, std::vector< Weight_h > &fluxWeightSystVector)
Float_t GetWeight(AnaTrueVertexB *vertex, int RunPeriod)
bool Apply(AnaEventC &event, bool &redo)
Apply all steps in the selection.
InputConverter & GetConverter()
Get the current selected converter.
This systematic evaluates the oofv systematic for nue analysis. At the moment a 30% rate unceratinty ...
AnaSuperEventB * LoadSuperEvent(Long64_t &evtIndex)
AnaSuperEventB * GetNextSuperEvent()
Returns The next SuperEvent.
bool GetIsMC() const
Return whether this event is from Monte Carlo or not.
void AddEventVariation(Int_t index, EventVariationBase *sys)
Add a new Event Variation provided its index in the manager and a pointer to it.
AnaSuperEventB ** _eventArray
The array of events.
AnaSuperEventB & GetSuperEvent()
Create the event.
void AddEventWeight(Int_t index, EventWeightBase *sys)
Add a new Event Weight provided its index in the manager and a pointer to it.
void SetNEntries(Int_t en)
Set and Get the special normalisation to be used for this sample.
Definition: DataSample.hxx:46
Long64_t GetEntries()
Return the number of entries in the input file tree(s).
bool AddDataFileToConverter(const std::string &infile_name, const std::string &conv, const bool isCosmic=false, bool reset=false)
AnaEventC * RawEvent
The Raw event.
AnaEventSummaryC * Summary
A summary of the event with high level quantities.
AnaEventB * GetEvent(Int_t eventIndex)
Returns The Event with a given index from the array.
void CreateEventArray(Int_t nmax)
Create the evevt array for preloading events.
void AddSelection(const std::string &name, const std::string &title, SelectionBase *sel, Int_t presel=-1)
Add a user selection to the selection manager.
UInt_t GetEntries()
Returns the number of entries in the input tree.
Int_t _nEventsProcessed
the number of events processed so far
void ApplyEventVariations(const ToyExperiment &toy, AnaEventC &event)
Apply all EventVariations.
TN-152 for a longer explanaition.
Charge confusion systematic. This is treated as an efficiency systematic, applying a weight to the ev...
Int_t _currentEvent
The current event in the event array.
UInt_t _nEventsInDataArray
the number of events in the preloaded Data array
This systematic smears the pull of each FGD track segment.
bool Initialize(const std::string &infile_name, const std::string &conv, const bool isCosmic=false)
void AddConverter(const std::string &name, InputConverter *conv)
Add this converter to the vector of recognised converters.
bool IsValidRun(int runPeriod) const
Method to see whether this selection should be applied to the given run period.
std::vector< EventWeightBase * > & GetEventWeights()
Get the vector of EventWeights.
void EnableEventWeight(Int_t index)
Enable the EventWeight registered with the given index.
void DisableSelection(const std::string &sel)
Disable a selection.
std::string _fluxFile
Flux file and option.
AnaSuperEventB ** _mcEventArray
The array of preloaded MC events.
FluxWeighting * _flux
Access to the flux weighting.
Weight_h ComputeEventWeights(const ToyExperiment &toy, const AnaEventC &event, const ToyBoxB &ToyBox)
Compute all eventWeights. Returns the total event normalization weight.
void AddVariationSystematic(Int_t index, EventVariationBase *sys)
Register an EventVariationBase as a variation systematic.
std::vector< std::string > GetMCFileVector()
Return pointer to vector of mc files.
Definition: Experiment.hxx:154
std::map< std::string, SampleGroup > & GetSampleGroups()
Returns all sample groups.
Definition: Experiment.hxx:115
void EnableEventVariation(Int_t index)
Enable the EventVariation registered with the given index.
SystematicManager & syst()
Returns the systematic manager.
This systematic smears the CT of each TPC track segment.
EventWeightManager & eweight()
Returns the EventWeight manager.
Michel electron effciency systematic.
virtual const char * GetName() const
Return the name of this systematic. This overrides the TObject::GetName() interface.
Int_t _nEventsToProcess
the maximum number of events to process
void SetValidRunPeriods(const std::string &ssel1, const std::string validRunPeriods)
Method to set the valid run periods for this selection (e.g. Anti-neutrino selections should only be ...
AnaSuperEventB * GetSuperEvent(Int_t eventIndex)
Returns The SuperEvent with a given index from the array.
void InitializeEvent(AnaEventC &event)
Initialize the EventBox for all enabled selections.
bool _applyEventWeights
whether to apply detector event weight or not