SND@LHC Software
Loading...
Searching...
No Matches
sndlhcGSimpleNtpConverter.cxx
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3#include <set>
4
5#include "TFile.h"
6#include "TTree.h"
7#include "TMath.h"
8#include "TRandom3.h"
9
10#include "ShipUnit.h"
11
12#include "Tools/Flux/GSimpleNtpFlux.h"
13
14/*
15 Converts neutrino rays generated by FLUKA into GSimpleNtpFlux format for GENIE
16*/
17
18int main(int argc, char** argv){
19
20 if (argc != 4) {
21 std::cout << "Three arguments required: path to FLUKA file AND output file name AND number of pp collisions used to generate FLUKA file." << std::endl;
22 return -1;
23 }
24
25 std::string inFileName = std::string(argv[1]);
26 std::string outFileName = std::string(argv[2]);
27 double pp_collision_number = std::stod(argv[3]);
28
29 // Convert FLUKA to PDG particle IDs. (ONLY NEUTRINOS IMPLEMENTED!!!)
30 std::map<int, int> FLUKAtoPDG { {27, 14},
31 {28, -14},
32 {5, 12},
33 {6, -12},
34 {43, 16},
35 {44, -16} };
36
37 // To generate a random seed which is stored in the metadata.
38 TRandom3 * ran = new TRandom3();
39
40 bool verbose = false;
41
42 // Scoring plane info
43 // Plane z in FLUKA coordinates: 48386 cm
44 // FLUKA z = 0 in sndsw coordinates : -48000 cm
45 double z = (48386-48000); // In cm for consistency with the FLUKA file.
46
47 double plane_corner[] = {-70., 5., z};
48 double plane_dir1[] = {140., 0, 0};
49 double plane_dir2[] = {0, 65., 0};
50
51 // Set up input file
52 std::ifstream in_file(inFileName);
53
54 // Input variables
55 /*
56 # Scoring particles entering Region No 2935
57 # Col 1: FLUKA run number
58 # Col 2: primary event number
59 # -- Particle information --
60 # Col 3: FLUKA particle type ID
61 # Col 4: Generation number
62 # Col 5: Kinetic energy (GeV)
63 # Col 6: Statistical weight
64 # -- Crossing at scoring plane --
65 # Col 7: x coord (cm)
66 # Col 8: y coord (cm)
67 # Col 9: x dir cosine
68 # Col 10: y dir cosine
69 # Col 11: Particle age since primary event (sec)
70 # Col 12: ...
71 */
72
73 int FlukaRun, evtNumber, FlukaID, generationN;
74 float Ekin, weight, x, y, x_cos, y_cos, age;
75
76 TFile * fOut = new TFile(outFileName.c_str(), "RECREATE");
77 TTree * tOut = new TTree("flux", "a simple flux n-tuple");
78 TTree * metaOut = new TTree("meta","metadata for flux n-tuple");
79
80 genie::flux::GSimpleNtpEntry * gsimple_entry = new genie::flux::GSimpleNtpEntry;
81 tOut->Branch("entry", &gsimple_entry);
82 genie::flux::GSimpleNtpMeta* meta_entry = new genie::flux::GSimpleNtpMeta;
83 metaOut->Branch("meta", &meta_entry);
84
85 genie::flux::GSimpleNtpAux* aux_entry = new genie::flux::GSimpleNtpAux;
86 tOut->Branch("aux", &aux_entry);
87
88
89 UInt_t metakey = TString::Hash(outFileName.c_str(),strlen(outFileName.c_str()));
90 metakey &= 0x7FFFFFFF;
91
92 // Metadata accumulators:
93 std::set<int> pdglist;
94 double min_weight = 1e10;
95 double max_weight = -1e10;
96 double max_energy = 0;
97
98 unsigned long int counter = 0;
99
100 if (in_file.is_open()){
101 string in_line;
102 while (!in_file.eof()){
103 getline(in_file, in_line);
104
105 // Skip lines containing "#"
106 if (in_line.find("#") == in_line.npos){
107 in_file >> FlukaRun
108 >> evtNumber
109 >> FlukaID
110 >> generationN
111 >> Ekin
112 >> weight
113 >> x
114 >> y
115 >> x_cos
116 >> y_cos
117 >> age;
118
119 gsimple_entry->Reset();
120 aux_entry->Reset();
121
122
123 if (verbose) std::cout << "Got entry " << counter++ << std::endl;
124
125 if (verbose){
126 std::cout << FlukaRun << "\n"
127 << evtNumber << "\n"
128 << FlukaID << "\n"
129 << generationN << "\n"
130 << Ekin << "\n"
131 << weight << "\n"
132 << x << "\n"
133 << y << "\n"
134 << z << "\n"
135 << x_cos << "\n"
136 << y_cos << "\n"
137 << age << "\n"
138 << "--------------------------------------------------" << std::endl;
139 }
140
141 gsimple_entry->metakey = metakey;
142 gsimple_entry->pdg = FLUKAtoPDG[FlukaID];
143 gsimple_entry->wgt = weight;
144 gsimple_entry->vtxx = x*ShipUnit::cm/ShipUnit::m; // in m
145 gsimple_entry->vtxy = y*ShipUnit::cm/ShipUnit::m;
146 gsimple_entry->vtxz = z*ShipUnit::cm/ShipUnit::m;
147 gsimple_entry->dist = 0.; // Distance from hadron decay point to neutrino "vertex", to use for oscillations, for example. Don't use.
148
149 // I'm assuming x_cos, y_cos are normalized.
150 double z_cos = sqrt(1 - pow(x_cos, 2) - pow(y_cos, 2));
151 // And massless neutrinos
152 gsimple_entry->px = Ekin*x_cos; // in GeV/c
153 gsimple_entry->py = Ekin*y_cos;
154 gsimple_entry->pz = Ekin*z_cos;
155 gsimple_entry->E = Ekin;
156
157 // Set auxiliary data
158 aux_entry->auxint.push_back(FlukaRun);
159 aux_entry->auxint.push_back(evtNumber);
160 aux_entry->auxint.push_back(FlukaID);
161 aux_entry->auxint.push_back(generationN);
162
163 aux_entry->auxdbl.push_back(age);
164 aux_entry->auxdbl.push_back(weight);
165
166 // Accumulate metadata
167 pdglist.insert(gsimple_entry->pdg);
168 min_weight = TMath::Min(min_weight, gsimple_entry->wgt);
169 max_weight = TMath::Max(max_weight, gsimple_entry->wgt);
170 max_energy = TMath::Max(max_energy, gsimple_entry->E);
171
172 // All done!
173 tOut->Fill();
174 }
175 }
176 }
177
178 // Sort out metadata
179 // Copy pdg list
180 meta_entry->pdglist.clear();
181 for (std::set<int>::const_iterator pdg_iterator = pdglist.begin();
182 pdg_iterator != pdglist.end();
183 ++pdg_iterator) meta_entry->pdglist.push_back(*pdg_iterator);
184 meta_entry->maxEnergy = max_energy;
185 meta_entry->maxWgt = max_weight;
186 meta_entry->minWgt = min_weight;
187
188 meta_entry->protons = pp_collision_number; // Number of pp collisions.
189 for (int i = 0; i < 3; i++) meta_entry->windowBase[i] = plane_corner[i]*ShipUnit::cm/ShipUnit::m;
190 for (int i = 0; i < 3; i++) meta_entry->windowDir1[i] = plane_dir1[i]*ShipUnit::cm/ShipUnit::m;
191 for (int i = 0; i < 3; i++) meta_entry->windowDir2[i] = plane_dir2[i]*ShipUnit::cm/ShipUnit::m;
192
193 meta_entry->infiles.push_back(inFileName);
194 meta_entry->seed = ran->GetSeed();
195 meta_entry->metakey = metakey;
196
197 meta_entry->auxintname.push_back("FlukaRun");
198 meta_entry->auxintname.push_back("evtNumber");
199 meta_entry->auxintname.push_back("FlukaID");
200 meta_entry->auxintname.push_back("generationN");
201
202 meta_entry->auxdblname.push_back("age");
203 meta_entry->auxdblname.push_back("FLUKA_weight");
204
205 metaOut->Fill();
206
207 fOut->cd();
208 metaOut->Write();
209 tOut->Write();
210 fOut->Close();
211
212}
213
Int_t counter
int main()
Definition main.cc:133