SND@LHC Software
Loading...
Searching...
No Matches
NtupleGenerator_FLUKA.cxx
Go to the documentation of this file.
1#include <math.h>
2#include "TROOT.h"
3#include "TSystem.h"
4#include "TFile.h"
5#include "FairPrimaryGenerator.h"
7#include "FairLogger.h"
8#include "FairMCEventHeader.h"
9
10// read events from ntuples produced by FLUKA
11
12// ----- Default constructor -------------------------------------------
14// -------------------------------------------------------------------------
15// ----- Default constructor -------------------------------------------
16Bool_t NtupleGenerator_FLUKA::Init(const char* fileName) {
17 return Init(fileName, 0);
18}
19// ----- Default constructor -------------------------------------------
20Bool_t NtupleGenerator_FLUKA::Init(const char* fileName, const int firstEvent) {
21 TString fN = "";
22 if (0 == strncmp("/eos",fileName,4) ) {
23 fN = gSystem->Getenv("EOSSHIP");
24 }
25 fN+=fileName;
26 fInputFile = TFile::Open(fN);
27 if (fInputFile->IsZombie()) {
28 LOG(FATAL) << "NtupleGenerator_FLUKA: Error opening the Signal file" << fN;
29 return kFALSE;
30 }
31 LOG(INFO) << "NtupleGenerator_FLUKA: Opening input file " << fN;
32
33 fTree = (TTree *)fInputFile->Get("nt");
34
35 fNevents = fTree->GetEntries();
36 fn = firstEvent;
37 primaries = 0;
38 // Check the input TTree structure
39 if (fTree->FindLeaf("primaries_") != nullptr) {
40 fTree->SetBranchAddress("primaries",&primaries);
41 }
42 else {
43 fTree->SetBranchAddress("run",&runN); // run number
44 fTree->SetBranchAddress("event",&eventN); // event number
45 fTree->SetBranchAddress("id",&id); // particle id
46 fTree->SetBranchAddress("generation",&generation); // origin generation number
47 fTree->SetBranchAddress("t",&t); // time of flight
48 fTree->SetBranchAddress("E",&E); // incoming muon energy
49 fTree->SetBranchAddress("w",&w); // weight of event
50 fTree->SetBranchAddress("x",&x); // position
51 fTree->SetBranchAddress("y",&y);
52 fTree->SetBranchAddress("z",&z);
53 fTree->SetBranchAddress("px",&px); // momentum
54 fTree->SetBranchAddress("py",&py);
55 fTree->SetBranchAddress("pz",&pz);
56 }
57 return kTRUE;
58}
59// -------------------------------------------------------------------------
60
61
62// ----- Destructor ----------------------------------------------------
64{
65 // cout << "destroy Ntuple" << endl;
66 fInputFile->Close();
67 fInputFile->Delete();
68 delete fInputFile;
69}
70// -------------------------------------------------------------------------
71
72// ----- Passing the event ---------------------------------------------
73Bool_t NtupleGenerator_FLUKA::ReadEvent(FairPrimaryGenerator* cpg)
74{
75 fTree->GetEntry(fn);
76 fn++;
77 if (fn %10000==0) {LOG(INFO)<< "reading event "<<fn;}
78 if (fn > fNevents) {
79 LOG(WARNING) << "No more input events";
80 return kFALSE; }
81 /* Add all primary tracks in the event. Typically, muon bkg MC has single muon per event.
82 Several muon primaries per event exist in the multi-muon MC.
83 */
84 if (fTree->FindLeaf("primaries_") != nullptr) {
85 int nf = primaries->GetEntries();
86 for(int i=0; i<nf; i++){
87 //casting
88 struct PrimaryTrack* primary = dynamic_cast<struct PrimaryTrack*>(primaries->AddrAt(i));
89 //convert time from ns to sec
90 cpg->AddTrack(int(primary->id),primary->px,primary->py,primary->pz,
91 primary->x,primary->y,primary->z-SND_Z,-1,true,primary->E,
92 primary->t/1E9,primary->w,(TMCProcess)primary->generation);
93 LOG(DEBUG) << "NtupleGenerator_FLUKA: add muon " << i << "," << int(primary->id)
94 <<","<<primary->px<<","<<primary->py<<","<<primary->pz<<","<<primary->x
95 <<","<<primary->y<<","<<primary->z-SND_Z<<","<<primary->generation
96 <<","<<primary->E<<","<<primary->t<<"ns,"<<primary->w;
97 }
98 }
99 else {
100 // convert time from ns to sec
101 cpg->AddTrack(int(id[0]),px[0],py[0],pz[0],x[0],y[0],z[0]-SND_Z,-1,true,E[0],t[0]/1E9,w[0],(TMCProcess)generation[0]);
102 LOG(DEBUG) << "NtupleGenerator_FLUKA: add muon " << id[0]<<","<<px[0]<<","<<py[0]<<","<<pz[0]<<","<<x[0]<<","<<y[0]<<","<<z[0]-SND_Z<<","<<generation[0]<<","<<E[0]<<","<<t[0]<<"ns,"<<w[0];
103 }
104
105 // Set the generation run and event numbers through the MC event header
106 FairMCEventHeader* header = cpg->GetEvent();
107 header->SetEventID(int(eventN));
108 header->SetRunID(int(runN));
109
110 return kTRUE;
111}
112
113// -------------------------------------------------------------------------
115{
116 return fNevents;
117}
118
119
virtual Bool_t Init(const char *, int)
Bool_t ReadEvent(FairPrimaryGenerator *)
ClassImp(ecalContFact) ecalContFact