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
15using std::cout;
16using std::endl;
17// read events from ntuples produced
18
19// ----- Default constructor -------------------------------------------
21// -------------------------------------------------------------------------
22// ----- Default constructor -------------------------------------------
23Bool_t NtupleGenerator::Init(const char* fileName) { return Init(fileName, 0); }
24// ----- Default constructor -------------------------------------------
25Bool_t NtupleGenerator::Init(const char* fileName, const int startEvent) {
26 cout << "Info NtupleGenerator: Opening input file " << fileName << endl;
27 fInputFile = new TFile(fileName);
28 if (fInputFile->IsZombie()) {
29 cout << "-E NtupleGenerator: Error opening the Signal file" << fileName
30 << endl;
31 }
32 fTree = dynamic_cast<TTree*>(fInputFile->Get("ntuple"));
33 fNevents = fTree->GetEntries();
34 fn = startEvent;
35 fTree->SetBranchAddress("id", &id); // particle id
36 if (fTree->FindBranch("parentid")) {
37 fTree->SetBranchAddress("parentid", &parentid);
38 } // parent id
39 if (fTree->FindBranch("tof")) {
40 fTree->SetBranchAddress("tof", &tof);
41 } // time of flight
42 fTree->SetBranchAddress("Nmeas", &Nmeas); // number of Geant4 points
43 fTree->SetBranchAddress("Ezero", &Ezero); // incoming muon energy
44 fTree->SetBranchAddress("w", &w); // weight of event
45 fTree->SetBranchAddress("x", &vx); // position
46 fTree->SetBranchAddress("y", &vy);
47 fTree->SetBranchAddress("z", &vz);
48 fTree->SetBranchAddress("px", &px); // momentum
49 fTree->SetBranchAddress("py", &py);
50 fTree->SetBranchAddress("pz", &pz);
51 fTree->SetBranchAddress("volid", &volid); // which volume
52 fTree->SetBranchAddress("procid", &procid); // which process
53 return kTRUE;
54}
55// -------------------------------------------------------------------------
56
57// ----- Destructor ----------------------------------------------------
59 // cout << "destroy Ntuple" << endl;
60 fInputFile->Close();
61 fInputFile->Delete();
62 delete fInputFile;
63}
64// -------------------------------------------------------------------------
65
66// ----- Passing the event ---------------------------------------------
67Bool_t NtupleGenerator::ReadEvent(FairPrimaryGenerator* cpg) {
68 while (fn < fNevents) {
69 fTree->GetEntry(fn);
70 fn++;
71 if (fn % 10000 == 0) {
72 cout << "reading event " << fn << endl;
73 }
74 // test if muon survives:
75 Int_t i = Nmeas - 3;
76 Float_t r2 = (vx[i] * vx[i] + vy[i] * vy[i]);
77 if (procid[Nmeas - 1] == 2 && r2 < 9) {
78 break;
79 }
80 }
81 if (fn == fNevents) {
82 cout << "No more input events" << endl;
83 return kFALSE;
84 }
85 TDatabasePDG* pdgBase = TDatabasePDG::Instance();
86 Double_t mass = pdgBase->GetParticle(id)->Mass();
87 Double_t e =
88 TMath::Sqrt(px[0] * px[0] + py[0] * py[0] + pz[0] * pz[0] + mass * mass);
89 tof = 0;
90 // first, original muon
91 cpg->AddTrack(id, px[0], py[0], pz[0], vx[0] * 100., vy[0] * 100.,
92 vz[0] * 100., -1., false, e, tof, w);
93 Int_t i = Nmeas - 1;
94 // second, surviving muon, extrapolate back to end of muon shield, z=20m
95 Double_t zscor = 20.;
96 Double_t lam = (zscor - vz[i]) / pz[i];
97 Double_t xscor = vx[i] + lam * px[i];
98 Double_t yscor = vy[i] + lam * py[i];
99 e = TMath::Sqrt(px[i] * px[i] + py[i] * py[i] + pz[i] * pz[i] + mass * mass);
100 cpg->AddTrack(id, px[i], py[i], pz[i], xscor * 100., yscor * 100.,
101 zscor * 100., 0, true, e, tof, w);
102 return kTRUE;
103}
104
105// -------------------------------------------------------------------------
Float_t vz[500]
~NtupleGenerator() override
Float_t vy[500]
Bool_t ReadEvent(FairPrimaryGenerator *) override
Float_t vx[500]
Float_t pz[500]
Int_t volid[500]
Int_t procid[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]