-
Notifications
You must be signed in to change notification settings - Fork 2
/
entangled.cpp
365 lines (322 loc) · 12.3 KB
/
entangled.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "entangled.h"
#include <iostream>
#include <fstream>
#include <TTree.h>
#include <TThread.h>
#include <TH1.h>
#include <TCanvas.h>
Entangled::Entangled(TString fileName, TString tree, UInt_t maxEntries)
{
time_.Start();
if (fileName.EndsWith(".root") && !fileName.EndsWith("_processed.root"))
{
std::cout << "Starting init: " << std::endl;
Init(fileName, tree);
std::cout << "Starting entanglement processing: " << std::endl;
Process();
outputRoot_->Close();
}
else
{
std::cerr << "Wrong input file " << fileName << std::endl;
}
time_.Stop();
time_.Print();
}
Entangled::~Entangled()
{
for (Int_t x1 = 0; x1 < X1_CUT; x1++)
{
for (Int_t y1 = 0; y1 < Y1_CUT; y1++)
{
for (Int_t x2 = 0; x2 < X2_CUT; x2++)
{
for (Int_t y2 = 0; y2 < Y2_CUT; y2++)
{
delete entTree_[x1][y1][x2][y2];
}
}
}
}
}
void Entangled::Init(TString file, TString tree)
{
std::cout << "Reading file" << std::endl;
inputName_ = file;
std::cout << "Reading tree" << std::endl;
fileRoot_ = new TFile(inputName_, "READ");
rawTree_ = (TTree *) fileRoot_->Get(tree);
std::cout << " - setting branches" << std::endl;
rawTree_->SetBranchAddress("Size", &Size_);
rawTree_->SetBranchAddress("Col", Cols_);
rawTree_->SetBranchAddress("Row", Rows_);
rawTree_->SetBranchAddress("ToT", ToTs_);
rawTree_->SetBranchAddress("ToA", ToAs_);
Entries_ = rawTree_->GetEntries();
std::cout << "Create writing file" << std::endl;
outputName_ = inputName_;
outputName_.ReplaceAll(".root","_"+tree+"_processed.root");
outputRoot_ = new TFile(outputName_,"RECREATE");
id_ = 0;
mainEntry_ = 0;
nextEntry_ = 0;
for (Int_t x1 = 0; x1 < X1_CUT; x1++)
{
for (Int_t y1 = 0; y1 < Y1_CUT; y1++)
{
for (Int_t i = 0; i < 2; i++)
{
area1_[x1][y1][i] = X1_LOW + ((i+x1)*(X1_SIZE/X1_CUT));
area1_[x1][y1][i+2] = Y1_LOW + ((i+y1)*(Y1_SIZE/Y1_CUT));
}
}
}
for (Int_t x2 = 0; x2 < X2_CUT; x2++)
{
for (Int_t y2 = 0; y2 < Y2_CUT; y2++)
{
for (Int_t i = 0; i < 2; i++)
{
area2_[x2][y2][i] = X2_LOW + ((i+x2)*(X2_SIZE/X2_CUT));
area2_[x2][y2][i+2] = Y2_LOW + ((i+y2)*(Y2_SIZE/Y2_CUT));
}
}
}
for (Int_t x1 = 0; x1 < X1_CUT; x1++)
{
for (Int_t y1 = 0; y1 < Y1_CUT; y1++)
{
for (Int_t x2 = 0; x2 < X2_CUT; x2++)
{
for (Int_t y2 = 0; y2 < Y2_CUT; y2++)
{
std::cout <<"Areas: " << std::endl;
std::cout << "Area1: " << area1_[x1][y1][0] << "," << area1_[x1][y1][1] << "x" << area1_[x1][y1][2] << "," << area1_[x1][y1][3] << std::endl;
std::cout << "Area2: " << area2_[x2][y2][0] << "," << area2_[x2][y2][1] << "x" << area2_[x2][y2][2] << "," << area2_[x2][y2][3] << std::endl;
TString tmpName;
tmpName.Form("entry_%dx%d_%dx%d",x1,y1,x2,y2);
combinationDir_[x1][y1][x2][y2] = outputRoot_->mkdir(tmpName);
combinationDir_[x1][y1][x2][y2]->cd();
TTree* tmpTree = new TTree("entTree","entangled data for sections");
tmpTree->Branch("ID" , &id_,"ID/i");
tmpTree->Branch("mE" ,&mainEntry_,"mE/i");
tmpTree->Branch("nE" ,&nextEntry_,"nE/i");
tmpTree->Branch("Size" , &Size_,"Size/i");
tmpTree->Branch("Col" , Cols_,"Col[Size]/i");
tmpTree->Branch("Row" , Rows_,"Row[Size]/i");
tmpTree->Branch("ToT" , ToTs_,"ToT[Size]/i");
tmpTree->Branch("ToA" , ToAs_,"ToA[Size]/l");
tmpTree->Branch("Size2", &Size2_,"Size2/i");
tmpTree->Branch("Col2" , Cols2_,"Col2[Size2]/i");
tmpTree->Branch("Row2" , Rows2_,"Row2[Size2]/i");
tmpTree->Branch("ToT2" , ToTs2_,"ToT2[Size2]/i");
tmpTree->Branch("ToA2" , ToAs2_,"ToA2[Size2]/l");
entTree_[x1][y1][x2][y2] = tmpTree;
}
}
}
}
}
Bool_t Entangled::PositionCheck(UInt_t area[4])
{
if ( !(ToTs_[0] <= CUT_TOT) && !(Size_ < CUT_SIZE) && Cols_[0] >= area[0] && Cols_[0] < area[1] && Rows_[0] >= area[2] && Rows_[0] < area[3] )
return kTRUE;
else
return kFALSE;
}
UInt_t Entangled::FindPairs(UInt_t area[4], Int_t &entry)
{
ULong64_t diffToA = (ULong64_t) (163.84 * MAX_DIFF);
Bool_t bFound = kFALSE;
Bool_t bSmaller = kTRUE;
UInt_t nextEntry;
// find pairs
for (Int_t pair = std::max(entry - ENTRY_LOOP, 0); pair < std::min(entry + ENTRY_LOOP, Entries_); pair++)
{
rawTree_->GetEntry(pair);
if (pair == entry)
{
bSmaller = kFALSE;
continue;
}
// area of incoming photons
if ( PositionCheck(area) )
{
if ( bSmaller && (ToAs2_[0] - ToAs_[0]) < diffToA )
{
diffToA = ToAs2_[0] - ToAs_[0];
nextEntry = pair;
bFound = kTRUE;
}
else if ( !bSmaller && (ToAs_[0] - ToAs2_[0]) < diffToA )
{
diffToA = ToAs_[0] - ToAs2_[0];
nextEntry = pair;
bFound = kTRUE;
}
}
}
if (bFound)
{
return nextEntry;
}
return 0;
}
void Entangled::ScanEntry(Int_t &entry)
{
rawTree_->GetEntry(entry);
if (entry % 1000 == 0) std::cout << "Entry " << entry << " of " << Entries_ << " done!" << std::endl;
// area of incoming photons
if ( PositionCheck(area1All_) || PositionCheck(area2All_))
{
mainEntry_ = entry;
Size2_ = Size_;
for (UInt_t subentry = 0; subentry < Size_; subentry++)
{
Cols2_[subentry] = Cols_[subentry];
Rows2_[subentry] = Rows_[subentry];
ToAs2_[subentry] = ToAs_[subentry];
ToTs2_[subentry] = ToTs_[subentry];
}
Int_t globalX1 = (Cols2_[0] - X1_LOW) / (X1_SIZE/X1_CUT);
Int_t globalY1 = (Rows2_[0] - Y1_LOW) / (Y1_SIZE/Y1_CUT);
Int_t globalX2 = (Cols2_[0] - X2_LOW) / (X2_SIZE/X2_CUT);
Int_t globalY2 = (Rows2_[0] - Y2_LOW) / (Y2_SIZE/Y2_CUT);
if (PositionCheck(area1All_))
{
for (Int_t x2 = 0; x2 < X2_CUT; x2++)
{
for (Int_t y2 = 0; y2 < Y2_CUT; y2++)
{
nextEntry_ = FindPairs(area2_[x2][y2],entry);
if (nextEntry_ != 0)
{
id_ = 0;
rawTree_->GetEntry(nextEntry_);
entTree_[globalX1][globalY1][x2][y2]->Fill();
}
}
}
for (Int_t x1 = 0; x1 < X1_CUT; x1++)
{
for (Int_t y1 = 0; y1 < Y1_CUT; y1++)
{
nextEntry_ = FindPairs(area1_[x1][y1],entry);
if (nextEntry_ != 0)
{
id_ = 1;
rawTree_->GetEntry(nextEntry_);
entTree_[globalX1][globalY1][x1][y1]->Fill();
}
}
}
}
else
{
for (Int_t x2 = 0; x2 < X2_CUT; x2++)
{
for (Int_t y2 = 0; y2 < Y2_CUT; y2++)
{
nextEntry_ = FindPairs(area2_[x2][y2],entry);
if (nextEntry_ != 0)
{
id_ = 2;
rawTree_->GetEntry(nextEntry_);
entTree_[globalX2][globalY2][x2][y2]->Fill();
}
}
}
}
}
}
void Entangled::Process()
{
outputRoot_->cd();
for (Int_t entry = 0; entry < Entries_; entry++)
{
if (maxEntries_ != 0 && entry >= maxEntries_)
break;
ScanEntry(entry);
}
outputRoot_->cd();
for (Int_t x1 = 0; x1 < X1_CUT; x1++)
{
for (Int_t y1 = 0; y1 < Y1_CUT; y1++)
{
for (Int_t x2 = 0; x2 < X2_CUT; x2++)
{
for (Int_t y2 = 0; y2 < Y2_CUT; y2++)
{
TString csvName = outputName_;
TString pdfName = outputName_;
csvName.Remove(csvName.Last('.'),200);
csvName.Append(".csv");
pdfName.Remove(csvName.Last('.'),200);
pdfName.Append(".pdf");
TCanvas* canvas = new TCanvas(Form("canvas_%dx%d_%dx%d",x1,y1,x2,y2), Form("canvas_%dx%d_%dx%d",x1,y1,x2,y2), 400, 400);
std::ofstream csvFile;
csvFile.open(csvName);
TH1F* ent = new TH1F(Form("histEnt_%dx%d_%dx%d",x1,y1,x2,y2), Form("Entangled %dx%d_%dx%d",x1,y1,x2,y2), 1 + ((2.0*MAX_DIFF)/1.5625), -MAX_DIFF-0.78125, MAX_DIFF+0.78125);
TH1F* single1 = new TH1F(Form("histSingle1_%dx%d_%dx%d",x1,y1,x2,y2), Form("Fiber 1 %dx%d_%dx%d",x1,y1,x2,y2), 1 + ((2.0*MAX_DIFF)/1.5625), -MAX_DIFF-0.78125, MAX_DIFF+0.78125);
TH1F* single2 = new TH1F(Form("histSingle2_%dx%d_%dx%d",x1,y1,x2,y2), Form("Fiber 2 %dx%d_%dx%d",x1,y1,x2,y2), 1 + ((2.0*MAX_DIFF)/1.5625), -MAX_DIFF-0.78125, MAX_DIFF+0.78125);
Float_t dToA;
Bool_t bSign;
for (Int_t entry = 0; entry < entTree_[x1][y1][x2][y2]->GetEntries(); entry++)
{
entTree_[x1][y1][x2][y2]->GetEntry(entry);
if (entry % 1000 == 0)
{
std::cout << "Entangled entry: " << entry << std::endl;
}
if (ToAs2_[0] > ToAs_[0])
{
dToA = (Float_t) (ToAs2_[0] - ToAs_[0]);
bSign = kTRUE;
}
else
{
dToA = (Float_t) (ToAs_[0] - ToAs2_[0]);
bSign = kFALSE;
}
dToA *= 25.0/4096;
if (id_ == 0)
{
if (bSign)
ent->Fill(dToA);
else
ent->Fill(-dToA);
}
else if (id_ == 1)
{
if (bSign)
single1->Fill(dToA);
}
else if (id_ == 2)
{
if (!bSign)
single2->Fill(-dToA);
}
}
ent->SetLineColor(1);
single1->SetLineColor(2);
ent->Draw();
single1->Draw("SAME");
single2->Draw("SAME");
canvas->Print(pdfName);
canvas->Close();
for (Int_t bin = 1; bin <= ent->GetNbinsX(); bin++)
{
if (bin < ent->GetNbinsX()/2)
csvFile << ent->GetBinCenter(bin) << "," << ent->GetBinContent(bin) << "," << single2->GetBinContent(bin) << "\n";
else if (bin > 1 + ent->GetNbinsX()/2)
csvFile << ent->GetBinCenter(bin) << "," << ent->GetBinContent(bin) << "," << single1->GetBinContent(bin) << "\n";
else
csvFile << ent->GetBinCenter(bin) << "," << ent->GetBinContent(bin) << "," << (single1->GetBinContent(bin)+single2->GetBinContent(bin)) << "\n";
}
csvFile.close();
}
}
}
}
outputRoot_->Write();
}