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