-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataprocess.cpp
1644 lines (1400 loc) · 57.2 KB
/
dataprocess.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include "dataprocess.h"
#include "TSystem.h"
#include "TFile.h"
#include "TCanvas.h"
using namespace std;
DataProcess::DataProcess()
{
Init();
}
DataProcess::~DataProcess()
{
delete m_sigHandler;
}
void DataProcess::Init()
{
m_chainCounter = 0;
//max entries in one tree set to 30 bits
m_treeMaxEntries = 0x020000000;
m_bCorrCsv = kFALSE;
setCorrection(CorrType::corrNew);
setProcess(procAll);
setOptions();
m_sigHandler = new TSignalHandler(kSigInterrupt);
m_sigHandler->Add();
m_sigHandler->Connect("Notified()", "DataProcess", this, "StopLoop()");
m_numInputs = 1;
m_trigCnt = 0;
m_maxEntries = 0;
m_bFirstTrig = kFALSE;
m_bDevID = kFALSE;
}
void DataProcess::setName(TString fileNameInput)
{
m_numInputs = 1;
m_fileNameInput.push_back(fileNameInput);
std::cout << "file name input " << m_fileNameInput.back() << std::endl;
}
void DataProcess::setName(TObjString* fileNameInput, Int_t size)
{
m_numInputs = static_cast<ULong64_t>(size);
for (ULong64_t input = 0; input < m_numInputs; input++)
{
m_fileNameInput.push_back( fileNameInput[input].GetString()) ;
std::cout << "file name input " << m_fileNameInput.back() << " number " << input << std::endl;
}
}
void DataProcess::setProcess(ProcType process)
{
m_process = process;
}
void DataProcess::setCorrection(CorrType correction, TString fileNameCorrection)
{
m_correction = correction;
switch (m_correction)
{
case corrNew:
{
m_correctionName = fileNameCorrection;
std::cout << "Creating new correction" << std::endl;
if (fileNameCorrection.Sizeof() > 1 )
{
closeCorr();
std::cout << " - with file" << m_correctionName << std::endl;
openCorr(kTRUE);
m_bCorrCsv = kTRUE;
}
else
{
m_bCorrCsv = kFALSE;
}
break;
}
case corrUse:
{
m_correctionName = fileNameCorrection;
std::cout << "Using correction name " << m_correctionName << std::endl;
openCorr(kFALSE);
m_bCorrCsv = kTRUE;
break;
}
case corrOff:
{
std::cout << "Ignoring correction" << std::endl;
m_bCorrCsv = kFALSE;
break;
}
default:
break;
}
}
void DataProcess::setNEntries(UInt_t nEntries)
{
m_maxEntries = nEntries;
}
Int_t DataProcess::setOptions(Bool_t bCol,
Bool_t bRow,
Bool_t bToT,
Bool_t bToA,
Bool_t bTrig,
Bool_t bTrigTime,
Bool_t bTrigToA,
Bool_t bProcTree,
Bool_t bCsv,
Bool_t bCentroid,
Int_t gapPixel,
Float_t gapTime,
Bool_t bNoTrigWindow,
Float_t timeWindow,
Float_t timeStart,
Bool_t singleFile,
Int_t linesPerFile)
{
m_bCol = bCol;
m_bRow = bRow;
m_bToT = bToT;
m_bToA = bToA;
m_bTrig = bTrig;
m_bTrigTime = bTrigTime;
m_bTrigToA = bTrigToA;
m_bProcTree = bProcTree;
m_bCsv = bCsv;
m_bCentroid = bCentroid;
m_gapTime = (ULong64_t) (gapTime * 163840); // conversion from us ToA size
m_gapPix = gapPixel;
m_bNoTrigWindow = bNoTrigWindow;
m_bSingleFile = singleFile;
m_timeWindow = timeWindow;
m_timeStart = timeStart;
m_linesPerFile = linesPerFile;
if (timeWindow/1000 > 500)
{
std::cout << " ========================================== " << std::endl;
std::cout << " === " << timeWindow << " us is very large winwdow!!!" << std::endl;
std::cout << " ========================================== " << std::endl;
return -1;
}
return 0;
}
Int_t DataProcess::process()
{
m_time.Start();
m_pixelCounter = 0;
//
// create filenames
if ( processFileNames() != 0) return -1;
//
// switch to decide what to do with the input
// either open dat file, root file or both
// close files after finished with them
// at closing of the root file, create plots
switch (m_process)
{
case procDat:
if ( openDat()) return -1;
if ( openRoot()) return -1;
processDat();
closeDat();
break;
case procRoot:
if ( openRoot()) return -1;
processRoot();
break;
case procAll:
if ( openDat()) return -1;
if ( openRoot()) return -1;
processDat();
processRoot();
closeDat();
break;
default :
std::cout << " ========================================== " << std::endl;
std::cout << " ====== COULD NOT DECIDE WHAT TO DO ======= " << std::endl;
std::cout << " ========================================== " << std::endl;
return -2;
}
if (m_correction != corrOff)
closeCorr();
closeRoot();
closeCsv();
//
// print time of the code execution to the console
m_time.Stop();
m_time.Print();
return 0;
}
void DataProcess::plotStandardData()
{
TCanvas* canvas;
if (m_trigCnt != 0)
{
canvas = new TCanvas("canvas", m_fileNamePdf, 1200, 1200);
canvas->Divide(3,3);
}
else
{
canvas = new TCanvas("canvas", m_fileNamePdf, 1200, 800);
canvas->Divide(3,2);
}
canvas->cd(1);
gPad->SetLogz();
gPad->SetRightMargin(static_cast<Float_t>(0.15));
m_pixelMap->SetStats(kFALSE);
m_pixelMap->Draw("colz");
canvas->cd(2);
gPad->SetLogz();
gPad->SetRightMargin(static_cast<Float_t>(0.15));
m_pixelMapToT->SetStats(kFALSE);
m_pixelMapToT->Draw("colz");
if (m_bCentroid)
{
canvas->cd(3);
gPad->SetLogz();
gPad->SetRightMargin(static_cast<Float_t>(0.15));
m_pixelCentMap->SetStats(kFALSE);
m_pixelCentMap->Draw("colz");
}
canvas->cd(5);
gPad->SetRightMargin(static_cast<Float_t>(0.15));
m_histToT->Draw();
canvas->cd(6);
gPad->SetRightMargin(static_cast<Float_t>(0.15));
m_histToA->Draw();
if (m_trigCnt != 0)
{
canvas->cd(4);
gPad->SetRightMargin(static_cast<Float_t>(0.15));
m_histTrigger->Draw();
if ( m_bTrigToA )
{
canvas->cd(7);
gPad->SetRightMargin(static_cast<Float_t>(0.15));
m_histSpectrum->Draw();
if (m_bCentroid)
{
canvas->cd(8);
gPad->SetRightMargin(static_cast<Float_t>(0.15));
m_histCentSpectrum->Draw();
}
}
}
canvas->Print(m_fileNamePath + m_fileNamePdf);
canvas->Close();
}
Int_t DataProcess::processFileNames()
{
deque<Int_t > inputNum;
TString tmpString;
for (ULong64_t input = 0; input < m_numInputs; input++)
{
if (m_fileNameInput[input].Sizeof()==1) return -1;
Int_t dotPos = m_fileNameInput[input].Last('.');
Int_t dashPos = m_fileNameInput[input].Last('-');
tmpString = m_fileNameInput[input];
TString tmpNum ( tmpString(dashPos+1,dotPos-dashPos-1) );
inputNum.push_back( tmpNum.Atoi() );
}
for (ULong64_t input = 0; input < m_numInputs; input++)
{
ULong64_t j = input;
while (j > 0 && inputNum[j-1] > inputNum[j])
{
swap(inputNum[j-1],inputNum[j]);
swap(m_fileNameInput[j-1],m_fileNameInput[j]);
j--;
}
}
tmpString = m_fileNameInput[0];
if (tmpString.EndsWith(".dat"))
m_type = dtDat;
else if (tmpString.EndsWith(".tpx3"))
m_type = dtTpx;
Int_t slash = tmpString.Last('/');
m_fileNamePath = tmpString(0, slash + 1);
for (ULong64_t input = 0; input < m_numInputs; input++)
{
m_fileNameInput[input].Remove(0, slash + 1);
m_fileNameDat.push_back( m_fileNameInput[input] );
//
// if one file only, keep number
Int_t repPos;
if (m_numInputs == 1) repPos = m_fileNameInput[input].Last('.');
else repPos = m_fileNameInput[input].Last('-');
m_fileName = m_fileNameInput[input].Replace(repPos, 200, ".");
m_fileNamePdf = m_fileNameInput[input].Replace(repPos, 200, ".pdf");
m_fileNameCsv = m_fileNameInput[input].Replace(repPos, 200, ".csv");
m_fileNameCentCsv = m_fileNameInput[input].Replace(repPos, 200, "_cent.csv");
}
return 0;
}
Int_t DataProcess::openCorr(Bool_t create)
{
if (create)
m_fileCorr = fopen(m_correctionName, "w+");
else
{
m_fileCorr = fopen(m_correctionName, "r");
loadCorrection();
}
if (m_fileCorr == NULL)
{
std::cout << " ========================================== " << std::endl;
std::cout << " == COULD NOT OPEN DAT, PLEASE CHECK IT === " << std::endl;
std::cout << " ========================================== " << std::endl;
return -1;
}
return 0;
}
Int_t DataProcess::openDat(ULong64_t fileCounter)
{
FILE* fileDat = fopen(m_fileNamePath + m_fileNameDat[fileCounter], "r");
std::cout << m_fileNameDat[fileCounter] << " at " << m_fileNamePath << std::endl;
if (fileDat == NULL)
{
std::cout << " ========================================== " << std::endl;
std::cout << " == COULD NOT OPEN DAT, PLEASE CHECK IT === " << std::endl;
std::cout << " ========================================== " << std::endl;
return -1;
}
m_filesDat.push_back(fileDat);
return 0;
}
Int_t DataProcess::openCsv(DataType type, TString fileCounter)
{
TString fileNameTmp;
if (type == dtCent)
{
fileNameTmp = m_fileNameCentCsv;
}
else
{
fileNameTmp = m_fileNameCsv;
}
Int_t dotPos = fileNameTmp.Last('.');
if (fileCounter.Sizeof() != 1)
{
fileNameTmp.Replace(dotPos, 200, "-" + fileCounter + ".csv");
}
FILE* fileCsv = fopen(m_fileNamePath + fileNameTmp, "w");
std::cout << fileNameTmp << " at " << m_fileNamePath << std::endl;
if (fileCsv == NULL)
{
std::cout << " ========================================== " << std::endl;
std::cout << " = COULD NOT OPEN CSV, "<< fileCounter << " PLEASE CHECK IT == " << std::endl;
std::cout << " ========================================== " << std::endl;
return -1;
}
deque<FILE* >* files;
if (type == dtCent)
{
files = &m_filesCentCsv;
}
else
{
files = &m_filesCsv;
}
files->push_back(fileCsv);
if (m_bTrig) fprintf(files->back(), "#TrigId,");
if (m_bTrigTime)fprintf(files->back(), "#TrigTime,");
if (m_bCol) fprintf(files->back(), "#Col,");
if (m_bRow) fprintf(files->back(), "#Row,");
if (m_bToA) fprintf(files->back(), "#ToA,");
if (m_bToT) fprintf(files->back(), "#ToT[arb],");
if (m_bTrigToA) fprintf(files->back(), "#Trig-ToA[arb],");
if (type == dtCent)
if (m_bCentroid)fprintf(files->back(), "#Centroid,");
if (m_correction != corrOff && m_bTrigToA) fprintf(files->back(), "#cTrig-ToA[us],");
fprintf(files->back(), "\n");
return 0;
}
Int_t DataProcess::openRoot(ULong64_t fileCounter)
{
//
// Open file
TFile * fileRoot = NULL;
TString fileNameTmp = m_fileName;
m_fileNameRoot.push_back(fileNameTmp.Replace(fileNameTmp.Last('.'),200, "_c" + TString::Format("%lld", fileCounter) + ".root"));
if ( m_process == procDat || m_process == procAll)
{
fileRoot = new TFile(m_fileNamePath + m_fileNameRoot.back(), "RECREATE");
std::cout << m_fileNameRoot.back() << " at " << m_fileNamePath << std::endl;
//
// check if file opened correctly
if (fileRoot == NULL)
{
std::cout << " ========================================== " << std::endl;
std::cout << " == COULD NOT OPEN ROOT, PLEASE CHECK IT == " << std::endl;
std::cout << " ========================================== " << std::endl;
return -1;
}
//
// create trees
m_rawTree.push_back(new TTree("rawtree", "raw data, Version 0.1.0"));
m_rawTree.back()->Branch("Size", &m_Size, "Size/i");
m_rawTree.back()->Branch("Col", m_Cols, "Col[Size]/i");
m_rawTree.back()->Branch("Row", m_Rows, "Row[Size]/i");
m_rawTree.back()->Branch("ToT", m_ToTs, "ToT[Size]/i");
m_rawTree.back()->Branch("ToA", m_ToAs, "ToA[Size]/l"); // l for long
m_timeTree.push_back(new TTree("timetree", "TDC counts"));
m_timeTree.back()->Branch("TrigCntr", &m_trigCnt, "TrigCntr/i");
m_timeTree.back()->Branch("TrigTime", &m_trigTime,"TrigTime/l");
//
// create plots
if (m_filesRoot.size() == 0)
{
m_pixelMap = new TH2I("pixelMap", "Col:Row", 256, -0.5, 255.5, 256, -0.5, 255.5);
m_pixelMapToT = new TH2F("pixelMapToT", "Col:Row:{ToT}", 256, -0.5, 255.5, 256, -0.5, 255.5);
m_pixelMapToA = new TH2F("pixelMapToA", "Col:Row:{ToA}", 256, -0.5, 255.5, 256, -0.5, 255.5);
m_histToT = new TH1I("histToT", "ToT", 200, 0, 5000);
m_histToA = new TH1I("histToA", "ToA", 1000, 0, 0);
m_pixelCentMap = new TH2I("pixelCentMap", "Col:Row", 256, -0.5, 255.5, 256, -0.5, 255.5);
m_pixelCentMapToT = new TH2F("pixelCentMapToT", "Col:Row:{ToT}", 256, -0.5, 255.5, 256, -0.5, 255.5);
m_pixelCentMapToA = new TH2F("pixelCentMapToA", "Col:Row:{ToA}", 256, -0.5, 255.5, 256, -0.5, 255.5);
m_histCentToT = new TH1I("histCentToT", "ToT", 200, 0, 5000);
m_histCentToA = new TH1I("histCentToA", "ToA", 1000, 0, 0);
m_scanDir = fileRoot->mkdir("ToTvsToA_scan");
m_scanDir->cd();
for (int totCounter = 0; totCounter < 1023; totCounter++)
{
m_histCentScan[totCounter] = new TH2F(Form("ToTvsToA_%d",totCounter*25),Form("ToTvsToA_%d",totCounter*25), 600, -468.75, 468.75, 400, 0, 10000);
}
fileRoot->cd();
m_mapCorr = new TH2F("cToTvsToT", "cToT:ToT", 400, 0, 10000, 400, 0, 10000);
m_mapCorrErr = new TH2F("cToTvsToTErr", "cToT:ToT:{Err}", 400, 0, 10000, 400, 0, 10000);
m_histCentToTvsToA = new TH2F("CentroidToTvsToA", "ToA:ToT", 300, -234.375, 234.375, 400, 0, 10000);
m_histCorrToTvsToA = new TH2F("CorrectedToTvsToA", "ToA:ToT", 300, -234.375, 234.375, 400, 0, 10000);
m_histTrigger = new TH1I("histTrigger", "Trigger", 1000, 0, 0);
}
}
else if (m_process == procRoot)
{
fileRoot = new TFile(m_fileNamePath + m_fileNameRoot.back(), "UPDATE");
std::cout << m_fileNameRoot.back() << " at " << m_fileNamePath << std::endl;
//
// check if file opened correctly
if (fileRoot == NULL)
{
std::cout << " ========================================== " << std::endl;
std::cout << " == COULD NOT OPEN ROOT, PLEASE CHECK IT == " << std::endl;
std::cout << " ========================================== " << std::endl;
return -1;
}
//
// read trees
m_rawTree.push_back(reinterpret_cast<TTree*>(fileRoot->Get("rawtree")));
m_rawTree.back()->SetBranchAddress("Size", &m_Size);
m_rawTree.back()->SetBranchAddress("Col", m_Cols);
m_rawTree.back()->SetBranchAddress("Row", m_Rows);
m_rawTree.back()->SetBranchAddress("ToT", m_ToTs);
m_rawTree.back()->SetBranchAddress("ToA", m_ToAs);
m_timeTree.push_back(reinterpret_cast<TTree*>(fileRoot->Get("timetree")));
m_timeTree.back()->SetBranchAddress("TrigCntr", &m_trigCnt);
m_timeTree.back()->SetBranchAddress("TrigTime", &m_trigTime);
if (m_filesRoot.size() == 0)
{
//
// read plots
m_pixelMap = reinterpret_cast<TH2I*>(fileRoot->Get("pixelMap"));
m_pixelMapToT = reinterpret_cast<TH2F*>(fileRoot->Get("pixelMapToT"));
m_pixelMapToA = reinterpret_cast<TH2F*>(fileRoot->Get("pixelMapToA"));
m_histToT = reinterpret_cast<TH1I*>(fileRoot->Get("histToT"));
m_histToA = reinterpret_cast<TH1I*>(fileRoot->Get("histToA"));
m_pixelCentMap = reinterpret_cast<TH2I*>(fileRoot->Get("pixelCentMap"));
m_pixelCentMapToT = reinterpret_cast<TH2F*>(fileRoot->Get("pixelCentMapToT"));
m_pixelCentMapToA = reinterpret_cast<TH2F*>(fileRoot->Get("pixelCentMapToA"));
m_histCentToT = reinterpret_cast<TH1I*>(fileRoot->Get("histCentToT"));
m_histCentToA = reinterpret_cast<TH1I*>(fileRoot->Get("histCentToA"));
m_histCentToTvsToA = reinterpret_cast<TH2F*>(fileRoot->Get("CentroidToTvsToA"));
m_histCorrToTvsToA = reinterpret_cast<TH2F*>(fileRoot->Get("CorrectedToTvsToA"));
m_histTrigger = reinterpret_cast<TH1I*>(fileRoot->Get("histTrigger"));
}
}
if (fileRoot != NULL)
{
m_filesRoot.push_back(fileRoot);
m_filesRoot.back()->cd();
return 0;
}
else
{
return -1;
}
}
void DataProcess::openChain()
{
std::cout << " ========================================== " << std::endl;
std::cout << " ============ SETTING UP CHAIN ============ " << std::endl;
std::cout << " ========================================== " << std::endl;
m_rawChain = new TChain("rawtree" );
m_rawChain->SetBranchAddress("Size", &m_Size);
m_rawChain->SetBranchAddress("Col", m_Cols);
m_rawChain->SetBranchAddress("Row", m_Rows);
m_rawChain->SetBranchAddress("ToT", m_ToTs);
m_rawChain->SetBranchAddress("ToA", m_ToAs);
m_timeChain = new TChain("timetree");
m_timeChain->SetBranchAddress("TrigCntr", &m_trigCnt);
m_timeChain->SetBranchAddress("TrigTime", &m_trigTime);
for (ULong64_t nameCnt = 0; nameCnt < m_fileNameRoot.size(); nameCnt++)
{
std::cout << "Add: " << m_fileNamePath + m_fileNameRoot[nameCnt] << std::endl;
m_filesRoot[nameCnt]->cd();
m_rawTree[nameCnt]->Write();
m_timeTree[nameCnt]->Write();
m_rawChain ->Add(m_fileNamePath + m_fileNameRoot[nameCnt]);
m_timeChain->Add(m_fileNamePath + m_fileNameRoot[nameCnt]);
}
}
void DataProcess::closeCorr()
{
if (m_bCorrCsv)
fclose(m_fileCorr);
while (m_lookupTable.size() != 0)
{
m_lookupTable.pop_front();
}
}
void DataProcess::closeDat()
{
while (m_filesDat.size() != 0)
// for (UInt_t size = 0; size < m_filesDat.size(); size++)
{
fclose(m_filesDat.front());
m_filesDat.pop_front();
m_fileNameInput.pop_front();
m_fileNameDat.pop_front();
}
}
void DataProcess::closeCsv()
{
while (m_filesCsv.size() != 0)
{
fclose(m_filesCsv.front());
m_filesCsv.pop_front();
}
while (m_bCentroid && m_filesCentCsv.size() != 0)
{
fclose(m_filesCentCsv.front());
m_filesCentCsv.pop_front();
}
}
void DataProcess::closeRoot()
{
if (m_nCent != 0 || m_nTime != 0)
plotStandardData();
while (m_filesRoot.size() != 0)
{
m_filesRoot.front()->cd();
m_filesRoot.front()->Write();
m_filesRoot.front()->Close();
m_filesRoot.pop_front();
}
}
Int_t DataProcess::skipHeader()
{
TNamed *deviceID = new TNamed("deviceID", "device ID not set");
Int_t retVal;
UInt_t sphdr_id;
UInt_t sphdr_size;
retVal = fread( &sphdr_id, sizeof(UInt_t), 1, m_filesDat.back());
retVal = fread( &sphdr_size, sizeof(UInt_t), 1, m_filesDat.back());
std::cout << hex << sphdr_id << dec << std::endl;
std::cout << "header size " << sphdr_size << std::endl;
if (sphdr_size > 66304) sphdr_size = 66304;
UInt_t *fullheader = new UInt_t[sphdr_size/sizeof(UInt_t)];
if (fullheader == 0) { std::cout << "failed to allocate memory for header " << std::endl; return -1; }
retVal = fread ( fullheader+2, sizeof(UInt_t), sphdr_size/sizeof(UInt_t) -2, m_filesDat.back());
fullheader[0] = sphdr_id;
fullheader[1] = sphdr_size;
if (!m_bDevID)
{
// todo read in header via structure
// for now just use fixed offset to find deviceID
UInt_t waferno = (fullheader[132] >> 8) & 0xFFF;
UInt_t id_y = (fullheader[132] >> 4) & 0xF;
UInt_t id_x = (fullheader[132] >> 0) & 0xF;
Char_t devid[16];
sprintf(devid,"W%04d_%c%02d", waferno, (Char_t)id_x+64, id_y); // make readable device identifier
deviceID->SetTitle(devid);
deviceID->Write(); // write deviceID to root file
m_bDevID = kTRUE;
}
return 0;
}
// find clusters recursively
void DataProcess::findCluster(ULong64_t index, ULong64_t stop, deque<UInt_t >* cols, deque<UInt_t >* rows, deque<ULong64_t >* toas, deque<UInt_t >* tots, deque<Bool_t >* centered, deque<ULong64_t >* indices)
{
Int_t colInd, rowInd;
Int_t colK, rowK;
UInt_t totK, totInd;
ULong64_t toaInd, toaK, toa, toaDiff;
UInt_t gap = 1 + m_gapPix;
colInd = static_cast<Int_t>(cols->at(index));
rowInd = static_cast<Int_t>(rows->at(index));
toaInd = toas->at(index);
totInd = tots->at(index);
toa = 81920;
for (ULong64_t k = 0; k < stop; k++)
{
colK = static_cast<Int_t>(cols->at(k));
rowK = static_cast<Int_t>(rows->at(k));
toaK = toas->at(k);
totK = tots->at(k);
if (toaK > toaInd)
{
toaDiff = toaK - toaInd;
}
else
{
toaDiff = toaInd - toaK;
}
if (!(centered->at(k)) && ((static_cast<UInt_t>(std::abs(colInd - colK)) <= gap) && (static_cast<UInt_t>(std::abs(rowInd - rowK)) <= gap )) && toaDiff < toa)
{
centered->at(k) = kTRUE;
indices->push_back(k);
findCluster(k,stop,cols,rows,toas,tots,centered,indices);
}
}
}
Int_t DataProcess::processDat()
{
//
// define all locally needed variables
ULong64_t headdata = 0;
ULong64_t pixdata;
ULong64_t longtime = 0;
ULong64_t longtime_lsb = 0;
ULong64_t longtime_msb = 0;
ULong64_t globaltime = 0;
Int_t pixelbits;
Int_t longtimebits;
Int_t diff;
UInt_t data;
UInt_t ToA_coarse;
UInt_t FToA;
UInt_t dcol, col;
UInt_t spix, row;
UInt_t ToT, ToA;
UInt_t pix;
UInt_t spidr_time; // additional timestamp added by SPIDR
UInt_t header;
UInt_t subheader;
UInt_t trigtime_coarse;
UInt_t prev_trigtime_coarse = 0 ;
UInt_t trigtime_fine;
ULong64_t trigtime_global_ext = 0;
ULong64_t tmpfine;
Int_t retVal;
//
// deque used for sorting and writing data
deque<UInt_t> Rows;
deque<UInt_t> Cols;
deque<UInt_t> ToTs;
deque<ULong64_t> ToAs;
ULong64_t sortSize = MAXHITS;
ULong64_t sortThreshold = static_cast<ULong64_t>(2*sortSize);
//
// used for centroiding
deque<Bool_t> Centered;
deque<ULong64_t> centeredIndices;
ULong64_t centeredIndex;
ULong64_t tmpToA;
Float_t tmpToT;
UInt_t posX, posY;
Bool_t indexFound;
UInt_t backjumpcnt = 0;
ULong64_t curInput = 0;
ULong64_t treeEntry = 0;
ULong64_t chainCounter = 0;
m_nCents.push_back(0);
m_nTimes.push_back(0);
//
// skip main header and obtain device ID
if (m_type == dtDat)
skipHeader();
//
// Main loop over all entries
// nentries is either infinite or user defined
while (!feof(m_filesDat.back()) && (m_pixelCounter < m_maxEntries || m_maxEntries == 0))
{
if (treeEntry >= m_treeMaxEntries)
{
openRoot(++chainCounter);
treeEntry = 0;
}
//
// read entries, write out each 10^5
retVal = fread( &pixdata, sizeof(ULong64_t), 1, m_filesDat.back());
if (m_pixelCounter % 100000 == 0) std::cout << "File "<< curInput << "/" << m_numInputs <<" Count " << m_pixelCounter << std::endl;
//
// reading data and saving them to deques or timeTrees
if (retVal == 1)
{
header = ((pixdata & 0xF000000000000000) >> 60) & 0xF;
//
// finding header type (data part - frames/data driven)
if (header == 0xA || header == 0xB)
{
treeEntry++;
m_pixelCounter++;
//
// calculate col and row
dcol = ((pixdata & 0x0FE0000000000000) >> 52); //(16+28+9-1)
spix = ((pixdata & 0x001F800000000000) >> 45); //(16+28+3-2)
pix = ((pixdata & 0x0000700000000000) >> 44); //(16+28)
col = (dcol + pix/4);
row = (spix + (pix & 0x3));
//
// calculate ToA
data = ((pixdata & 0x00000FFFFFFF0000) >> 16);
spidr_time = (pixdata & 0x000000000000FFFF);
ToA = ((data & 0x0FFFC000) >> 14 );
ToA_coarse = (spidr_time << 14) | ToA;
FToA = (data & 0xF);
globaltime = (longtime & 0xFFFFC0000000) | (ToA_coarse & 0x3FFFFFFF);
//
// poor ol' ToT is all alone :(
ToT = (data & 0x00003FF0) >> 4;
//
// calculate the global time
pixelbits = ( ToA_coarse >> 28 ) & 0x3; // units 25 ns
longtimebits = ( longtime >> 28 ) & 0x3; // units 25 ns;
diff = longtimebits - pixelbits;
if( diff == 1 || diff == -3) globaltime = ( (longtime - 0x10000000) & 0xFFFFC0000000) | (ToA_coarse & 0x3FFFFFFF);
if( diff == -1 || diff == 3 ) globaltime = ( (longtime + 0x10000000) & 0xFFFFC0000000) | (ToA_coarse & 0x3FFFFFFF);
if ( ( ( globaltime >> 28 ) & 0x3 ) != ( (ULong64_t) pixelbits ) )
{
std::cout << "Error, checking bits should match .. " << std::endl;
std::cout << hex << globaltime << " " << ToA_coarse << " " << dec << ( ( globaltime >> 28 ) & 0x3 ) << " " << pixelbits << " " << diff << std::endl;
}
//
// fill deques
Centered.push_back(kFALSE);
Cols.push_back(col);
Rows.push_back(row);
ToTs.push_back(ToT*25); // save in ns
// subtract fast ToA (FToA count until the first clock edge, so less counts means later arrival of the hit)
ToAs.push_back((globaltime << 12) - (FToA << 8));
// now correct for the column to column phase shift (todo: check header for number of clock phases)
ToAs.back() += ( ( (col/2) %16 ) << 8 );
if (((col/2)%16) == 0) ToAs.back() += ( 16 << 8 );
}
//
// finding header type (time part - trigger/global time)
// packets with time information, old and new format: 0x4F and 0x6F
if ( header == 0x4 || header == 0x6 )
{
subheader = ((pixdata & 0x0F00000000000000) >> 56) & 0xF;
//
// finding subheader type (F for trigger or 4,5 for time)
if ( subheader == 0xF ) // trigger information
{
m_trigCnt = ((pixdata & 0x00FFF00000000000) >> 44) & 0xFFF;
trigtime_coarse = ((pixdata & 0x00000FFFFFFFF000) >> 12) & 0xFFFFFFFF;
tmpfine = (pixdata >> 5 ) & 0xF; // phases of 320 MHz clock in bits 5 to 8
tmpfine = ((tmpfine-1) << 9) / 12;
trigtime_fine = (pixdata & 0x0000000000000E00) | (tmpfine & 0x00000000000001FF);
//
// check if the first trigger number is 1
if (! m_bFirstTrig && m_trigCnt != 1)
{
std::cout << "first trigger number in file is not 1" << std::endl;
} else
{
if ( trigtime_coarse < prev_trigtime_coarse ) // 32 time counter wrapped
{
if ( trigtime_coarse < prev_trigtime_coarse-1000 )
{
trigtime_global_ext += 0x100000000;
std::cout << "coarse trigger time counter wrapped: " << m_trigCnt << " " << trigtime_coarse << " " << prev_trigtime_coarse << std::endl;
}
else std::cout << "small backward time jump in trigger packet " << m_trigCnt << " (jump of " << prev_trigtime_coarse-trigtime_coarse << ")" << std::endl;
}
m_bFirstTrig = kTRUE;
m_trigTime = ((trigtime_global_ext + (ULong64_t) trigtime_coarse) << 12) | (ULong64_t) trigtime_fine; // save in ns
//
// fill timeTree and trigger histogram
m_timeTree.back()->Fill();
m_histTrigger->Fill(m_trigTime);
prev_trigtime_coarse = trigtime_coarse;
}
} else if ( subheader == 0x4 ) // 32 lsb of timestamp
{
longtime_lsb = (pixdata & 0x0000FFFFFFFF0000) >> 16;
} else if ( subheader == 0x5 ) // 32 lsb of timestamp
{
longtime_msb = (pixdata & 0x00000000FFFF0000) << 16;
ULong64_t tmplongtime = longtime_msb | longtime_lsb;
//
// now check for large forward jumps in time;
// 0x10000000 corresponds to about 6 seconds
if ( (tmplongtime > ( longtime + 0x10000000)) && (longtime > 0) )
{
std::cout << "Large forward time jump" << std::endl;
longtime = (longtime_msb - 0x10000000) | longtime_lsb;
}
else longtime = tmplongtime;
}
}
}
else if (++curInput < m_numInputs)
{
m_nCent = m_rawTree.back()->GetEntries();
m_nTime = m_timeTree.back()->GetEntries();
finishMsg(m_fileNameDat[curInput-1], "processing data", m_pixelCounter, curInput);
openDat(curInput);
if (m_type == dtDat)
skipHeader();
continue;
}
//
// sorting the data, wait for either finishing the file or reaching sorting count
// list contain 2*sortSize elements
if ( (m_pixelCounter >= sortThreshold) || (retVal <= 0) )
{
//
// processing either sort size or hitlist size
ULong64_t actSortedSize;
if (retVal <= 0) actSortedSize = Cols.size();
else actSortedSize = sortSize;
//
// insert sort the doublechunk
for (ULong64_t i = 0; i < Cols.size() ; i++)
{
ULong64_t j = i;
while (j > 0 && ToAs[j-1] > ToAs[j])//(ToAs[j-1] & 0xFFFF) > (ToAs[j] & 0xFFFF))
{
swap(Cols[j-1],Cols[j]);
swap(Rows[j-1],Rows[j]);
swap(ToTs[j-1],ToTs[j]);
swap(ToAs[j-1],ToAs[j]);
j--;
}
}
//
// saving half of the sorted data to root file
// first do centroiding
for (ULong64_t i = 0; i < actSortedSize ; i++)
{
indexFound = kFALSE;
posX = Cols.front();
posY = Rows.front();
ToT = ToTs.front();
if (m_bCentroid)
{
// get all hits in one time window
ULong64_t j = 0;
while (!Centered.front() && j < Cols.size())
{
// reaching the window gap
if ((ToAs[j] - ToAs.front()) > m_gapTime)