FairShip
Loading...
Searching...
No Matches
NtupleGenerator.cxx
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP
3// Collaboration
4
5#include "NtupleGenerator.h"
6
7#include <cmath>
8
9#include "FairPrimaryGenerator.h"
10#include "TDatabasePDG.h" // for TDatabasePDG
11#include "TFile.h"
12#include "TMath.h" // for Sqrt
13#include "TROOT.h"
14
15// read events from ntuples produced
16
17// ----- Default constructor -------------------------------------------
19 : fInputFile(nullptr),
20 fTree(nullptr),
21 fLogger(nullptr),
22 fNevents(0),
23 fn(0) {}
24// -------------------------------------------------------------------------
25// ----- Default constructor -------------------------------------------
26Bool_t NtupleGenerator::Init(const char* fileName) { return Init(fileName, 0); }
27// ----- Default constructor -------------------------------------------
28Bool_t NtupleGenerator::Init(const char* fileName, const int startEvent) {
29 if (startEvent < 0) {
30 LOG(error) << "NtupleGenerator: startEvent must be >= 0, got "
31 << startEvent;
32 return kFALSE;
33 }
34
35 LOG(info) << "NtupleGenerator: Opening input file " << fileName;
36 fInputFile = TFile::Open(fileName, "READ");
37 if (!fInputFile) {
38 LOG(error) << "NtupleGenerator: Error opening input file " << fileName;
39 delete fInputFile;
40 fInputFile = nullptr;
41 return kFALSE;
42 }
43
44 fTree = dynamic_cast<TTree*>(fInputFile->Get("ntuple"));
45 if (!fTree) {
46 LOG(error) << "NtupleGenerator: Cannot find tree ntuple in file "
47 << fileName;
48 fInputFile->Close();
49 delete fInputFile;
50 fInputFile = nullptr;
51 return kFALSE;
52 }
53
54 fNevents = fTree->GetEntries();
55 if (startEvent >= fNevents) {
56 LOG(error) << "NtupleGenerator: startEvent " << startEvent
57 << " is out of range for " << fNevents << " entries";
58 fInputFile->Close();
59 delete fInputFile;
60 fInputFile = nullptr;
61 fTree = nullptr;
62 return kFALSE;
63 }
64
65 fn = startEvent;
66 bool ok = true;
67 ok &= (fTree->SetBranchAddress("id", &id) >= 0);
68 if (fTree->FindBranch("parentid")) {
69 ok &= (fTree->SetBranchAddress("parentid", &parentid) >= 0);
70 } // parent id
71 if (fTree->FindBranch("tof")) {
72 ok &= (fTree->SetBranchAddress("tof", &tof) >= 0);
73 } // time of flight
74 ok &= (fTree->SetBranchAddress("Nmeas", &Nmeas) >= 0);
75 ok &= (fTree->SetBranchAddress("Ezero", &Ezero) >= 0);
76 ok &= (fTree->SetBranchAddress("w", &w) >= 0);
77 ok &= (fTree->SetBranchAddress("x", &vx) >= 0);
78 ok &= (fTree->SetBranchAddress("y", &vy) >= 0);
79 ok &= (fTree->SetBranchAddress("z", &vz) >= 0);
80 ok &= (fTree->SetBranchAddress("px", &px) >= 0);
81 ok &= (fTree->SetBranchAddress("py", &py) >= 0);
82 ok &= (fTree->SetBranchAddress("pz", &pz) >= 0);
83 ok &= (fTree->SetBranchAddress("volid", &volid) >= 0);
84 ok &= (fTree->SetBranchAddress("procid", &procid) >= 0);
85 if (!ok) {
86 LOG(error)
87 << "NtupleGenerator: failed to bind one or more required branches";
88 fInputFile->Close();
89 delete fInputFile;
90 fInputFile = nullptr;
91 fTree = nullptr;
92 return kFALSE;
93 }
94 return kTRUE;
95}
96// -------------------------------------------------------------------------
97
98// ----- Destructor ----------------------------------------------------
100 // cout << "destroy Ntuple" << endl;
101 if (fInputFile) {
102 fInputFile->Close();
103 fInputFile->Delete();
104 delete fInputFile;
105 }
106}
107// -------------------------------------------------------------------------
108
109// ----- Passing the event ---------------------------------------------
110Bool_t NtupleGenerator::ReadEvent(FairPrimaryGenerator* cpg) {
111 while (fn < fNevents) {
112 fTree->GetEntry(fn);
113 IncrementCounter("scanned_entries");
114 fn++;
115 if (fn % 10000 == 0) {
116 LOG(info) << "NtupleGenerator: reading event " << fn;
117 }
118 // test if muon survives:
119 Int_t i = Nmeas - 3;
120 Float_t r2 = (vx[i] * vx[i] + vy[i] * vy[i]);
121 if (procid[Nmeas - 1] == 2 && r2 < 9) {
122 IncrementCounter("accepted_entries");
123 break;
124 }
125 IncrementCounter("rejected_entries");
126 }
127 if (fn == fNevents) {
128 LOG(info) << "NtupleGenerator: no more input events";
129 return kFALSE;
130 }
131 TDatabasePDG* pdgBase = TDatabasePDG::Instance();
132 Double_t mass = pdgBase->GetParticle(id)->Mass();
133 Double_t e =
134 TMath::Sqrt(px[0] * px[0] + py[0] * py[0] + pz[0] * pz[0] + mass * mass);
135 tof = 0;
136 // first, original muon
137 cpg->AddTrack(id, px[0], py[0], pz[0], vx[0] * 100., vy[0] * 100.,
138 vz[0] * 100., -1., false, e, tof, w);
139 Int_t i = Nmeas - 1;
140 // second, surviving muon, extrapolate back to end of muon shield, z=20m
141 Double_t zscor = 20.;
142 Double_t lam = (zscor - vz[i]) / pz[i];
143 Double_t xscor = vx[i] + lam * px[i];
144 Double_t yscor = vy[i] + lam * py[i];
145 e = TMath::Sqrt(px[i] * px[i] + py[i] * py[i] + pz[i] * pz[i] + mass * mass);
146 cpg->AddTrack(id, px[i], py[i], pz[i], xscor * 100., yscor * 100.,
147 zscor * 100., 0, true, e, tof, w);
148 return kTRUE;
149}
150
151// -------------------------------------------------------------------------
Float_t vz[500]
~NtupleGenerator() override
Float_t vy[500]
Bool_t ReadEvent(FairPrimaryGenerator *) override
FairLogger * fLogger
Float_t vx[500]
Float_t pz[500]
Float_t py[500]
int fNevents
don't make it persistent, magic ROOT command
Bool_t Init(const char *, int) override
Float_t px[500]
void IncrementCounter(const std::string &name, Long64_t delta=1)
Definition Generator.h:65