-
Notifications
You must be signed in to change notification settings - Fork 8
/
RAW2ROOT.cc
1122 lines (877 loc) · 34.9 KB
/
RAW2ROOT.cc
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
#ifndef RAW2ROOT_CC
#define RAW2ROOT_CC
#include <iomanip>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <bitset>
#include <math.h>
#include <vector>
#include <string>
#include <algorithm>
#include <TH1F.h>
#include <TH2F.h>
#include <TF1.h>
#include <TTree.h>
#include <TFile.h>
#include <TCanvas.h>
#include <TString.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "InfoChip.cc"
// .x RAW2ROOT.C+
using std::cout;
using std::endl;
class RAW2ROOT {
public:
RAW2ROOT(){
recordEvent = false;
_savelog = true;
_debug = false;
if(_debug==true) _savelog=true;
chipIds.push_back(0x0000);//chip 2
chipIds.push_back(0x0001);//chip 4
chipIds.push_back(0x0002);//chip 3
chipIds.push_back(0x0003);//chip 1
chipIds.push_back(0x0004);//chip
chipIds.push_back(0x0005);//chip
chipIds.push_back(0x0006);//chip
chipIds.push_back(0x0007);//chip
chipIds.push_back(0x0008);//chip
chipIds.push_back(0x0009);//chip
chipIds.push_back(0x000A);//chip
chipIds.push_back(0x000B);//chip
chipIds.push_back(0x000C);//chip
chipIds.push_back(0x000D);//chip
chipIds.push_back(0x000E);//chip
chipIds.push_back(0x000F);//chip
info = new InfoChip(); R2Rstate=-1;
};
~RAW2ROOT(){
delete info;
};
void ReadFile(TString inputFileName, bool overwrite=false, TString outFileName = "default", int bcidthres=15, int maxevt=99999999);
protected:
enum {
MEMDEPTH=15,
NCHANNELS=64,
NCHIP=16,
CHIPHEAD=4,
CHIPENDTAG=2,
NEGDATA_THR=11 //event with data below are tagged badbcid+=32
};
int R2Rstate;
int readEvent(std::vector < unsigned short int > & eventData, int bcidthres);
int data_integrity(std::vector < unsigned short int > & eventData, int i, int local_offset, int nColumns, int ichip);
void Initialisation();
void analyse_hits();
void analyse_dataintegrity();
void plotHistos();
void treeInit();
void printEvent(std::vector < unsigned short int > & eventData);
void DebugMode(bool t){ _savelog=t; }
void searchNmax(int N, int Size, Float_t * table, Float_t * tableout ,int * ranks);
int GetTree(TString rootfilename);
int BuildTimedEvents(TString rootfilename);
TH1F* h_hit_high[NCHIP]; //hist of all hits per chip per channel
TH1F* h_hit_filtered[NCHIP];//hist of good hits per chip per channel
TH1F* h_hit_filtered_histo[NCHIP];//hist of nb of good hits per chip
TH1F* h_nCol[NCHIP];
TH1F* h_bcid[NCHIP];
TH1D* h_TagHist[NCHIP];
TH2D* TagHist;
TH2I* h_chnnb;
TH2I* h_chipnb;
TH2F* h2_hits;
TH2F* h2_hits_Acq;
TH1F* h_hits_BCID[NCHIP][NCHANNELS];
TH1F* h_col_Acq[NCHIP];
// data integrity mistakes counter
TH1F* h_dataIntegrity;
TH1F* h_dataIntegrity_bcid;
TH1F* h_dataIntegrity_sca;
TH2F* h_dataIntegrity_map;
//Data Integrity
// value = 0 --> OK
// value = 1 --> bad data size
// value = 2 --> more than 15 memory columns
// value = 3 --> bad chip number
// value = 4 --> extra bits in BCID (>12)
// value = 5 --> extra bits in LOW GAIN charge/hbits --> expected 13 bits, no more. The 14th is for autogain mode --> not used
// value = 6 --> extra bits in HIGH GAIN charge/hbits --> expected 13 bits, no more. The 14th is for autogain mode --> not used
// value = 7 --> hit bit from high gain != hit bit from low gain
// value = 8 --> gain = 1 but negative data
// value = 9 --> Bad number of columns or bad number of channels
TDirectory *dPlots ;
TFile* fout;
TTree* tree;
TFile *finroot;
TTree* fev10read;
int bcid[NCHIP][MEMDEPTH];
int corrected_bcid[NCHIP][MEMDEPTH];
int badbcid[NCHIP][MEMDEPTH];
int nhits[NCHIP][MEMDEPTH];
int charge_low[NCHIP][MEMDEPTH][NCHANNELS];
int charge_high[NCHIP][MEMDEPTH][NCHANNELS];
int gain_hit_low[NCHIP][MEMDEPTH][NCHANNELS];
int gain_hit_high[NCHIP][MEMDEPTH][NCHANNELS];
int event;
int thr;
//int numbcid;
int numCol[NCHIP];
int chipID[NCHIP];
int acqNumber;
bool recordEvent;
bool _savelog;
bool _debug;
std::vector <int> chipIds;
ofstream out_log; //file where the log info will be
InfoChip * info;
};
//******************************************************************************************************************
void RAW2ROOT::Initialisation() {
fout->cd(); R2Rstate=-1;
tree = new TTree("fev10","fev10");
tree->Branch("event",&event,"event/I");
tree->Branch("thr",&thr,"thr/I");
tree->Branch("acqNumber",&acqNumber,"acqNumber/I");
TString name;
name = "chipid[";
name+=NCHIP; name+="]/I";
tree->Branch("chipid",chipID,name);
name = "nColumns[";
name+=NCHIP; name+="]/I";
tree->Branch("nColumns",numCol,name);
name = "bcid[";
name+=NCHIP; name+="][";name+=MEMDEPTH; name+="]/I";
tree->Branch("bcid",bcid,name);
name = "corrected_bcid[";
name+=NCHIP; name+="][";name+=MEMDEPTH; name+="]/I";
tree->Branch("corrected_bcid",corrected_bcid,name);
name = "badbcid[";
name+=NCHIP; name+="][";name+=MEMDEPTH; name+="]/I";
tree->Branch("badbcid",badbcid,name);
name = "nhits[";
name+=NCHIP; name+="][";name+=MEMDEPTH; name+="]/I";
tree->Branch("nhits",nhits,name);
name = "lowGain[";
name+=NCHIP; name+="][";name+=MEMDEPTH; name+="]["; name+=NCHANNELS; name+="]/I";
tree->Branch("charge_lowGain",charge_low,name);
name = "highGain[";
name+=NCHIP; name+="][";name+=MEMDEPTH; name+="]["; name+=NCHANNELS; name+="]/I";
tree->Branch("charge_hiGain",charge_high,name);
name = "gain_hit_low[";
name+=NCHIP; name+="][";name+=MEMDEPTH; name+="]["; name+=NCHANNELS; name+="]/I";
tree->Branch("gain_hit_low",gain_hit_low,name);
name = "gain_hit_high[";
name+=NCHIP; name+="][";name+=MEMDEPTH; name+="]["; name+=NCHANNELS; name+="]/I";
tree->Branch("gain_hit_high",gain_hit_high,name);
h2_hits = new TH2F("Hits_XY","Hits",32,-89.5,90-0.5,32,-89.5,90-0.5);
h2_hits_Acq = new TH2F("Hits_XY_Acq","Hits per Acq",32,-89.5,90-0.5,32,-89.5,90-0.5);
h_dataIntegrity = new TH1F("Data_Integrity","Data Integrity",10,-0.5,9.5);
h_dataIntegrity_bcid = new TH1F("Data_Integrity_BCID","Data Integrity_BCID",4096,0.5,4096.5);
h_dataIntegrity_sca = new TH1F("Data_Integrity_SCA","Data Integrity_SCA",15,-0.5,14.5);
h_dataIntegrity_map = new TH2F("Data_Integrity_map","Data Integrity_map",64,-0.5,63.5,16,-0.5,15.5);
for (int j=0; j<NCHIP; j++) {
name = "hits_chip_";
name+=j+1;
h_hit_high[j] = new TH1F(name,name,NCHANNELS,0,NCHANNELS);
name = "hits_filtered_chip_";
name+=j+1;
h_hit_filtered[j] = new TH1F(name,name,NCHANNELS,0,NCHANNELS);
name = "nCol_chip_";
name+=j+1;
h_nCol[j] = new TH1F(name,name,MEMDEPTH+1,0,MEMDEPTH+1);
name = "bcid_chip_";
name+=j+1;
h_bcid[j] = new TH1F(name,name,4096,0,4096);
name = "col_chip_Acq";
name+=j+1;
h_col_Acq[j] = new TH1F(name,name,4,1,5);
}
dPlots->cd();
for (int j=0; j<NCHIP; j++) {
for (int i=0; i<NCHANNELS; i++) {
name = "chargeHigh_chip_";
name+=j+1;
name+="_channel_";
name+=i;
name+="_withHit";
name = "chargeLow_chip_";
name+=j+1;
name+="_channel_";
name+=i;
name+="_withHit";
name = "hits_chip_";
name+=j+1;
name+="_channel_";
name+=i;
name+="_fct_BCID";
h_hits_BCID[j][i] = new TH1F(name,name,4096,0,4096);
}
name = "TagHist_";
name+=j+1;
h_TagHist[j] = new TH1D(name, name,16,-0.5,15.5);
}
// map of channel number
fout->cd();
h_chnnb = new TH2I("Map_chn_nb","Map_chn_nb",32,-89.5,90-0.5,32,-89.5,90-0.5);
h_chipnb = new TH2I("Map_chip_nb","Map_chip_nb",32,-89.5,90-0.5,32,-89.5,90-0.5);
for(int chip = 0;chip<NCHIP;chip++){
for(int chn = 0;chn<NCHANNELS;chn++){
h_chnnb->Fill(info->GetX(chip,chn),info->GetY(chip,chn),chn);
h_chipnb->Fill(info->GetX(chip,chn),info->GetY(chip,chn),chip);
}
}
// map of channel number
fout->cd();
if(_savelog) out_log<<"End Initialisation"<<endl;
return;
}
//******************************************************************************************************************
void RAW2ROOT::treeInit() { //init data for a single SPILL ?
for (int k=0; k<NCHIP; k++) {
for (int i=0; i<MEMDEPTH; i++) {
//allbcid[i+k*MEMDEPTH]=0;
bcid[k][i]=-999;
badbcid[k][i]=-999;
corrected_bcid[k][i]=-999;
nhits[k][i]=-999;
for (int j=0; j<NCHANNELS; j++) {
charge_low[k][i][j]=-999;
charge_high[k][i][j]=-999;
gain_hit_low[k][i][j]=-999;
gain_hit_high[k][i][j]=-999;
}
}
chipID[k]=-999;
numCol[k]=-999;
}
recordEvent = true;
return;
}
//******************************************************************************************************************
void RAW2ROOT::searchNmax(int N, int Size, Float_t * table, Float_t * tableout , int * ranks) {
//assumes positive numbers, tableout and ranks initialized to 0
int i=0;
for (int dat=1;dat<Size+1;dat++){
i=-1;
while (i<N-1) {
if (table[dat]>tableout[i+1]) {i++;} else {break;}
}
if (i>0) {
for (int j=0;j<i;j++){tableout[j]=tableout[j+1]; ranks[j]=ranks[j+1];}
tableout[i]=table[dat]; ranks[i]=dat;
} else if (i==0) {tableout[i]=table[dat]; ranks[i]=dat;}
}
return;}
void RAW2ROOT::analyse_dataintegrity() {
out_log<<" "<<endl;
out_log<< " DATA INTEGRITY SUMMARY "<<endl;
out_log<< " total number of spills = "<< h_dataIntegrity->GetEntries() << endl;
out_log<< " TOTALGOOD "<< 100.*h_dataIntegrity->GetBinContent(1)/h_dataIntegrity->GetEntries() << " % are spills with acceptable data"<<endl;
out_log<< " bad -- " << 100.*h_dataIntegrity->GetBinContent(2)/h_dataIntegrity->GetEntries() << " % have bad data size"<<endl;
out_log<< " bad -- " << 100.*h_dataIntegrity->GetBinContent(3)/h_dataIntegrity->GetEntries() << " % have more than 15 SCA"<<endl;
out_log<< " bad -- " << 100.*h_dataIntegrity->GetBinContent(4)/h_dataIntegrity->GetEntries() << " % have bad chip number"<<endl;
out_log<< " bad -- " << 100.*h_dataIntegrity->GetBinContent(5)/h_dataIntegrity->GetEntries() << " % have extra bits in BCID"<<endl;
out_log<< " bad -- " << 100.*h_dataIntegrity->GetBinContent(6)/h_dataIntegrity->GetEntries() << " % have extrabits in low gain."<<endl;
out_log<< " bad -- " << 100.*h_dataIntegrity->GetBinContent(7)/h_dataIntegrity->GetEntries() << " % have extrabits in high gain"<<endl;
out_log<< " bad -- " << 100.*h_dataIntegrity->GetBinContent(8)/h_dataIntegrity->GetEntries() << " % have different hit bit for low and high gain"<<endl;
// out_log<< " bad -- " << 100.*h_dataIntegrity->GetBinContent(9)/h_dataIntegrity->GetEntries() << " % gain = 1 but charge <10"<<endl;
out_log<< " bad -- " << 100.*h_dataIntegrity->GetBinContent(9)/h_dataIntegrity->GetEntries() << " % bad number of SCA or channels"<<endl;
}
void RAW2ROOT::analyse_hits() {
// for each chip one look to the mean ratio of (hits)/(total events) and (good hits)/(hits)
// good hits are identified according to algo in readEvent function
// total events is the cumulated sum of columns over spills
TString name;
if(_savelog) out_log << "ANALYSE HITS"<<endl;
//one get total events using sum(bin x bin value) from the histo of the number of columns
Double_t TotEvents[NCHIP]; //Double_t
Double_t UnFilteredEvents[NCHIP]; //Double_t
for (int nchip=0; nchip<NCHIP; nchip++) {
TotEvents[nchip]=0;
UnFilteredEvents[nchip]=0;
for (int nbin=2;nbin<17;nbin++) {
TotEvents[nchip]=TotEvents[nchip]+ (nbin-1)*h_nCol[nchip]->GetBinContent(nbin) ; //ncol=nbin-1
}
UnFilteredEvents[nchip] = TotEvents[nchip] - h_TagHist[nchip]->GetEntries();
if(_savelog) out_log << " get " << UnFilteredEvents[nchip] << " out of " << TotEvents[nchip] << " events for chip " << nchip ;
if(_savelog) out_log << " (" << UnFilteredEvents[nchip]*100/TotEvents[nchip] << "%)"<<endl;
}
//map of Tagged events, % of tagged evt per chip per column
name = "TagHist";
TagHist = new TH2D(name, name, NCHIP,-0.5,NCHIP-0.5,15,0.5,15.5);
for (int nchip=0; nchip<NCHIP; nchip++) {
Double_t *bins = h_TagHist[nchip]->GetArray();
for (int col=1; col<16; col++) {//bins start at index 1.
TagHist->SetBinContent(nchip+1,col, bins[col]*100/ TotEvents[nchip] );
}
}
//map of hit per chip per channel, normalized to the total number of event
name = "HitMapHist";
TH2D* HitMapHist = new TH2D(name, name, 16,-0.5,15.5,64,-0.5,63.5);
name = "HitMapFilteredHist";
TH2D* HitMapFilteredHist = new TH2D(name, name, 16,-0.5,15.5,64,-0.5,63.5);
for (int nchip=0; nchip<NCHIP; nchip++) {
Float_t *bins = h_hit_high[nchip]->GetArray();
Float_t *binsfiltered = h_hit_filtered[nchip]->GetArray();
for (int chn=0; chn<NCHANNELS; chn++) {//bins start at index 1.
HitMapHist->SetBinContent(nchip+1,chn+1, bins[chn+1]);
HitMapFilteredHist->SetBinContent(nchip+1,chn+1, binsfiltered[chn+1]);
}
}
if(_savelog) out_log<<" ------------------------------- "<<endl;
return;
}
//******************************************************************************************************************
void RAW2ROOT::plotHistos() {
h2_hits_Acq->Scale(1./event);
h_col_Acq[0]->Scale(1./event);
h_col_Acq[1]->Scale(1./event);
h_col_Acq[2]->Scale(1./event);
h_col_Acq[3]->Scale(1./event);
fout->cd();
fout->Write(0);
fout->Close();
return;
}
//******************************************************************************************************************
void RAW2ROOT::printEvent(std::vector < unsigned short int > & eventData) {
if (_debug) {
out_log << "printing event for debug" << endl;
for (unsigned int i=0; i<eventData.size(); i++) {
out_log << i << " 0x" << hex << eventData[i] << " "<< dec <<int(eventData[i] & 0x0fff) << endl;
}
}
return;
}
//******************************************************************************************************************
int RAW2ROOT::readEvent(std::vector < unsigned short int > & eventData, int bcidthres) {
// ASSUMES HIGH/LOW GAIN mode !!!
// ----------------------
//Data Integrity
// value = 0 --> OK
// value = 1 --> bad data size
// value = 2 --> more than 15 memory columns
// value = 3 --> bad chip number
// value = 4 --> extra bits in BCID (>12)
// value = 5 --> extra bits in LOW GAIN charge/hbits --> expected 13 bits, no more. The 14th is for autogain mode --> not used
// value = 6 --> extra bits in HIGH GAIN charge/hbits --> expected 13 bits, no more. The 14th is for autogain mode --> not used
// value = 7 --> hit bit from high gain != hit bit from low gain
// value = 8 --> gain = 1 but negative data
// value = 9 --> Bad number of columns or bad number of channels
// check we have a decent amount of data
printEvent(eventData);
unsigned short int last=0;
//unsigned int previousChipEndIndex = 0;
unsigned int chipStartIndex = 0;
int rawDataSize=0;
int nColumns = 0;
int local_offset = 0; //adapt to redundant chip ID word, value can be 0 or 1
int previousBCID = -999;
bool isValidChip = false;
for(unsigned int i=0;i<eventData.size();i++){
if( eventData[i] == 0xfffd){// find start chip tag
chipStartIndex=i+CHIPHEAD;
if (_debug) out_log << " start chip";
}
if(last == 0xfffe){// find end chip tag
if( (eventData[i] > 0xff00) & (eventData[i] < 0xffff)){// chips was fffc
isValidChip=true;
}
if(isValidChip){
const int offset=1; //was 2
rawDataSize = i-chipStartIndex-CHIPENDTAG;
local_offset = (rawDataSize-offset)%(1+NCHANNELS*2);
if(local_offset!=0){
if (_savelog) out_log<<"<!> WARNING <!> Additionnal data words detected"<<endl;
// last=eventData[i];
}
nColumns = (rawDataSize-offset-local_offset)/(1+NCHANNELS*2);
if (_debug) out_log<<"Chip data: " <<" size: "<<rawDataSize<<" col: "<<nColumns<<" evt: "<<(rawDataSize-offset)%(1+NCHANNELS*2)
<<" local_offset :"<< local_offset << endl;
if((rawDataSize-offset-local_offset)%(1+NCHANNELS*2)!=0){
if(_savelog) out_log<<"<!> ERROR <!> Bad data size"<<endl;
last=eventData[i];
return 1;
}
if (nColumns>MEMDEPTH){
if(_savelog) out_log << "<!> ERROR <!> Bad number of columns" <<endl;
last=eventData[i];
return 2;
}
if(i<2) return 9;
//test of the chip id
int chipid = eventData[i-2];//was 3
//irles
bool isGoodChipNumber=false;
for(unsigned int iChip = 0 ; iChip<chipIds.size();iChip++){
if(chipid==chipIds[iChip]) isGoodChipNumber=true;
}
if(!isGoodChipNumber){
if(_savelog) out_log << "<!> ERROR <!> Bad Chip ID: " << chipid <<endl;
last=eventData[i];
return 3;
}
//data integrity
int data_integ=data_integrity(eventData,i,local_offset,nColumns,chipid);
if(data_integ>0) return data_integ;
// ----------------------
//Fill variables of the tree only if the data is okay
int c = info->GetASUChipNumberFromChipID(chipid);
chipID[chipid]=chipid;
numCol[chipid]= nColumns;
previousBCID = -999;
int loopBCID = 0;
// now loop over the data, fill the charge values
for (int ibc=0; ibc<nColumns; ibc++) {
//fill BCID
bcid[chipid][ibc]=eventData[i-3-1*local_offset-ibc] & 0x0fff ; // !!! index to be verified !!!
if(bcid[chipid][ibc]-previousBCID < 0) loopBCID++;
corrected_bcid[chipid][ibc] = bcid[chipid][ibc]+loopBCID*4096;
h_bcid[c]->Fill(bcid[chipid][ibc]);
// fill the charges
int ichan(0);
// range for this column
int begin = i-3 - 1*local_offset - nColumns - ibc*NCHANNELS*2; // !!! index to be verified !!!
int end = begin - NCHANNELS;
for (int jj = begin; jj>end; jj--) {
if (ibc<MEMDEPTH && ichan<NCHANNELS){
charge_low[chipid][ibc][ichan]=eventData[jj] & 0x0fff;
gain_hit_low[chipid][ibc][ichan]= (eventData[jj] >> 12 ) & 0xf;
}
else {
if (_savelog) out_log << "<!> ERROR <!> Low Gain : Bad number of columns: " << ibc << " or bad number of channels: " << ichan << endl;
}
ichan++;
}
// analyse hits and bcids
begin=end;
end=begin - NCHANNELS;
ichan=0;
int count_hits = 0;
for (int jj = begin; jj>end; jj--) {
if (ibc<MEMDEPTH && ichan<NCHANNELS){
charge_high[chipid][ibc][ichan]=eventData[jj] & 0x0fff;
gain_hit_high[chipid][ibc][ichan] = (eventData[jj] >> 12 ) & 0xf;
if(gain_hit_high[chipid][ibc][ichan]==1 && charge_high[chipid][ibc][ichan]>0 ){
count_hits++;
}
} else {
if (_savelog) out_log << "<!> ERROR <!> High Gain : Bad number of columns: " << ibc << " or bad number of channels: " << ichan << endl;
}
ichan++;
}
nhits[chipid][ibc]=count_hits;
previousBCID = bcid[chipid][ibc];
}
isValidChip=false;
//print event info
if(_debug) {
for(int ibc=0; ibc<nColumns; ibc++)
out_log<< "bcid = "<<bcid[chipid][ibc]<<endl;
for(int ibc=0; ibc<nColumns; ibc++) {
for(int ich=0; ich<NCHANNELS; ich++) {
out_log<< chipid <<" "<< ibc <<" "<< ich << " low = "<<charge_high[chipid][ibc][ich]<< " high = "<<charge_low[chipid][ibc][ich]<<endl;
}
}
} //
}
}
// ----------------------
last=eventData[i];
}
//add tags
int count_negdata=0;
for (int k=0; k<NCHIP; k++) {
//only for valid chips in this spill
if (chipID[k]>=0) {
for (int ibc=0; ibc<numCol[k]; ibc++) {
// if sca+1 is filled with consec bcid, but sca+2 not, then badbcid[sca]==1 && badbcid[sca+1]==2 (bcid+1 issue, events are not bad, just the next sca is bad)
// if sca+1 is filled with consec bcid, and sca+2 also, then badbcid[sca]==3 && badbcid[sca+1]==3 (retriggering)
// if sca+1 is not filled with consec bcid, badbcid==0
if(ibc==0) {
badbcid[k][ibc]=0;
int corri=corrected_bcid[k][ibc];
if(corrected_bcid[k][ibc+1]>0 && corrected_bcid[k][ibc]>0 && (corrected_bcid[k][ibc+1]-corrected_bcid[k][ibc])>0) {
int corri1=corrected_bcid[k][ibc+1];
if(corrected_bcid[k][ibc+2]>0 && (corrected_bcid[k][ibc+2]-corrected_bcid[k][ibc+1])>0) {
int corri2=corrected_bcid[k][ibc+2];
if( ( corri2-corri1) < bcidthres && (corri1-corri) < bcidthres) {
badbcid[k][ibc]=3;
badbcid[k][ibc+1]=3;
badbcid[k][ibc+2]=3;
}
if( ( corri2-corri1) >(bcidthres - 1) && (corri1-corri) > 1 && (corri1-corri) <bcidthres) {
badbcid[k][ibc]=3;
badbcid[k][ibc+1]=3;
}
if( ( corri2-corri1) >(bcidthres - 1) && (corri1-corri) ==1) {
badbcid[k][ibc]=1;
badbcid[k][ibc+1]=2;
}
} else {
if( (corri1-corri) < bcidthres) {
badbcid[k][ibc]=3;
badbcid[k][ibc+1]=3;
}
} //ibc+2 if
}//ibc+1 if
} //ibc==0 if
if(ibc>0 && badbcid[k][ibc]<0 && corrected_bcid[k][ibc] >0 && (corrected_bcid[k][ibc]-corrected_bcid[k][ibc-1])>0 ) {
badbcid[k][ibc]=0;
int corri=corrected_bcid[k][ibc];
int corriminus=corrected_bcid[k][ibc-1];
if(corrected_bcid[k][ibc+1]>0 && (corrected_bcid[k][ibc+1]-corrected_bcid[k][ibc])>0) {
int corri1=corrected_bcid[k][ibc+1];
if(corrected_bcid[k][ibc+2]>0 && (corrected_bcid[k][ibc+2]-corrected_bcid[k][ibc+1])>0) {
int corri2=corrected_bcid[k][ibc+2];
if( ( corri2-corri1) < bcidthres && (corri1-corri) < bcidthres) {
badbcid[k][ibc]=3;
badbcid[k][ibc+1]=3;
badbcid[k][ibc+2]=3;
}
if( (corri1-corri) < bcidthres && (corri-corriminus) < bcidthres ) badbcid[k][ibc]=3;
if( badbcid[k][ibc]!=3 && ( corri2-corri1) >(bcidthres - 1) && (corri1-corri) ==1) {
badbcid[k][ibc]=1;
badbcid[k][ibc+1]=2;
}
if( badbcid[k][ibc]!=3 && ( corri2-corri1) >(bcidthres - 1) && (corri1-corri) > 1 && (corri1-corri) <bcidthres) {
badbcid[k][ibc]=3;
badbcid[k][ibc+1]=3;
}
if( (corri-corriminus) < bcidthres ) badbcid[k][ibc]=3;
//if( badbcid[k][ibc-1]==1 && (corri1-corri) > (bcidthres - 1)) badbcid[k][ibc]=2;
} else {
if( (corri1-corri) < bcidthres ) badbcid[k][ibc]=3;
if( (corri-corriminus) < bcidthres ) badbcid[k][ibc]=3;
} //ibc+2 if
} else {
if( (corri-corriminus) < bcidthres ) badbcid[k][ibc]=3;
}//ibc+1 if
} //ibc>0 if
for (int ichan=0; ichan<NCHANNELS; ichan++) {
if(gain_hit_high[k][ibc][ichan]%2==1){
h_hit_filtered[k]->Fill(ichan);
}
}
//tag zero (under/over flow) data
count_negdata=0;
for (int ichan=0; ichan<NCHANNELS; ichan++) {
if (charge_high[k][ibc][ichan] < NEGDATA_THR) count_negdata++;
}//ichan
if (count_negdata>0) {badbcid[k][ibc]+=32;}
//if(k==15) out_log<<ibc<< " " <<badbcid[k][ibc]<<" "<<corrected_bcid[k][ibc]<<endl;
}//ibc
//if(k==15) out_log<<" ------------------ " <<std::endl;
}//chipID
}//k
return 0;
}
int RAW2ROOT::data_integrity(std::vector < unsigned short int > & eventData, int i, int local_offset, int nColumns, int ichip){
if (_debug) out_log << "DataIntegrity: analysing event with " << eventData.size() << " entries " << event << endl;
// ASSUMES HIGH/LOW GAIN mode !!!
// ----------------------
//Data Integrity
// value = 0 --> OK
// value = 1 --> bad data size
// value = 2 --> more than 15 memory columns
// value = 3 --> bad chip number
// value = 4 --> extra bits in BCID (>12)
// value = 5 --> extra bits in LOW GAIN charge/hbits --> expected 13 bits, no more. The 14th is for autogain mode --> not used
// value = 6 --> extra bits in HIGH GAIN charge/hbits --> expected 13 bits, no more. The 14th is for autogain mode --> not used
// value = 7 --> hit bit from high gain != hit bit from low gain
// value = 8 --> Bad number of columns or bad number of channels
int check_data=0;
//create vectors of high/low gain hit bits for comparison
int gain_high[15][64], gain_low[15][64];
for(int ibcid=0; ibcid<15; ibcid++) {
for(int ichan=0; ichan<64; ichan++) {
gain_high[ibcid][ichan]=-999;
gain_low[ibcid][ichan]=-999;
}
}
for (int ibc=0; ibc<nColumns; ibc++) {
int bcid = int(eventData[i-3-1*local_offset-ibc] & 0x0fff);
if( int(eventData[i-3-1*local_offset-ibc] & 0xf000) !=0 ) {
check_data=4; //extra bits
if (_savelog) out_log << "<!> DataIntegrity ERROR <!> Extra bits in BCID " << ibc << endl;
h_dataIntegrity_bcid->Fill(bcid);
h_dataIntegrity_sca->Fill(ibc);
//return check_data;
}
// fill the charges
int ichan(0);
// range for this column
int begin = i-3 - 1*local_offset - nColumns - ibc*NCHANNELS*2; // !!! index to be verified !!!
int end = begin - NCHANNELS;
for (int jj = begin; jj>end; jj--) {
if (ibc<MEMDEPTH && ichan<NCHANNELS){
gain_low[ibc][ichan]=(eventData[jj] >> 12 ) & 0xf;
int charge = int(eventData[jj] & 0x0fff);
if( gain_low[ibc][ichan] > 1 ) {
check_data=5; //extra bits
if (_savelog) out_log << "<!> DataIntegrity ERROR <!> Extra bits in LOW GAIN " << ibc << " "<< gain_low[ibc][ichan] << endl;
h_dataIntegrity_bcid->Fill(bcid);
h_dataIntegrity_sca->Fill(ibc);
h_dataIntegrity_map->Fill(ichan,ichip);
// return check_data;
}
} else {
check_data=8;
if (_savelog) out_log << "<!> DataIntegrity ERROR <!> Low Gain : Bad number of columns: " << ibc << " or bad number of channels: " << ichan << endl;
h_dataIntegrity_bcid->Fill(bcid);
h_dataIntegrity_sca->Fill(ibc);
h_dataIntegrity_map->Fill(ichan,ichip);
return check_data;
}
ichan++;
}
//if(check_data>0) return check_data;
// analyse hits and bcids
begin=end;
end=begin - NCHANNELS;
ichan=0;
int count_hits = 0;
for (int jj = begin; jj>end; jj--) {
if (ibc<MEMDEPTH && ichan<NCHANNELS){
gain_high[ibc][ichan] = (eventData[jj] >> 12 ) & 0xf;
int charge = int(eventData[jj] & 0x0fff);
if( gain_high[ibc][ichan] > 1 ) {
check_data=6; //extra bits
if (_savelog) out_log << "<!> DataIntegrity ERROR <!> Extra bits in HIGH GAIN " << ibc << " "<< gain_high[ibc][ichan] << endl;
h_dataIntegrity_bcid->Fill(bcid);
h_dataIntegrity_sca->Fill(ibc);
h_dataIntegrity_map->Fill(ichan,ichip);
//return check_data;
}
if( gain_high[ibc][ichan] != gain_low[ibc][ichan] ) {
check_data=7; //extra bits
if (_savelog) out_log << "<!> DataIntegrity ERROR <!> HIT HIGH GAIN != HIT LOW GAIN for ibc="<<ibc<<" ichan="<<ichan << gain_high[ibc][ichan]<<" "<< gain_low[ibc][ichan] << endl;
h_dataIntegrity_bcid->Fill(bcid);
h_dataIntegrity_sca->Fill(ibc);
h_dataIntegrity_map->Fill(ichan,ichip);
//return check_data;
}
} else {
check_data=8;
if (_savelog) out_log << "<!> DataIntegrity ERROR <!> High Gain : Bad number of columns: " << ibc << " or bad number of channels: " << ichan << endl;
h_dataIntegrity_bcid->Fill(bcid);
h_dataIntegrity_sca->Fill(ibc);
h_dataIntegrity_map->Fill(ichan,ichip);
return check_data;
}
ichan++;
}
if(check_data>0) return check_data;
}
return 0;
// ----------------------
}
//******************************************************************************************************************
void RAW2ROOT::ReadFile(TString inputFileName, bool overwrite, TString outFileName, int bcidthres, int maxevt) {
TString out_log_name = inputFileName+"_conversion.log";
if(_savelog) out_log.open(out_log_name.Data());
if(_savelog) out_log <<endl;
if(_savelog) out_log << " **** READING FILE " << inputFileName << " ****"<<endl;
// read threshold value from event number
if(inputFileName.Contains("_trig")){
// thr = std::stoi(inputFileName(inputFileName.Index("_trig")+5,3));
if(_savelog) out_log << endl << "Opened file with threshold\t" << thr << endl << endl;
}
event=0;
acqNumber=0;
if(outFileName == "default"){
outFileName = inputFileName+".root";
}
if(!overwrite){
fout = new TFile(outFileName,"create");
if(!fout->IsOpen()){
if(_savelog) out_log<<"<!> ERROR <!> File already created!"<<endl;
return;
}
}
else {
fout = new TFile(outFileName,"recreate");
}
dPlots = fout->mkdir("plots");
Initialisation();
ifstream fin;
unsigned short int dataResult=0;
//int rawDataSize=0;
//int nColumns = 0;
bool altTag = false;
int countchipdata=0;
int countchip=0;
fin.open(inputFileName.Data(), ios_base::in|ios_base::binary);
//int nline=0;
std::vector < unsigned short int > packetData;
std::vector < unsigned short int > eventData;
unsigned short int lastTwo[2]={0};
if(fin.is_open())
while (fin.read((char *)&dataResult, sizeof(dataResult) ) && event<maxevt ) {
packetData.push_back(dataResult);countchipdata++;
if(dataResult == 0xFFFE) {//END of CHIP
altTag = true;
}
if(lastTwo[1] == 0xFFFC){
if (_debug) out_log << " SPILL "<< lastTwo[0] << " " << dataResult << " START--->" <<endl;
countchip=0;
}
if(lastTwo[1] == 0xFFFD){
if (_debug) out_log << " CHIP "<< lastTwo[0] - 65280 << " Begin..."; countchipdata=0;
}
if(lastTwo[1] == 0xFFFE){
if (_debug) out_log << "...End CHIP "<< lastTwo[0] - 65280 << " with " << countchipdata-2-3 << " words" <<endl;
if (countchipdata>100) {countchip++;};
}
if(lastTwo[1] == 0xFFFF){
if (_debug) out_log << " ----->END SPILL "<< lastTwo[0] << " " << dataResult << " with " << countchip << " chips"<< endl<<acqNumber<<endl;
packetData.clear();
lastTwo[0]=lastTwo[1]=dataResult=0;
altTag = false;
}
if(lastTwo[1] == 0x2020 && lastTwo[0] == 0x2020 && dataResult == 0xFFFF){// SPILL END
//out_log<<"SPILL END= "<<packetData.size()<<endl;
if(recordEvent){
if (countchip>0){
int data_int = readEvent(packetData,bcidthres);
h_dataIntegrity->Fill(data_int);
if(_debug) out_log<<" Data Int = "<<data_int<<endl;
if(data_int==0) {
event++;
tree->Fill();
//QUICK ANALISYS HISTORGRAMS
for(int nc=0; nc<NCHIP; nc++) {
int c = info->GetASUChipNumberFromChipID(nc);
if(numCol[c]>0 ) {
h_nCol[c]->Fill(numCol[c]);
h_col_Acq[c]->Fill(numCol[c]);
}
for(int ibc=0; ibc<MEMDEPTH; ibc++) {
for( int ichan=0; ichan<NCHANNELS; ichan++){
if(gain_hit_high[nc][ibc][ichan]==1 && charge_high[nc][ibc][ichan]>NEGDATA_THR ){
h_hit_high[c]->Fill(ichan);
h2_hits->Fill(info->GetX(c,ichan),info->GetY(c,ichan) );
h2_hits_Acq->Fill(info->GetX(c,ichan),info->GetY(c,ichan) );
h_hits_BCID[c][ichan]->Fill(bcid[nc][ibc]);
}
}
}
} // end quick analisys
}// end good data block
}
}
}
if(lastTwo[1] == 0x5053 && lastTwo[0] == 0x4C49 && dataResult == 0x2020){ //New SPILL: extract SPILL number
if(altTag)
if (_savelog) out_log<<"<!> WARNING <!> New spill without end flag of the previous spill - some chips found "<<endl;
acqNumber= packetData[packetData.size()-5]*65536+packetData[packetData.size()-4];
treeInit();
packetData.clear();
lastTwo[0]=lastTwo[1]=dataResult=0;
altTag = false;
}
lastTwo[1] = lastTwo[0];
lastTwo[0] = dataResult;
}
if(_savelog) {
out_log <<endl;
out_log << " **** Finished reading file ****" << endl;
out_log << " **** with "<< tree->GetEntries()<< " entries ****" << endl;
out_log <<endl;
out_log << " FILEINTEGRITY "<< inputFileName<< " "<< h_dataIntegrity->GetBinContent(1)/h_dataIntegrity->GetEntries()<<endl;
analyse_dataintegrity();
}
analyse_hits();
plotHistos();
return;