FairShip
Loading...
Searching...
No Matches
NtupleGenerator Class Reference

#include <NtupleGenerator.h>

Inheritance diagram for NtupleGenerator:
[legend]
Collaboration diagram for NtupleGenerator:
[legend]

Public Member Functions

 NtupleGenerator ()
 ~NtupleGenerator () override
Bool_t ReadEvent (FairPrimaryGenerator *) override
Bool_t Init (const char *, int) override
Bool_t Init (const char *) override
Int_t GetNevents ()
virtual Bool_t Init (const std::vector< std::string > &inFiles, int startNumber)
virtual Bool_t Init (const std::vector< std::string > &inFiles)
Public Member Functions inherited from SHiP::Generator
 Generator ()=default
 ~Generator () override
virtual void UseExternalFile (std::string x, Int_t i)
virtual void UseExternalFile (std::vector< std::string > &inFiles, Int_t i)
void SetMaxTheta (Double_t thetaX, Double_t thetaY)
Int_t nrOfGeoRejections () const
Long64_t GetCounter (const std::string &name) const
void SetCounter (const std::string &name, Long64_t value)
void IncrementCounter (const std::string &name, Long64_t delta=1)

Protected Attributes

Int_t id
Int_t Nmeas
Int_t volid [500]
Int_t procid [500]
Int_t parentid
Float_t Ezero
Float_t tof
Double_t w
Float_t px [500]
Float_t py [500]
Float_t pz [500]
Float_t vx [500]
Float_t vy [500]
Float_t vz [500]
TFile * fInputFile
TTree * fTree
FairLogger * fLogger
int fNevents
 don't make it persistent, magic ROOT command
int fn
Protected Attributes inherited from SHiP::Generator
std::optional< std::string > fextFile
Int_t firstEvent = 0
Double_t fMaxThetaX = 0
Double_t fMaxThetaY = 0
bool fUseVesselAcceptance = false
Int_t fnGeoRejects = 0
std::unordered_map< std::string, int64_t > fCounters

Additional Inherited Members

Protected Member Functions inherited from SHiP::Generator
bool IsInVesselAcceptance (Double_t px, Double_t py, Double_t pz) const

Detailed Description

Definition at line 15 of file NtupleGenerator.h.

Constructor & Destructor Documentation

◆ NtupleGenerator()

NtupleGenerator::NtupleGenerator ( )

default constructor

Definition at line 18 of file NtupleGenerator.cxx.

19 : fInputFile(nullptr),
20 fTree(nullptr),
21 fLogger(nullptr),
22 fNevents(0),
23 fn(0) {}
FairLogger * fLogger
int fNevents
don't make it persistent, magic ROOT command

◆ ~NtupleGenerator()

NtupleGenerator::~NtupleGenerator ( )
override

destructor

Definition at line 99 of file NtupleGenerator.cxx.

99 {
100 // cout << "destroy Ntuple" << endl;
101 if (fInputFile) {
102 fInputFile->Close();
103 fInputFile->Delete();
104 delete fInputFile;
105 }
106}

Member Function Documentation

◆ GetNevents()

Int_t NtupleGenerator::GetNevents ( )

Definition at line 152 of file NtupleGenerator.cxx.

152{ return fNevents; }

◆ Init() [1/4]

Bool_t NtupleGenerator::Init ( const char * fileName)
overridevirtual

Implements SHiP::Generator.

Definition at line 26 of file NtupleGenerator.cxx.

26{ return Init(fileName, 0); }
Bool_t Init(const char *, int) override

◆ Init() [2/4]

Bool_t NtupleGenerator::Init ( const char * fileName,
int startEvent )
overridevirtual

Implements SHiP::Generator.

Definition at line 28 of file NtupleGenerator.cxx.

28 {
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}
Float_t vz[500]
Float_t vy[500]
Float_t vx[500]
Float_t pz[500]
Float_t py[500]
Float_t px[500]

◆ Init() [3/4]

virtual Bool_t SHiP::Generator::Init ( const std::vector< std::string > & inFiles)
inlinevirtual

public method ReadEvent

Reimplemented from SHiP::Generator.

Definition at line 33 of file Generator.h.

33 {
34 LOG(warning) << "Multiple input files not implemented for this generator "
35 "- only taking the first!";
36 return Init(inFiles.at(0).c_str(), 0);
37 };

◆ Init() [4/4]

virtual Bool_t SHiP::Generator::Init ( const std::vector< std::string > & inFiles,
int startNumber )
inlinevirtual

public method ReadEvent

Reimplemented from SHiP::Generator.

Definition at line 27 of file Generator.h.

28 {
29 LOG(warning) << "Multiple input files not implemented for this generator "
30 "- only taking the first!";
31 return Init(inFiles.at(0).c_str(), startNumber);
32 };

◆ ReadEvent()

Bool_t NtupleGenerator::ReadEvent ( FairPrimaryGenerator * cpg)
override

Definition at line 110 of file NtupleGenerator.cxx.

110 {
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}
void IncrementCounter(const std::string &name, Long64_t delta=1)
Definition Generator.h:65
int i
Definition ShipAna.py:108

Member Data Documentation

◆ Ezero

Float_t NtupleGenerator::Ezero
protected

Definition at line 33 of file NtupleGenerator.h.

◆ fInputFile

TFile* NtupleGenerator::fInputFile
protected

Definition at line 36 of file NtupleGenerator.h.

◆ fLogger

FairLogger* NtupleGenerator::fLogger
protected

Definition at line 38 of file NtupleGenerator.h.

◆ fn

int NtupleGenerator::fn
protected

Definition at line 40 of file NtupleGenerator.h.

◆ fNevents

int NtupleGenerator::fNevents
protected

don't make it persistent, magic ROOT command

Definition at line 39 of file NtupleGenerator.h.

◆ fTree

TTree* NtupleGenerator::fTree
protected

Definition at line 37 of file NtupleGenerator.h.

◆ id

Int_t NtupleGenerator::id
protected

Definition at line 32 of file NtupleGenerator.h.

◆ Nmeas

Int_t NtupleGenerator::Nmeas
protected

Definition at line 32 of file NtupleGenerator.h.

◆ parentid

Int_t NtupleGenerator::parentid
protected

Definition at line 32 of file NtupleGenerator.h.

◆ procid

Int_t NtupleGenerator::procid[500]
protected

Definition at line 32 of file NtupleGenerator.h.

◆ px

Float_t NtupleGenerator::px[500]
protected

Definition at line 35 of file NtupleGenerator.h.

◆ py

Float_t NtupleGenerator::py[500]
protected

Definition at line 35 of file NtupleGenerator.h.

◆ pz

Float_t NtupleGenerator::pz[500]
protected

Definition at line 35 of file NtupleGenerator.h.

◆ tof

Float_t NtupleGenerator::tof
protected

Definition at line 33 of file NtupleGenerator.h.

◆ volid

Int_t NtupleGenerator::volid[500]
protected

Definition at line 32 of file NtupleGenerator.h.

◆ vx

Float_t NtupleGenerator::vx[500]
protected

Definition at line 35 of file NtupleGenerator.h.

◆ vy

Float_t NtupleGenerator::vy[500]
protected

Definition at line 35 of file NtupleGenerator.h.

◆ vz

Float_t NtupleGenerator::vz[500]
protected

Definition at line 35 of file NtupleGenerator.h.

◆ w

Double_t NtupleGenerator::w
protected

Definition at line 34 of file NtupleGenerator.h.


The documentation for this class was generated from the following files: