/
coincidence.cpp
278 lines (235 loc) · 7.51 KB
/
coincidence.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
277
278
// g++ -Wl,--no-as-needed -o coincidence coincidence.cpp `root-config --cflags --glibs`
// procedure
// once the .root files are produced by gate, this should be run on each one of them to produce a elm2 file with all the possible coincidences
// all the root files will be in a folder
#include "TROOT.h"
#include "TStyle.h"
#include "TSystem.h"
#include "TLegend.h"
#include "TCanvas.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TString.h"
#include "TApplication.h"
#include "TLegend.h"
#include "TTree.h"
#include "TFile.h"
#include "TF2.h"
#include "TSpectrum.h"
#include "TSpectrum2.h"
#include "TTreeFormula.h"
#include "TMath.h"
#include "TChain.h"
#include "TCut.h"
#include "TLine.h"
#include "TError.h"
#include "TEllipse.h"
#include "TGraph2D.h"
#include <iostream>
#include <bitset> // std::bitset
#include <fstream>
#include <string>
#include <sstream>
#include "TChain.h"
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <cmath>
//declares the struct of events
struct EventFormat
{
double ts; // time of the event, in seconds. if i'm not mistaken is the absolute machine time in seconds
u_int8_t random; // set probably at the level of acq, says if the event is random (when it's value is 1)
float d; // distance between the heads, fixed to the head distance value...
float yozRot; // rotation of the heads
float x1; // x coordinate of the event, for detector 1
float y1; // y coordinate of the event, for detector 1
float z1; // z coordinate of the event, for detector 1
float e1; // energy deposited in detector 1
u_int8_t n1; // some check done probably at the level of acq, it's 1 if the event is ok, otherwise is not ok
float x2; // x coordinate of the event, for detector 2
float y2; // y coordinate of the event, for detector 2
float z2; // z coordinate of the event, for detector 2
float e2; // energy deposited in detector 2
u_int8_t n2; // some check done probably at the level of acq, it's 1 if the event is ok, otherwise is not ok
float dt; // delta time between event in detector 1 and event in detector 2
} __attribute__((__packed__));
int main(int argc, char** argv)
//int coincidence(TString filename)
{
std::cout << "Reading file " << argv[1] << std::endl;
//TFile *file = new TFile(argv[1], "READ");
std::string filename = argv[1];
std::string outFileName;
outFileName = filename.substr(0,filename.length() -5 );
outFileName += ".elm2";
TFile *file = new TFile(filename.c_str(), "UPDATE");
TTree *tree = (TTree *)file->Get("Singles");
Float_t distance = 209.059998; //real heads distance, in mm
Float_t yozRot = 0.0; //head rotation, in radiands
Float_t absTime = 0.0; //t_start of this take, from the beginning of the "exam"
if(argc > 2)
distance = atof(argv[2]);
if(argc > 3)
yozRot = atof(argv[3]);
if(argc > 4)
absTime = atof(argv[4]);
std::cout << "Heads distance [mm] = " << distance << std::endl;
std::cout << "Rotation [rad] = " << yozRot << std::endl;
std::cout << "Absolute initial time for this take [s] = " << absTime << std::endl;
u_int8_t randomSet = 0;
u_int8_t n1Set = 1;
u_int8_t n2Set = 1;
int entry;
double time;
Float_t xPos,yPos,zPos;
Float_t energy;
tree->SetBranchAddress("eventID",&entry);
tree->SetBranchAddress("time",&time);
tree->SetBranchAddress("globalPosX",&xPos);
tree->SetBranchAddress("globalPosY",&yPos);
tree->SetBranchAddress("globalPosZ",&zPos);
tree->SetBranchAddress("energy",&energy);
int ctrue = 0;
int crandom = 0;
long int nEvents = tree->GetEntries();
std::cout << std::endl;
std::cout << "nEvents = " << nEvents << std::endl;
const int depth = 100;
double dt = 4e-9;
double timeWindow = 90e-9;
//output binary file
ofstream output_file(outFileName.c_str(), std::ios::binary);
//count the coincidences generated
long int nCoincidence = 0;
for(int i = 0 ; i < nEvents ; i++)
{
//std::cout << "Event " << i << std::endl;
std::vector<double> t;
std::vector<Float_t> x,y,z,e;
std::vector<int> id;
//get the "first" single
tree->GetEntry(i);
t.push_back( time + absTime);
id.push_back( entry );
x.push_back( (Float_t) xPos );
y.push_back( (Float_t) yPos );
z.push_back( (Float_t) zPos );
e.push_back( (Float_t) energy );
// tree->GetEntry(i+1);
// t.push_back(time);
// e.push_back(entry);
// std::cout << "diff " << t[1] - t[0] << std::endl;
//get all the events in a 90ns time window
int j=0;
while( (fabs(t[j] - t[0]) < timeWindow) && ((i+j) < nEvents) )
{
j++;
tree->GetEntry(i+j);
t.push_back( time + absTime);
id.push_back(entry);
x.push_back( (Float_t) xPos);
y.push_back( (Float_t) yPos);
z.push_back( (Float_t) zPos);
e.push_back( (Float_t) energy);
//std::cout << "diff " << t[j] - t[0] << std::endl;
}
//std::cout << "size " << t.size() << std::endl;
for(int j = 1 ; j < t.size() ; j++)
{
//first check, the coincidence is NOT in the same detector head
if(z[0]*z[j] < 0)
{
//prepare the coincidence event to be written
nCoincidence++;
EventFormat fe;
fe.ts = t[0];
fe.random = randomSet;
fe.d = distance;
fe.yozRot = yozRot;
// //invert x with y - definitions in clearpem are different
// fe.x1 = y[0];
// fe.y1 = x[0];
// fe.z1 = z[0];
// fe.e1 = e[0] * 1000;
// fe.n1 = n1Set;
// // invert x with y - definitions in clearpem are different
// fe.x2 = y[j];
// fe.y2 = x[j];
// fe.z2 = z[j];
// fe.e2 = e[j] * 1000;
// fe.n2 = n2Set;
// fe.dt = fabs(t[j] - t[0]);
//geometry fixed in the simulation, axis don't need to be inverted anymore
fe.x1 = x[0];
fe.y1 = y[0];
fe.z1 = z[0];
fe.e1 = e[0] * 1000;
fe.n1 = n1Set;
fe.x2 = x[j];
fe.y2 = y[j];
fe.z2 = z[j];
fe.e2 = e[j] * 1000;
fe.n2 = n2Set;
fe.dt = fabs(t[j] - t[0]);
//debug
// std::cout << fe.ts
// << " "
// << std::bitset<8>(fe.random)
// << " "
// << fe.d
// << " "
// << fe.x1
// << " "
// << fe.y1
// << " "
// << fe.z1
// << " "
// << fe.e1
// << " "
// << std::bitset<8>(fe.n1)
// << " "
// << fe.x2
// << " "
// << fe.y2
// << " "
// << fe.z2
// << " "
// << fe.e2
// << " "
// << std::bitset<8>(fe.n2)
// << " "
// << fe.dt
// << std::endl;
//write data to file
output_file.write((char*)&fe,sizeof(fe));
if(fabs(t[j] - t[0]) < dt)
{
if(id[j] == id[0])
{
//std::cout << "true " << t[j] - t[0] << " " << id[0] << " " << id[j] << std::endl;
ctrue++;
}
else
{
//std::cout << "random " << t[j] - t[0] << " " << id[0] << " " << id[j] << std::endl;
crandom++;
}
}
}
//delete t;
//delete e;
//cout << i << "\t" << entry<< "\t" << std::setprecision(11) << time << endl;
}
}
output_file.close();
std::cout << std::endl;
std::cout << "Number of coincidences written = " << nCoincidence << std::endl;
std::cout << "Trues = " << ctrue << std::endl;
std::cout << "Randoms = " << crandom << std::endl;
std::cout << std::endl;
std::cout << "Output written to " << outFileName << std::endl;
return 0;
}