-
Notifications
You must be signed in to change notification settings - Fork 0
/
simToPet-irene.cpp
276 lines (226 loc) · 7.73 KB
/
simToPet-irene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//simple program to translate sim output to "adc" format
// compile with
// g++ -o ../build/simToPet simToPet.cpp `root-config --cflags --glibs`
// syntax
// simToPet `ls out*`
#include "TROOT.h"
#include "TTree.h"
#include "TFile.h"
#include "TChain.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "TObjArray.h"
#include "TObject.h"
#include <algorithm>
int main (int argc, char** argv)
{
gROOT->ProcessLine("#include <vector>");
//TFile* f1 = new TFile(argv[1]);
TChain *tree = new TChain("tree");
for (int i = 1 ; i < argc ; i++)
{
std::cout << "Adding file " << argv[i] << std::endl;
tree->Add(argv[i]);
}
//play with input names
// std::string inputFileName =
// find the number of channels directly from the tchain file
// before creating the variables
// first, get the list of leaves
TObjArray *leavescopy = tree->GetListOfLeaves();
int nLeaves = leavescopy->GetEntries();
std::vector<std::string> leavesName;
// fill a vector with the leaves names
// std::cout << nLeaves << std::endl;
for(int i = 0 ; i < nLeaves ; i++)
{
// std::cout << i << std::endl;
leavesName.push_back(leavescopy->At(i)->GetName());
}
// count the entries that start with "ch"
int numOfCh = 0;
int numOfCry = 0;
std::string det_prefix("detector");
std::string cry_prefix("cry");
for(int i = 0 ; i < nLeaves ; i++)
{
// leavesName.push_back(leavescopy->At(i)->GetName());
if (!leavesName[i].compare(0, det_prefix.size(), det_prefix))
numOfCh++;
if (!leavesName[i].compare(0, cry_prefix.size(), cry_prefix))
numOfCry++;
}
//the string "cry" appears 4 times per crystal..
numOfCry = numOfCry / 4;
std::cout << numOfCh << std::endl;
std::cout << numOfCry << std::endl;
Long64_t Seed;
int Run;
int Event;
float totalEnergyDeposited;
int NumOptPhotons;
int NumCherenkovPhotons;
std::vector<float> *CryEnergyDeposited;
std::vector<float> **pCryEnergyDeposited;
std::vector<float> *PosXEnDep;
std::vector<float> **pPosXEnDep;
std::vector<float> *PosYEnDep;
std::vector<float> **pPosYEnDep;
std::vector<float> *PosZEnDep;
std::vector<float> **pPosZEnDep;
// DetectorHit = new Short_t [numOfCh];
CryEnergyDeposited = new std::vector<float> [numOfCry];
pCryEnergyDeposited = new std::vector<float>* [numOfCry];
PosXEnDep = new std::vector<float> [numOfCry];
pPosXEnDep = new std::vector<float>* [numOfCry];
PosYEnDep = new std::vector<float> [numOfCry];
pPosYEnDep = new std::vector<float>* [numOfCry];
PosZEnDep = new std::vector<float> [numOfCry];
pPosZEnDep = new std::vector<float>* [numOfCry];
// short RunDetectorHit[16];
std::vector<float> **pEdep;
std::vector<float> **px;
std::vector<float> **py;
std::vector<float> **pz;
pEdep = new std::vector<float>* [numOfCry];
px = new std::vector<float>* [numOfCry];
py = new std::vector<float>* [numOfCry];
pz = new std::vector<float>* [numOfCry];
for (int i = 0 ; i < numOfCry ; i++)
{
pEdep[i] = 0;
px[i] = 0;
py[i] = 0;
pz[i] = 0;
}
Short_t *detector;
detector = new Short_t [numOfCh];
tree->SetBranchAddress("Seed",&Seed);
tree->SetBranchAddress("Run",&Run);
tree->SetBranchAddress("Event",&Event);
tree->SetBranchAddress("totalEnergyDeposited",&totalEnergyDeposited);
tree->SetBranchAddress("NumOptPhotons",&NumOptPhotons);
tree->SetBranchAddress("NumCherenkovPhotons",&NumCherenkovPhotons);
for (int i = 0 ; i < numOfCry ; i++)
{
std::stringstream snames;
snames << "cry" << i;
tree->SetBranchAddress(snames.str().c_str(),&pEdep[i]);
snames.str("");
snames<< "cry" << i << "PosXEnDep";
tree->SetBranchAddress(snames.str().c_str(),&px[i]);
snames.str("");
snames<< "cry" << i << "PosYEnDep";
tree->SetBranchAddress(snames.str().c_str(),&py[i]);
snames.str("");
snames<< "cry" << i << "PosZEnDep";
tree->SetBranchAddress(snames.str().c_str(),&pz[i]);
}
for (int i = 0 ; i < numOfCh ; i++)
{
std::stringstream snames;
snames << "detector" << i;
tree->SetBranchAddress(snames.str().c_str(),&detector[i]);
}
//output ttree
long long int DeltaTimeTag,ExtendedTimeTag;
Short_t charge[32]; //adc type is always 32 channels
Float_t RealX,RealY,RealZ;
Short_t CrystalsHit;
Short_t NumbOfInteractions;
std::vector <float> TotalCryEnergy;
std::vector <float>* pTotalCryEnergy;
pTotalCryEnergy = &TotalCryEnergy;
TTree* t1 = new TTree("adc","adc");
t1->Branch("ExtendedTimeTag",&ExtendedTimeTag,"ExtendedTimeTag/l"); //absolute time tag of the event
t1->Branch("DeltaTimeTag",&DeltaTimeTag,"DeltaTimeTag/l"); //delta time from previous event
t1->Branch("TotalCryEnergy","std::vector<float>",&pTotalCryEnergy);
//branches of the 32 channels data
for (int i = 0 ; i < 32 ; i++)
{
//empty the stringstreams
std::stringstream snames,stypes;
charge[i] = 0;
snames << "ch" << i;
stypes << "ch" << i << "/S";
t1->Branch(snames.str().c_str(),&charge[i],stypes.str().c_str());
}
t1->Branch("RealX",&RealX,"RealX/F");
t1->Branch("RealY",&RealY,"RealY/F");
t1->Branch("RealZ",&RealZ,"RealZ/F");
t1->Branch("CrystalsHit",&CrystalsHit,"CrystalsHit/S");
t1->Branch("NumbOfInteractions",&NumbOfInteractions,"NumbOfInteractions/S");
long int counter = 0;
int nEntries = tree->GetEntries();
std::cout << "nEntries = " << nEntries << std::endl;
for(int i = 0; i < nEntries ; i++)
{
tree->GetEvent(i);
ExtendedTimeTag = 1e-9;
DeltaTimeTag = 1e-9;
NumbOfInteractions = 0;
CrystalsHit = 0;
for(int i = 0; i < numOfCh ; i++)
{
//convert to ADC channels, as if it was data from a digitizer
//mppc gain = 1.25e6
//adc channel binning 156e-15 C
double adcCh = detector[i]*1.25e6*1.6e-19/156e-15;
charge[i*2] = (Short_t) adcCh;
}
RealX = RealY = RealZ = 0;
// calculate a weigthed energy deposition in x,y,z
for(int i = 0; i < numOfCry ; i++) //first total energy deposited
{
Float_t SumEnergy = 0;
NumbOfInteractions += px[i]->size();
if(px[i]->size()) CrystalsHit++;
for(int j = 0; j < px[i]->size(); j++)
{
RealX += (px[i]->at(j) * pEdep[i]->at(j))/totalEnergyDeposited;
}
for(int j = 0; j < px[i]->size(); j++)
{
RealY += (py[i]->at(j) * pEdep[i]->at(j))/totalEnergyDeposited;
}
for(int j = 0; j < px[i]->size(); j++)
{
RealZ += (pz[i]->at(j) * pEdep[i]->at(j))/totalEnergyDeposited;
}
for(int j = 0; j < px[i]->size(); j++)
{
SumEnergy += pEdep[i]->at(j);
}
TotalCryEnergy.push_back(SumEnergy);
}
/*
//find crystal with max energy deposition
Float_t MaxEnergyCry = 0;
Short_t MaxEnergyCryNum = -1;
MaxEnergyCry = *std::max_element(TotalCryEnergy.begin(), TotalCryEnergy.end());
MaxEnergyCryNum = std::distance(TotalCryEnergy.begin(), (std::max_element(TotalCryEnergy.begin(), TotalCryEnergy.end())));
*/
if(NumbOfInteractions > 0) // discard events with no energy deposition (they would never trigger the detectors anyway..)
{
t1->Fill();
}
counter++;
int perc = ((100*counter)/nEntries); //should strictly have not decimal part, written like this...
if( (perc % 10) == 0 )
{
std::cout << "\r";
std::cout << perc << "% done... ";
//std::cout << counter << std::endl;
}
TotalCryEnergy.clear();
}
std::cout << std::endl;
std::string outFile = "Tree_OUT.root";
TFile* fOut = new TFile(outFile.c_str(),"recreate");
t1->Write();
// f1->Close();
fOut->Close();
return 0;
}