HighLAND
CreateMiniTree.cxx
1 #include "CreateMiniTree.hxx"
2 #include "InputManager.hxx"
3 #include "FlatTreeConverter.hxx"
4 #include "HighlandMiniTreeConverter.hxx"
5 #include "oaAnalysisTreeConverter.hxx"
6 #include "Parameters.hxx"
7 #include "Versioning.hxx"
8 #include "GeometryManager.hxx"
9 #include "HighlandAnalysisUtils.hxx"
10 
11 #include "CorrId.hxx"
12 
13 // Corrections
14 #include "CT4POTCorrection.hxx"
15 #include "DataQualityCorrection.hxx"
16 #include "PileUpCorrection.hxx"
17 #include "TPCdEdxDataCorrection.hxx"
18 #include "TPCdEdxMCCorrection.hxx"
19 #include "TPCExpecteddEdxCorrection.hxx"
20 #include "IgnoreRightECalRuns3and4Correction.hxx"
21 #include "UseGlobalAltMomCorrection.hxx"
22 #include "FlipKinematicsCorrection.hxx"
23 #include "ToFCorrection.hxx"
24 #include "MomResolMCCorrection.hxx"
25 #include "MomRangeCorrection.hxx"
26 #include "TPCPIDPullMCCorrection.hxx"
27 
28 
29 
30 Int_t NVtx;
31 TClonesArray* Vtx;
32 Int_t RunID;
33 Int_t SubrunID;
34 Int_t EventID;
35 bool Preselected;
36 
37 //********************************************************************
38 CreateMiniTree::CreateMiniTree(int argc, char *argv[]):SimpleLoopBase(argc, argv){
39  //********************************************************************
40 
41  // Add the different productions
42  ND::versioning().AddProduction(ProdId::PROD5E, "PROD5E", "v10r11p17", "v10r11p18");
43  ND::versioning().AddProduction(ProdId::PROD5F, "PROD5F", "v10r11p19", "v10r11p24");
44  ND::versioning().AddProduction(ProdId::PROD5G, "PROD5G", "v10r11p27", "v10r11p28");
45  ND::versioning().AddProduction(ProdId::PROD6PRE, "PROD6PRE", "v11r17", "v11r28");
46  ND::versioning().AddProduction(ProdId::PROD6A, "PROD6A", "v11r29", "v11r30");
47  ND::versioning().AddProduction(ProdId::PROD6BC, "PROD6B/6C", "v11r31", "v11r32");
48  //May change when production evolves
49  ND::versioning().AddProduction(ProdId::PROD7DEVEL, "PROD7DEVEL", "v12", "v13");
50 
51 
52  // Add package versions to be saved in config tree
53  ND::versioning().AddPackage("psycheCore", anaUtils::GetSoftwareVersionFromPath((std::string)getenv("PSYCHECOREROOT")));
54  ND::versioning().AddPackage("psycheUtils", anaUtils::GetSoftwareVersionFromPath((std::string)getenv("PSYCHEUTILSROOT")));
55  ND::versioning().AddPackage("highlandCore", anaUtils::GetSoftwareVersionFromPath((std::string)getenv("HIGHLANDCOREROOT")));
56  ND::versioning().AddPackage("highlandCorrections", anaUtils::GetSoftwareVersionFromPath((std::string)getenv("HIGHLANDCORRECTIONSROOT")));
57  ND::versioning().AddPackage("highlandIO", anaUtils::GetSoftwareVersionFromPath((std::string)getenv("HIGHLANDIOROOT")));
58 
59  // add the different converters
60  input().AddConverter("FlatTree", new FlatTreeConverter(true));
61  input().AddConverter("MiniTree", new HighlandMiniTreeConverter(true));
62  input().AddConverter("oaAnalysisTree", new oaAnalysisTreeConverter());
63 
64 }
65 //********************************************************************
67  //********************************************************************
68  // save RooTrackerVtx tree
69  _saveGeometry = (bool)ND::params().GetParameterI("highlandIO.FlatTree.SaveGeometry");
70 
71  // save RooTrackerVtx tree
72  _saveRoo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.SaveRooTracker");
73 
74  // Filter RooTrackerVtxTree
75  _filterRoo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.FilterRooTracker");
76 
77  _saveTPCInfo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.SaveTPCInfo");
78  _saveFGDInfo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.SaveFGDInfo");
79  _saveECALInfo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.SaveECALInfo");
80  _saveP0DInfo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.SaveP0DInfo");
81  _saveSMRDInfo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.SaveSMRDInfo");
82  _saveTrackerInfo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.SaveTrackerInfo");
83 
84  // save info from the local reconstruction to the mini tree?
85  _saveReconDirFGDOnlyInfo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseReconDirFGDOnly" );
86  _saveReconDirP0DInfo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseReconDirP0D" );
87  _saveReconDirPECALInfo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseReconDirP0DECal" );
88  _saveReconDirTECALInfo = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseReconDirTrackerECal");
89 
90  _useTPC1 = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseTPC1");
91  _useTPC2 = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseTPC2");
92  _useTPC3 = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseTPC3");
93  _useFGD1 = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseFGD1");
94  _useFGD2 = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseFGD2");
95  _useP0D = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseP0D");
96  _useDsECal = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseDsECal");
97  _useTrECal = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseTrECal");
98  _useP0DECal = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseP0DECal");
99  _useSMRD = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseSMRD");
100 
101  _useTPC1outOfBunch = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseTPC1outOfBunch");
102  _useTPC2outOfBunch = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseTPC2outOfBunch");
103  _useTPC3outOfBunch = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseTPC3outOfBunch");
104  _useFGD1outOfBunch = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseFGD1outOfBunch");
105  _useFGD2outOfBunch = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseFGD2outOfBunch");
106  _useP0DoutOfBunch = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseP0DoutOfBunch");
107  _useDsECaloutOfBunch = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseDsECaloutOfBunch");
108  _useTrECaloutOfBunch = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseTrECaloutOfBunch");
109  _useP0DECaloutOfBunch = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseP0DECaloutOfBunch");
110  _useSMRDoutOfBunch = (bool)ND::params().GetParameterI("highlandIO.FlatTree.UseSMRDoutOfBunch");
111 
112  _saveTrueNuNC = (bool)ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.NuNC");
113  _saveTrueAntiNuNC = (bool)ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.AntiNuNC");
114  _saveTrueNumuCC = (bool)ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.NumuCC");
115  _saveTrueAntiNumuCC = (bool)ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.AntiNumuCC");
116  _saveTrueNueCC = (bool)ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.NueCC");
117  _saveTrueAntiNueCC = (bool)ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.AntiNueCC");
118 
119 
120  if (ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.TPC1")) _saveTrueVertexInDet.push_back(SubDetId::kTPC1);
121  if (ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.TPC2")) _saveTrueVertexInDet.push_back(SubDetId::kTPC2);
122  if (ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.TPC3")) _saveTrueVertexInDet.push_back(SubDetId::kTPC3);
123  if (ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.FGD1")) _saveTrueVertexInDet.push_back(SubDetId::kFGD1);
124  if (ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.FGD2")) _saveTrueVertexInDet.push_back(SubDetId::kFGD2);
125  if (ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.P0D")) _saveTrueVertexInDet.push_back(SubDetId::kP0D);
126  if (ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.DsECal")) _saveTrueVertexInDet.push_back(SubDetId::kDSECAL);
127  if (ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.TrECal")) _saveTrueVertexInDet.push_back(SubDetId::kTECAL);
128  if (ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.P0DECal")) _saveTrueVertexInDet.push_back(SubDetId::kPECAL);
129  if (ND::params().GetParameterI("highlandIO.FlatTree.TrueVertex.SMRD")) _saveTrueVertexInDet.push_back(SubDetId::kSMRD);
130 
131 
132  _currentGeomID=999;
133 
134 
135  // Check software version compatibility between nd280AnalysisTools and current file
136  if (_versionCheck){
137  if(!ND::versioning().CheckVersionCompatibility(input().GetSoftwareVersion(),anaUtils::GetProductionIdFromND280AnalysisTools())) return false;
138  }
139 
140  // Select the production based on the software version of the input file. This will be used to select the appropriate corrections,
141  // bunching, systematics, etc. Assume production 5 when no software version exists
142  versionUtils::prod6_bunching = false;
143  versionUtils::prod6_corrections = false;
144  versionUtils::prod6_systematics = false;
145  if (_versionCheck){
146  if (ND::versioning().GetProduction(input().GetSoftwareVersion()) >= ProdId::PROD6PRE){
147  versionUtils::prod6_bunching = true;
148  versionUtils::prod6_corrections = true;
149  versionUtils::prod6_systematics = true;
150  }
151  }
152 
153  // Dump the production used for corrections, bunching, systematics, etc
154  versionUtils::DumpProductions();
155 
156 
157  // Enable any corrections that have to be done while the mini tree is being created.
158 
159 #if !VERSION_HAS_OFFICIAL_POT
160  // Only need this correction for P5 files.
161 
162  if (ND::params().GetParameterI("highlandIO.FlatTree.EnablePOTCorrection")) {
163  // Use CT4 for MR44 POT accounting, as CT5 wasn't working properly.
164  _corrections.AddCorrection(CorrId::kCT4POTCorr, "ct4pot_corr", new CT4POTCorrection());
165  }
166 #endif
167 
168 
169  // Change the main fit momentum by the muon alternate momentum, but only for P5
170 #if !VERSION_HAS_EQUIVALENT_MAIN_AND_ALT_FITS
171  if (ND::params().GetParameterI("highlandIO.FlatTree.EnableUseMuonAltMomCorrection")) {
172  _corrections.AddCorrection(CorrId::kGlobalAltMomCorr, "altmom_corr", new UseGlobalAltMomCorrection(UseGlobalAltMomCorrection::kMuon));
173  }
174 #endif
175 
176 
177  if (ND::params().GetParameterI("highlandIO.FlatTree.EnableDQCorrection")) {
178  // Correct the data quality in periods when a FGD FEB wasn't working.
179  _corrections.AddCorrection(CorrId::kDQCorr, "dq_corr", new DataQualityCorrection());
180  }
181 
182  if (ND::params().GetParameterI("highlandIO.FlatTree.EnablePileUpCorrection")){
183  _corrections.AddCorrection(CorrId::kPileUpCorr, "pileup_corr", new PileUpCorrection());
184  }
185  // Apply the TPC dE/dx correction in all configurations
186  if (ND::params().GetParameterI("highlandIO.FlatTree.EnableTPCdEdxDataCorrection")){
187  _corrections.AddCorrection(CorrId::kTPCdEdxDataCorr, "tpcdedx_data_corr", new TPCdEdxDataCorrection());
188  }
189  // Apply the TPC dE/dx correction in all configurations
190  if (ND::params().GetParameterI("highlandIO.FlatTree.EnableTPCdEdxMCCorrection")){
191  _corrections.AddCorrection(CorrId::kTPCdEdxMCCorr, "tpcdedx_mc_corr", new TPCdEdxMCCorrection());
192  }
193  // Apply the TPC expected dE/dx correction in all configurations
194  if (ND::params().GetParameterI("highlandIO.FlatTree.EnableTPCExpecteddEdxCorrection")){
195  _corrections.AddCorrection(CorrId::kTPCdEdxExpCorr, "tpcexpecteddedx_corr", new TPCExpecteddEdxCorrection());
196  }
197 
198  // Apply the TPC PID correction based on pull params smearing
199  if (ND::params().GetParameterI("highlandIO.FlatTree.EnableTPCPIDPullMCCorrection")){
200  _corrections.AddCorrection(CorrId::kTPCPIDPullMCCorr, "tpc_pid_pull_mc_corr", new TPCPIDPullMCCorrection());
201  }
202 
203  // Ignore right ECal for runs 3 and 4 as part of it is broken.
204  if (ND::params().GetParameterI("highlandIO.FlatTree.EnableIgnoreRightECalCorrection")){
205  _corrections.AddCorrection(CorrId::kRightECalRun3And4Corr, "ignorerightecal_corr", new IgnoreRightECalRuns3and4Correction());
206  }
207 
208  // Need to be called before the ToF in order to correctly flip a track if required
209  if (ND::params().GetParameterI("highlandIO.FlatTree.EnableFlipKinematicsCorrection")){
210  _corrections.AddCorrection(CorrId::kFlipKinCorr, "flipkinematics_corr", new FlipKinematicsCorrection());
211  }
212 
213  if (ND::params().GetParameterI("highlandIO.FlatTree.EnableToFCorrection")){
214  _corrections.AddCorrection(CorrId::kToFCorr, "tof_corr", new ToFCorrection());
215  }
216 
217  if (ND::params().GetParameterI("highlandIO.FlatTree.EnableMomResolMCCorrection")){
218  _corrections.AddCorrection(CorrId::kMomResolMCCorr, "mom_resol_mc_corr", new MomResolMCCorrection());
219  }
220 
221  if (ND::params().GetParameterI("highlandIO.FlatTree.EnableMomRangeCorrection")){
222  _corrections.AddCorrection(CorrId::kMomRangeCorr, "momrange_corr", new MomRangeCorrection());
223  }
224 
225 
226  _file->mkdir("geom");
227 
228  return true;
229 }
230 
231 //********************************************************************
233  //********************************************************************
234 
235  // Add a new tree called "MiniTree"
236  AddTreeWithName(minitree,"MiniTree");
237 
238  // Add a tree to save the truth info as a clone of the TruthDir/NRooTrackerVtx tree.
239  // oaAnalysis tree converter has already dealt with the NRooTrackerVtx bit for us.
240 
241  std::string rooTreeName="";
242  if(input().GetChain("NRooTrackerVtx"))
243  rooTreeName = "NRooTrackerVtx";
244  else if (input().GetChain("GRooTrackerVtx"))
245  rooTreeName = "GRooTrackerVtx";
246 
247  if(_saveRoo && rooTreeName!=""){
248  if (input().InputIsOriginalTree() && _filterRoo){
249  AddTreeWithName(RooTrackerVtx, rooTreeName);
250 
251  Vtx = new TClonesArray(("ND::"+rooTreeName).c_str(),100);
252 
253  GetTree(RooTrackerVtx)->Branch("RunID", &RunID, "RunID/I", 32000);
254  GetTree(RooTrackerVtx)->Branch("EventID", &EventID, "EventID/I", 32000);
255  GetTree(RooTrackerVtx)->Branch("Preselected", &Preselected, "Preselected/B", 32000);
256  GetTree(RooTrackerVtx)->Branch("SubrunID", &SubrunID, "SubrunID/I", 32000);
257 
258  GetTree(RooTrackerVtx)->Branch("NVtx", &NVtx, "NVtx/I", 32000);
259  GetTree(RooTrackerVtx)->Branch("Vtx","TClonesArray", &Vtx , 32000, 1);
260  }
261  else
262  AddTreeWithName(RooTrackerVtx, rooTreeName,input().GetChain(rooTreeName));
263  }
264 
265  SetCurrentTree(minitree);
266  SetFillSingleTree(GetCurrentTree());
267 
268  // The full AnaSpill. _spill cannot be undefined here, otherwise it give problems in some platforms
269  _spill=NULL;
270  GetTree(minitree)->Branch("Spill", "AnaSpill", &_spill,64000,1);
271 
272  _POTSincePreviousSavedSpill =0;
273  _SpillsSincePreviousSavedSpill=0;
274 }
275 
276 
277 //********************************************************************
279 //********************************************************************
280 
281  // Set the tree to fill
282  SetFillSingleTree(minitree);
283  SetCurrentTree(minitree);
284 
285  // Get the corrected spill and set the branch address
286  _spill = static_cast<AnaSpill*>(&input().GetCorrectedSpill());
287  GetTree(minitree)->SetBranchAddress("Spill",&_spill );
288 
289  // The number of POT and Spills since the last saved Spill
290  _POTSincePreviousSavedSpill += _spill->Beam->POTSincePreviousSavedSpill;
291  _SpillsSincePreviousSavedSpill += _spill->Beam->SpillsSincePreviousSavedSpill;
292 
293  // Delete the uninteresting bunches
294  DeleteUninterestingBunches();
295 
296  _lastSpillSaved = false;
297 
298  // The spill is saved when there are bunches or when there are true signal vertices on it (for eff calculation)
299  if (_spill->Bunches.size()>0 || CheckTruthFillMiniTree(*_spill)){
300 
301  // Write the geometry when it changes
302  if (_saveGeometry) WriteGeometry();
303 
304  // Fill the RooTrackerVtx tree
305  // Must be called before FillMiniTree since when the RooTrackerVtx filtering is applied the RooVtxIndex in true vertices changes
306  FillRooTrackerVtxTree();
307 
308  // Fill the minitree
309  FillMiniTree();
310 
311  // Mark this spill as saved
312  _lastSpillSaved=true;
313  }
314 
315  return true;
316 }
317 
318 //********************************************************************
320 //********************************************************************
321  // Save the last spill (if not save yet) such that we keep track of the total POT since the last saved entry.
322 
323  if (_lastSpillSaved) return;
324 
325  // Fill the RooTrackerVtx tree
326  // Must be called before FillMiniTree since when the RooTrackerVtx filtering is applied the RooVtxIndex in true vertices changes
327  FillRooTrackerVtxTree();
328 
329  // Fill the minitree
330  FillMiniTree();
331 
332  // Delete Spills for the last entry
333  input().DeleteSpill();
334 }
335 
336 //********************************************************************
337 bool CreateMiniTree::CheckTruthFillMiniTree(const AnaSpill& spill){
338 //********************************************************************
339 
340  bool ok_det = false;
341  bool ok_reac = false;
342 
343  const std::vector<AnaTrueVertexB*>& vertices = spill.TrueVertices;
344  for (std::vector<AnaTrueVertexB*>::const_iterator it = vertices.begin(); it!=vertices.end(); it++) {
345  AnaTrueVertex& vtx = *static_cast<AnaTrueVertex*>(*it);
346 
347  // Check the reaction code
348  ok_reac = CheckTrueVertexReaction(vtx);
349 
350  // Check the detector only if the reaction is OK
351  if (ok_reac)
352  ok_det = CheckTrueVertexDetector(vtx.Detector);
353 
354  // return true when it finds a tru vertex fulfilling both conditions
355  if (ok_reac && ok_det) return true;
356  }
357 
358  return false;
359 }
360 
361 //********************************************************************
362 bool CreateMiniTree::CheckTrueVertexReaction(const AnaTrueVertex& vtx){
363 //********************************************************************
364 
365  // NuWro prod5 uses 70 for 2p2h code (Neut does not have 70 at all, and uses 2 instead)
366  // NuWro prod6 uses 2 as NEUT
367  if (_saveTrueNuNC && vtx.ReacCode>=+30 && abs(vtx.ReacCode)!=70) return true;
368  if (_saveTrueAntiNuNC && vtx.ReacCode<=-30 && abs(vtx.ReacCode)!=70) return true;
369  if (_saveTrueNumuCC && vtx.NuPDG==+14 && ((vtx.ReacCode>0 && vtx.ReacCode<+30) || vtx.ReacCode==+70)) return true;
370  if (_saveTrueAntiNumuCC && vtx.NuPDG==-14 && ((vtx.ReacCode<0 && vtx.ReacCode>-30) || vtx.ReacCode==-70)) return true;
371  if (_saveTrueNueCC && vtx.NuPDG==+12 && ((vtx.ReacCode>0 && vtx.ReacCode<+30) || vtx.ReacCode==+70)) return true;
372  if (_saveTrueAntiNueCC && vtx.NuPDG==-12 && ((vtx.ReacCode<0 && vtx.ReacCode>-30) || vtx.ReacCode==-70)) return true;
373  return false;
374 }
375 
376 //********************************************************************
377 bool CreateMiniTree::CheckTrueVertexDetector(unsigned long det){
378 //********************************************************************
379 
380  for (unsigned int i = 0;i<_saveTrueVertexInDet.size();i++)
381  if (SubDetId::GetDetectorUsed(det,_saveTrueVertexInDet[i])) return true;
382 
383  return false;
384 }
385 
386 //********************************************************************
387 bool CreateMiniTree::CheckReconFillMiniTreeOutOfBunch(const AnaBunchB& bunch){
388 //********************************************************************
389 
390  for (std::vector<AnaParticleB*>::const_iterator it = bunch.Particles.begin(); it!=bunch.Particles.end(); it++) {
391 
392  unsigned long bitfield = (*it)->Detector;
393 
394  if (_useTPC1outOfBunch)
395  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kTPC1)) return true;
396  if (_useTPC2outOfBunch)
397  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kTPC2)) return true;
398  if (_useTPC3outOfBunch)
399  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kTPC3)) return true;
400  if (_useFGD1outOfBunch)
401  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kFGD1)) return true;
402  if (_useFGD2outOfBunch)
403  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kFGD2)) return true;
404  if (_useP0DoutOfBunch)
405  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kP0D)) return true;
406  if (_useDsECaloutOfBunch)
407  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kDSECAL)) return true;
408  if (_useTrECaloutOfBunch)
409  if (SubDetId::HasTECALDetector(bitfield)) return true;
410  if (_useP0DECaloutOfBunch)
411  if (SubDetId::HasPECALDetector(bitfield)) return true;
412  if (_useSMRDoutOfBunch)
413  if (SubDetId::HasSMRDDetector(bitfield)) return true;
414  }
415 
416  return false;
417 }
418 
419 //********************************************************************
420 bool CreateMiniTree::CheckReconFillMiniTree(const AnaBunchB& bunch){
421 //********************************************************************
422 
423  if (bunch.Bunch == -1) return CheckReconFillMiniTreeOutOfBunch(bunch);
424 
425  Int_t necal = 0;
426 
427  for (std::vector<AnaParticleB*>::const_iterator it = bunch.Particles.begin(); it!=bunch.Particles.end(); it++) {
428 
429  unsigned long bitfield = (*it)->Detector;
430 
431  if (_useFGD1)
432  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kFGD1)) return true;
433  if (_useFGD2)
434  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kFGD2)) return true;
435  if (_useP0D)
436  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kP0D)) return true;
437  if (_useTPC1)
438  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kTPC1)) return true;
439  if (_useTPC2)
440  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kTPC2)) return true;
441  if (_useTPC3)
442  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kTPC3)) return true;
443  if (_useDsECal)
444  if (SubDetId::GetDetectorUsed(bitfield, SubDetId::kDSECAL)){
445  necal++;
446  if (necal>1) return true;
447  }
448  if (_useTrECal)
449  if (SubDetId::HasTECALDetector(bitfield)){
450  necal++;
451  if (necal>1) return true;
452  }
453  if (_useP0DECal)
454  if (SubDetId::HasPECALDetector(bitfield)) return true;
455  if (_useSMRD)
456  if (SubDetId::HasSMRDDetector(bitfield)) return true;
457 
458 
459  }
460 
461  return false;
462 }
463 
464 //********************************************************************
465 void CreateMiniTree::WriteGeometry(){
466 //********************************************************************
467 
468  // Write the geometry when it changes
469  if (_spill->GeomID != _currentGeomID){
470 
471  // Write the geometry in the geom directory
472  if(_file) _file->cd("geom");
473  ND::hgman().GeoManager()->Write();
474  _file->cd();
475 
476  // Update the current GeomID
477  _currentGeomID= _spill->GeomID;
478  }
479 
480 }
481 
482 //********************************************************************
483 void CreateMiniTree::FillMiniTree(){
484 //********************************************************************
485 
486  // Copy arrays into std::vectors
487  _spill->CopyArraysIntoVectors();
488 
489  // Update the POT and spills
490  _spill->Beam->POTSincePreviousSavedSpill = _POTSincePreviousSavedSpill;
491  _spill->Beam->SpillsSincePreviousSavedSpill = _SpillsSincePreviousSavedSpill;
492 
493  // When the RooTrackerVtx tree is saved in the output file the entry must be recomputed (is not the one in the input file)
494  if (HasTree(RooTrackerVtx))
495  _spill->RooVtxEntry = _rooVtxManager.GetRooVtxEntry();
496 
497  // Fill the tree
498  FillTree(minitree);
499 
500  // Reset to 0
501  _POTSincePreviousSavedSpill =0;
502  _SpillsSincePreviousSavedSpill=0;
503 }
504 
505 //********************************************************************
506 void CreateMiniTree::FillRooTrackerVtxTree(){
507 //********************************************************************
508 
509  if (HasTree(RooTrackerVtx)){
510  // Select the interesting vertices
511  if (_filterRoo) FilterRooTrackerVtxTree();
512 
513  // Fill the RooTrackerVtx tree
514  OutputManager::FillTree(RooTrackerVtx);
515 
516  _rooVtxManager.IncrementRooVtxEntry();
517  }
518 }
519 
520 //********************************************************************
521 void CreateMiniTree::FilterRooTrackerVtxTree(){
522 //********************************************************************
523 
524  // only available for oaAnalysis input
525  if (!input().InputIsOriginalTree()) return;
526 
527  // Save only RooTrackerVtx with a AnaTrueVertex link
528 
529  // event, run and subrun info
530  if (_spill->EventInfo){
531  RunID = _spill->EventInfo->Run;
532  SubrunID = _spill->EventInfo->SubRun;
533  EventID = _spill->EventInfo->Event;
534  }
535 
536  // Get RooTrackerVtx info from the converter
537  TClonesArray* Vtx_input = static_cast<oaAnalysisTreeConverter*>(&input().GetConverter())->GetVtx();
538  Int_t NVtx_input = static_cast<oaAnalysisTreeConverter*>(&input().GetConverter())->GetNVtx();
539 
540  // Get the AnaTrueVertices in the spill
541  const std::vector<AnaTrueVertexB*>& vertices = _spill->TrueVertices;
542 
543  NVtx=0;
544  Vtx->Clear();
545  for (int roov = 0; roov < NVtx_input; roov++) {
546  if (input().GetChain("NRooTrackerVtx")){
547  ND::NRooTrackerVtx *lvtx = (ND::NRooTrackerVtx*) (*Vtx_input)[roov];
548  if ( ! lvtx->StdHepPdg) continue;
549 
550  for (std::vector<AnaTrueVertexB*>::const_iterator it = vertices.begin(); it!=vertices.end(); it++) {
551  AnaTrueVertex* vertex = static_cast<AnaTrueVertex*>(*it);
552  // look for the current vertex
553  if (vertex->ID == lvtx->TruthVertexID){
554  // The Index of the vertex is going to change since we have now less vertices in the RooTrackerVtx tree
555  vertex->RooVtxIndex=NVtx;
556  (*Vtx)[NVtx++]=lvtx;
557  break;
558  }
559  }
560  }
561  else{
562  ND::GRooTrackerVtx *lvtx = (ND::GRooTrackerVtx*) (*Vtx_input)[roov];
563  if ( ! lvtx->StdHepPdg) continue;
564 
565  for (std::vector<AnaTrueVertexB*>::const_iterator it = vertices.begin(); it!=vertices.end(); it++) {
566  AnaTrueVertex* vertex = static_cast<AnaTrueVertex*>(*it);
567  // look for the current vertex
568  if (vertex->ID == lvtx->TruthVertexID){
569  // The Index of the vertex is going to change since we have now less vertices in the RooTrackerVtx tree
570  vertex->RooVtxIndex=NVtx;
571  (*Vtx)[NVtx++]=lvtx;
572  break;
573  }
574  }
575  }
576  }
577 }
578 
579 //********************************************************************
580 void CreateMiniTree::DeleteUninterestingBunches(){
581 //********************************************************************
582 
583  std::vector<AnaParticleB*> particlesToConsider;
584 
585  // Loop over bunches and schedule for deletion the ones not satisfying the conditions about reconstructed objects
586  std::vector<Int_t> bunchesToDelete;
587  for (std::vector<AnaBunchC*>::iterator it=_spill->Bunches.begin();it!=_spill->Bunches.end();it++){
588  AnaBunchB* bunch = static_cast<AnaBunchB*>(*it);
589  // Schedule bunches not fulfilling the requirements for deletion
590  if (!CheckReconFillMiniTree(*bunch))
591  bunchesToDelete.push_back(bunch->Bunch);
592  else{
593  // Save in a vector all belonging to vertices in that bunch
594  for (std::vector<AnaVertexB*>::iterator it2=bunch->Vertices.begin();it2!=bunch->Vertices.end();it2++){
595  AnaVertexB* vertex = *it2;
596  for (Int_t i = 0;i<vertex->nParticles;i++){
597  AnaParticleB* particle = vertex->Particles[i];
598  particlesToConsider.push_back(particle);
599  }
600  }
601  }
602  }
603 
604  // Delete the uninteresting bunches
605  for (UInt_t i=0;i<bunchesToDelete.size();i++){
606  for (std::vector<AnaBunchC*>::iterator it=_spill->Bunches.begin();it!=_spill->Bunches.end();it++){
607  AnaBunchB* bunch = static_cast<AnaBunchB*>(*it);
608  if (bunch->Bunch == bunchesToDelete[i]){
609  bool found=false;
610  // Don't delete if there is a particle in this bunch which belongs to a vertex in another bunch that is not deleted
611  for (std::vector<AnaParticleB*>::iterator it2=bunch->Particles.begin();it2!=bunch->Particles.end();it2++){
612  AnaParticleB* particle2 = *it2;
613  for (std::vector<AnaParticleB*>::iterator it3=particlesToConsider.begin();it3!=particlesToConsider.end();it3++){
614  AnaParticleB* particle3 = *it3;
615  if (particle2==particle3){
616  found=true;
617  break;
618  }
619  }
620  if (found) break;
621  }
622  if (!found){
623  delete bunch;
624  _spill->Bunches.erase(it);
625  break;
626  }
627  }
628  }
629  }
630 
631  // Delete OutOfBunch
632  if (_spill->OutOfBunch && !CheckReconFillMiniTreeOutOfBunch(*_spill->OutOfBunch)){
633 
634  bool found=false;
635  for (std::vector<AnaParticleB*>::iterator it2=_spill->OutOfBunch->Particles.begin();it2!=_spill->OutOfBunch->Particles.end();it2++){
636  AnaParticleB* particle2 = *it2;
637  for (std::vector<AnaParticleB*>::iterator it3=particlesToConsider.begin();it3!=particlesToConsider.end();it3++){
638  AnaParticleB* particle3 = *it3;
639  if (particle2==particle3){
640  found=true;
641  break;
642  }
643  }
644  if (found) break;
645  }
646  if (!found){
647 
648  delete _spill->OutOfBunch;
649  _spill->OutOfBunch=NULL;
650  }
651  }
652 
653 }
Int_t NuPDG
The PDG code of the incoming neutrino.
int GetParameterI(std::string)
Get parameter. Value is returned as integer.
Definition: Parameters.cxx:217
AnaSpillC & GetCorrectedSpill()
Get the current spill (to have corrections applied to it).
static bool HasPECALDetector(unsigned long BitField)
Check if a detector bit field has a P0D ECAL or not.
Definition: SubDetId.cxx:155
void AddCorrection(Int_t index, const std::string &name, CorrectionBase *corr)
void AddTreeWithName(Int_t tree_index, const std::string &tree_name, TTree *tree=NULL)
Add a tree provided its index and name.
Int_t RooVtxIndex
The index of the associated RooTrackerVtx vertex from its position in the TClonesArray.
Definition: DataClasses.hxx:75
void AddPackage(const std::string &name, const std::string &version)
Add a package.
std::vector< AnaTrueVertexB * > TrueVertices
The true MC vertices used in this spill.
Int_t GetCurrentTree() const
Retuns the current tree index.
Definition: TreeManager.hxx:52
static bool GetDetectorUsed(unsigned long BitField, SubDetId::SubDetEnum det)
Method to see if a certain subdetector or subdetector system is used.
Definition: SubDetId.cxx:40
CorrectionManager _corrections
Correction manager.
void DeleteSpill()
clean up the remaining pointers to the spills.
virtual bool Process()
Int_t ID
The ID of the trueObj, which corresponds to the ID of the TTruthParticle that created it...
std::vector< AnaVertexB * > Vertices
std::string GetSoftwareVersionFromPath(const std::string &path)
Get The software version from the path of the package.
void FillTree()
Fill the current tree.
static bool HasSMRDDetector(unsigned long BitField)
Check if a detector bit field has a SMRD or not.
Definition: SubDetId.cxx:160
virtual bool Initialize()
Representation of a true Monte Carlo vertex.
Definition: DataClasses.hxx:50
This class defines a correction that affects the reconstructed TPC Momentum of an.
void SetCurrentTree(Int_t index)
Sets the current tree provided the index.
Definition: TreeManager.hxx:58
virtual void DefineOutputTree()
Define the tree that should be written to the output file.
Creates the appropriate AnaSpillB type. The rest of the work is done by the base converter.
unsigned long Detector
bool HasTree(Int_t index)
Check the existence of a tree provided the index.
Definition: TreeManager.cxx:59
InputConverter & GetConverter()
Get the current selected converter.
static bool HasTECALDetector(unsigned long BitField)
Check if a detector bit field has a Tracker ECAL or not.
Definition: SubDetId.cxx:150
AnaParticleB ** Particles
Representation of a global vertex.
void IncrementRooVtxEntry()
Increment by one the current entry in the RooTrackerVtx tree.
Int_t Bunch
The index of this bunch (0-7).
TFile * _file
Root input or output file.
Definition: TreeManager.hxx:72
void AddConverter(const std::string &name, InputConverter *conv)
Add this converter to the vector of recognised converters.
ProdId_h GetProductionIdFromND280AnalysisTools()
Get Production Id from nd280AnalysisTools.
virtual void Finalize()
std::vector< AnaParticleB * > Particles
bool _versionCheck
Check version compatibility between nd280AnalysisTools compilation and oaAnalysis file...
void AddProduction(ProdId_h prodId, const std::string &prodName, const std::string &prodLowVersion, const std::string &prodHighVersion)
Add a production.
TTree * GetTree()
Returns the a tree set as current.
Definition: TreeManager.hxx:34
Representation of a reconstructed particle (track or shower).