-
Notifications
You must be signed in to change notification settings - Fork 8
/
recoCrv.cpp
1858 lines (1668 loc) · 81.5 KB
/
recoCrv.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 <cstdio>
# include <cmath>
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <fstream>
# include <sstream>
# include <string>
# include <cassert>
# include <vector>
# include <algorithm>
# include <experimental/filesystem>
# include "TCanvas.h"
# include "TROOT.h"
# include "TGraphErrors.h"
# include "TGraph.h"
# include "TH1.h"
# include "TH1F.h"
# include "TF1.h"
# include "TLegend.h"
# include "TLegendEntry.h"
# include "TLatex.h"
# include "TStyle.h"
# include "TApplication.h"
# include "TMultiGraph.h"
# include "TMath.h"
# include "TTree.h"
# include "TNtuple.h"
# include "TFile.h"
# include "TKey.h"
# include "TLine.h"
# include "TPaveText.h"
# include "TFitResult.h"
# include "TSystem.h"
const float CRV_TDC_RATE = 159.324e6; // Hz
const float RATE=(CRV_TDC_RATE/2.0)/1.0e9; // GHZ
const int BOARD_STATUS_REGISTERS=22;
const int FPGA_BLOCK_REGISTERS=38;
const int FPGA_BLOCKS=4;
const float DEFAULT_BETA=16.0;
struct TemperatureCorrections
{
double PEOvervoltageChange{0.229}; //1/V
double calibOvervoltageChange{125.5}; //ADC*ns/V
double calibTemperatureChangeAFE{-1.46}; //ADC*ns/K additional calib change due to temperature change in AFE
double overvoltageTemperatureChangeCMB{-0.0554}; //V/K
double overvoltageTemperatureChangeFEB{0.00409}; //V/K
double referenceTemperatureCMB{20.0}; //degC
double referenceTemperatureFEB{40.0}; //degC
};
typedef std::map<std::pair<int,std::pair<int,int> >, std::pair<int,std::pair<float,float> > > ChannelMapType; //feb,(channel1,channel2) --> side,(x,y)
class Calibration
{
public:
Calibration(const std::string &calibFileName, const int numberOfFebs, const int channelsPerFeb);
bool IsNewFormat() const {return _newFormat;}
const std::vector<float> &GetPedestals() const {return _pedestal;}
const std::vector<float> &GetCalibrationFactors() const {return _calibrationFactor;}
const std::vector<float> &GetCalibrationFactorsTemperatureCorrected() const {return _calibrationFactorTemperatureCorrected;}
const std::vector<float> &GetNoiseRate() const {return _noise;}
const std::vector<float> &GetXtalkProbability() const {return _xtalk;}
private:
int _numberOfFebs;
int _channelsPerFeb;
bool _newFormat; //temperature corrected
std::vector<float> _pedestal;
std::vector<float> _calibrationFactor;
std::vector<float> _calibrationFactorTemperatureCorrected;
std::vector<float> _noise;
std::vector<float> _xtalk;
Calibration();
};
Calibration::Calibration(const std::string &calibFileName, const int numberOfFebs, const int channelsPerFeb) :
_numberOfFebs(numberOfFebs), _channelsPerFeb(channelsPerFeb), _newFormat(false)
{
std::ifstream calibFile;
calibFile.open(calibFileName.c_str());
if(!calibFile.is_open()) {std::cerr<<"Could not open calib file."<<std::endl; exit(1);}
_pedestal.resize(_numberOfFebs*_channelsPerFeb);
_calibrationFactor.resize(_numberOfFebs*_channelsPerFeb);
_calibrationFactorTemperatureCorrected.resize(_numberOfFebs*_channelsPerFeb);
_noise.resize(_numberOfFebs*_channelsPerFeb);
_xtalk.resize(_numberOfFebs*_channelsPerFeb);
std::string tmp;
getline(calibFile,tmp);
if(tmp.find("calib v2")==0)
{
getline(calibFile,tmp); //table header
_newFormat=true;
int i, j;
float pedestalTmp, calibrationFactorTmp, calibrationFactorTemperatureCorrectedTmp, noiseTmp, xtalkTmp;
while(calibFile >> i >> j >> pedestalTmp >> calibrationFactorTmp >> calibrationFactorTemperatureCorrectedTmp >> noiseTmp >> xtalkTmp)
{
_pedestal.at(i*_channelsPerFeb+j)=pedestalTmp;
_calibrationFactor.at(i*_channelsPerFeb+j)=calibrationFactorTmp;
_calibrationFactorTemperatureCorrected.at(i*_channelsPerFeb+j)=calibrationFactorTemperatureCorrectedTmp;
_noise.at(i*_channelsPerFeb+j)=noiseTmp;
_xtalk.at(i*_channelsPerFeb+j)=xtalkTmp;
}
calibFile.close();
return;
}
//try old format
calibFile.seekg(0);
for (int i=0; i<numberOfFebs; i++)
{
for (int j=0; j<_channelsPerFeb; j++)
{
calibFile>>tmp;
_pedestal[i*_channelsPerFeb+j]=atof(tmp.c_str());
}
}
for (int i=0; i<numberOfFebs; i++)
{
for (int j=0; j<_channelsPerFeb; j++)
{
calibFile>>tmp;
_calibrationFactor[i*_channelsPerFeb+j]=atof(tmp.c_str());
_calibrationFactorTemperatureCorrected[i*_channelsPerFeb+j]=0.0;
}
}
calibFile.close();
}
class CrvRecoEvent
{
CrvRecoEvent();
public:
CrvRecoEvent(int signalRegionStart, int signalRegionEnd) : _f("peakfitter","[0]*(TMath::Exp(-(x-[1])/[2]-TMath::Exp(-(x-[1])/[2])))"), _signalRegionStart(signalRegionStart), _signalRegionEnd(signalRegionEnd) {Init();}
void Init();
bool FailedFit(TFitResultPtr fr);
void PeakFitter(const short* data, int numberOfSamples, float pedestal, float calibrationFactor, bool &draw);
TF1 _f;
int _signalRegionStart;
int _signalRegionEnd;
int _fitStatus[2]; //0: no pulse, 1: everything ok, 2: fit failed
float _PEs[2];
float _pulseHeight[2];
float _beta[2];
float _time[2];
float _LEtime[2];
int _recoStartBin[2], _recoEndBin[2];
};
void CrvRecoEvent::Init()
{
for(int i=0; i<2; ++i)
{
_fitStatus[i]=0; _PEs[i]=0; _pulseHeight[i]=NAN; _beta[i]=NAN; _time[i]=NAN; _LEtime[i]=NAN; _recoStartBin[i]=-1; _recoEndBin[i]=-1;
}
}
bool CrvRecoEvent::FailedFit(TFitResultPtr fr)
{
if(fr!=0) return true;
if(!fr->IsValid()) return true;
const double tolerance=0.01; //TODO: Try to ask the minimizer, if the parameter is at the limit
for(int i=0; i<=2; ++i)
{
double v=fr->Parameter(i);
double lower, upper;
fr->ParameterBounds(i,lower,upper);
if((v-lower)/(upper-lower)<tolerance) return true;
if((upper-v)/(upper-lower)<tolerance) return true;
}
return false;
}
void CrvRecoEvent::PeakFitter(const short* data, int numberOfSamples, float pedestal, float calibrationFactor, bool &draw)
{
if(std::isnan(calibrationFactor) || calibrationFactor==0) return;
// if(data[0]==0 && data[1]==0 && data[2]==0 && data[3]==0) return; //FIXME temporary check for bad events
// //where other channels work, so that timSinceSpill wasn't marked as NAN
//remove the pedestal and find the maxima in the signal region
std::vector<float> waveform;
std::vector<std::pair<float, std::pair<int,int> > > peaks; //std::pair<ADC, std::pair<peakbinsStart,peakbinsEnd> >
//for peaks where two more more consecutive ADC values are equal
//(e.g. if the max ADC value is reached)
//the beginning and end of these peak bins is indicated by
//peakbinsStart and peakbinsEnd.
//the actual peak is probably in between this interval
//(relevant for the seed value of the fit)
int peakBinsStart=0;
int peakBinsEnd=0;
for(int bin=_signalRegionStart; bin<=std::min(_signalRegionEnd,numberOfSamples); bin++)
{
waveform.push_back(data[bin]-pedestal);
if(bin<=_signalRegionStart) continue;
if(data[bin-1]<data[bin]) //rising edge
{
peakBinsStart=bin;
peakBinsEnd=bin;
}
if(data[bin-1]==data[bin]) //potentially at a peak with consecutive ADC values which are equal
{
peakBinsEnd=bin;
}
if(data[bin-1]>data[bin]) //falling edge
{
if(peakBinsStart>0) //found a peak
{
if(data[peakBinsStart]-pedestal>6) //ignores fluctuations of the baseline
peaks.emplace_back(data[peakBinsStart]-pedestal, std::make_pair(peakBinsStart,peakBinsEnd));
}
peakBinsStart=0; //so that the loop has to wait for the next rising edge
}
}
//ignore events without pulses
if(peaks.size()==0) return;
//only look at the largest peak (and if available the peak after that for a potential reflected pulse)
size_t iPeak=std::max_element(peaks.begin(),peaks.end())-peaks.begin();
for(int i=0; i<2 && iPeak<peaks.size(); ++i, ++iPeak)
{
peakBinsStart = peaks[iPeak].second.first-_signalRegionStart;
peakBinsEnd = peaks[iPeak].second.second-_signalRegionStart;
float averagePeakBin = 0.5*(peakBinsStart+peakBinsEnd);
//select a range of up to 4 points before and after the peak
//-find up to 5 points before and after the peak for which the waveform is stricly decreasing
//-remove 1 point on each side. this removes potentially "bad points" belonging to a second pulse (i.e. in double pulses)
int nBins = waveform.size();
_recoStartBin[i]=peakBinsStart-1;
_recoEndBin[i]=peakBinsEnd+1;
for(int bin=peakBinsStart-1; bin>=0 && bin>=peakBinsStart-5; bin--)
{
if(waveform[bin]<=waveform[bin+1]) _recoStartBin[i]=bin;
else break;
}
for(int bin=peakBinsEnd+1; bin<=nBins && bin<=peakBinsEnd+5; bin++)
{
if(waveform[bin]<=waveform[bin-1]) _recoEndBin[i]=bin;
else break;
}
if(peakBinsStart-_recoStartBin[i]>1) _recoStartBin[i]++;
if(_recoEndBin[i]-peakBinsEnd>1) _recoEndBin[i]--;
//fill the graph
float binWidth=1.0/RATE;
TGraph g;
for(int bin=_recoStartBin[i]; bin<=_recoEndBin[i]; bin++)
{
float t=bin*binWidth;
float v=waveform[bin];
g.SetPoint(g.GetN(), t, v);
}
//set the fit function
_f.SetParameter(0, waveform[peakBinsStart]*TMath::E());
_f.SetParameter(1, averagePeakBin*binWidth);
_f.SetParameter(2, DEFAULT_BETA);
_f.SetParLimits(0, waveform[peakBinsStart]*TMath::E()*0.7,waveform[peakBinsStart]*TMath::E()*1.5);
_f.SetParLimits(1, averagePeakBin*binWidth-15.0,averagePeakBin*binWidth+15.0);
_f.SetParLimits(2, 5.0, 40.0);
//do the fit
TFitResultPtr fr = g.Fit(&_f,(draw && i==0)?"QS":"NQS");
bool invalidFit=FailedFit(fr);
if(invalidFit)
{
_PEs[i] = waveform[peakBinsStart]*TMath::E()*DEFAULT_BETA/calibrationFactor; //using maximum ADC value of this pulse and a typical value of beta
_pulseHeight[i] = waveform[peakBinsStart];
_time[i] = averagePeakBin*binWidth;
_beta[i] = DEFAULT_BETA;
_LEtime[i] = _time[i]-0.985*DEFAULT_BETA; //time-0.985*beta for 50% pulse height
_fitStatus[i] = 2;
draw = false;
}
else
{
_PEs[i] = fr->Parameter(0)*fr->Parameter(2) / calibrationFactor;
_pulseHeight[i] = fr->Parameter(0)/TMath::E();
_time[i] = fr->Parameter(1);
_beta[i] = fr->Parameter(2);
_LEtime[i] = _time[i]-0.985*_beta[i]; //at 50% of pulse height
_fitStatus[i] = 1;
}
_time[i]+=_signalRegionStart*binWidth;
_LEtime[i]+=_signalRegionStart*binWidth;
if(_PEs[i]<20 || i>0) draw=false;
if(draw)
{
g.SetTitle(Form("Example of a %0.0f PE pulse; Time [ns]; ADC ",_PEs[i]));
g.SetMarkerStyle(20);
g.SetMarkerColor(kBlack);
g.GetHistogram()->GetXaxis()->SetTitleSize(0.05);
g.GetHistogram()->GetXaxis()->SetLabelSize(0.05);
g.GetHistogram()->GetYaxis()->SetTitleSize(0.05);
g.GetHistogram()->GetYaxis()->SetLabelSize(0.05);
g.GetHistogram()->GetYaxis()->SetTitleOffset(-0.6);
g.DrawClone("AP");
_f.SetLineColor(kRed);
_f.DrawCopy("same");
}
}
return;
}
class CrvEvent
{
public:
CrvEvent(const std::string &runNumber, const int numberOfFebs, const int channelsPerFeb, const int numberOfSamples,
TTree *tree, TTree *recoTree, const TemperatureCorrections &temperatureCorrections, float PEcut);
void Reconstruct(int entry, const Calibration &calib);
TCanvas *GetCanvas(int feb, int channel) {return _canvas[feb*_channelsPerFeb+channel];}
TH1F *GetHistPEs(int i, int feb, int channel)
{
return (i==0?_histPEs[feb*_channelsPerFeb+channel]:_histPEsTemperatureCorrected[feb*_channelsPerFeb+channel]);
}
TGraph *GetHistTemperatures(int feb, int channel) {return _histTemperatures[feb*_channelsPerFeb+channel];}
TGraph *GetHistTemperaturesFEB(int feb, int channel) {return _histTemperaturesFEB[feb*_channelsPerFeb+channel];}
void ReadChannelMap(const std::string &channelMapFile);
void TrackFit();
int GetMaxedOutEvents(int feb, int channel) {return _maxedOut[feb*_channelsPerFeb+channel];}
private:
CrvEvent();
int _signalRegionStart;
int _signalRegionEnd;
std::string _runNumber;
int _run;
int _subrun;
int _numberOfFebs;
int _channelsPerFeb;
int _numberOfSamples;
TTree *_tree;
TTree *_recoTree;
const TemperatureCorrections _tc;
int _spillIndex;
int _spillNumber;
int *_lastSpillIndex;
int _eventNumber;
//int *_tdcSinceSpill; //OLD
Long64_t *_tdcSinceSpill;
double *_timeSinceSpill;
short *_adc;
float *_temperature;
int *_boardStatus;
int *_FPGABlocks;
Long64_t _timestamp; //time_t
int *_fitStatus;
float *_PEs;
float *_PEsTemperatureCorrected;
float *_pulseHeight;
float *_beta;
float *_time;
float *_LEtime;
int *_recoStartBin;
int *_recoEndBin;
float *_pedestal;
int *_maxedOut;
int *_fitStatusReflectedPulse;
float *_PEsReflectedPulse;
float *_PEsTemperatureCorrectedReflectedPulse;
float *_pulseHeightReflectedPulse;
float *_betaReflectedPulse;
float *_timeReflectedPulse;
float *_LEtimeReflectedPulse;
int *_recoStartBinReflectedPulse;
int *_recoEndBinReflectedPulse;
std::vector<TCanvas*> _canvas;
std::vector<int> _plot;
std::vector<TH1F*> _histPEs, _histPEsTemperatureCorrected;
std::vector<TGraph*> _histTemperatures;
std::vector<TGraph*> _histTemperaturesFEB;
//for track fits
ChannelMapType _channelMap;
float _PEcut;
float _trackSlope; //using slope=dx/dy to avoid inf for vertical tracks
float _trackIntercept; //x value, where y=0
float _trackChi2;
int _trackPoints;
float _trackPEs;
};
CrvEvent::CrvEvent(const std::string &runNumber, const int numberOfFebs, const int channelsPerFeb, const int numberOfSamples,
TTree *tree, TTree *recoTree, const TemperatureCorrections &temperatureCorrections, float PEcut) :
_runNumber(runNumber), _numberOfFebs(numberOfFebs), _channelsPerFeb(channelsPerFeb), _numberOfSamples(numberOfSamples),
_tree(tree), _recoTree(recoTree), _tc(temperatureCorrections), _PEcut(PEcut)
{
std::ifstream configFile;
configFile.open("config.txt");
if(!configFile.is_open()) {std::cerr<<"Could not open config.txt."<<std::endl; exit(1);}
std::string configKey, configValue;
while(configFile>>configKey>>configValue)
{
if(configKey=="signalRegionStart") _signalRegionStart=atoi(configValue.c_str());
if(configKey=="signalRegionEnd") _signalRegionEnd=atoi(configValue.c_str());
}
configFile.close();
_lastSpillIndex = new int[_numberOfFebs*_channelsPerFeb];
// _tdcSinceSpill = new int[_numberOfFebs]; //OLD
// _timeSinceSpill = new double[_numberOfFebs]; //OLD
_tdcSinceSpill = new Long64_t[_numberOfFebs*_channelsPerFeb];
_timeSinceSpill = new double[_numberOfFebs*channelsPerFeb];
_adc = new short[_numberOfFebs*_channelsPerFeb*_numberOfSamples];
_temperature = new float[_numberOfFebs*_channelsPerFeb];
_boardStatus = new int[_numberOfFebs*BOARD_STATUS_REGISTERS];
_FPGABlocks = new int[_numberOfFebs*FPGA_BLOCKS*FPGA_BLOCK_REGISTERS];
tree->SetBranchAddress("runtree_run_num", &_run);
tree->SetBranchAddress("runtree_subrun_num", &_subrun);
tree->SetBranchAddress("runtree_spill_index", &_spillIndex);
tree->SetBranchAddress("runtree_spill_num", &_spillNumber);
tree->SetBranchAddress("runtree_event_num",&_eventNumber);
tree->SetBranchAddress("runtree_tdc_since_spill", _tdcSinceSpill);
tree->SetBranchAddress("runtree_time_since_spill", _timeSinceSpill);
tree->SetBranchAddress("runtree_adc", _adc);
tree->SetBranchAddress("runtree_temperature", _temperature);
tree->SetBranchAddress("runtree_boardStatus", _boardStatus);
tree->SetBranchAddress("runtree_FPGABlocks", _FPGABlocks);
tree->SetBranchAddress("runtree_spillTimestamp", &_timestamp);
_fitStatus = new int[_numberOfFebs*_channelsPerFeb];
_PEs = new float[_numberOfFebs*_channelsPerFeb];
_PEsTemperatureCorrected = new float[_numberOfFebs*_channelsPerFeb];
_pulseHeight = new float[_numberOfFebs*_channelsPerFeb];
_beta = new float[_numberOfFebs*_channelsPerFeb];
_time = new float[_numberOfFebs*_channelsPerFeb];
_LEtime = new float[_numberOfFebs*_channelsPerFeb];
_recoStartBin = new int[_numberOfFebs*_channelsPerFeb];
_recoEndBin = new int[_numberOfFebs*_channelsPerFeb];
_pedestal = new float[_numberOfFebs*_channelsPerFeb];
_maxedOut = new int[_numberOfFebs*_channelsPerFeb];
_fitStatusReflectedPulse = new int[_numberOfFebs*_channelsPerFeb];
_PEsReflectedPulse = new float[_numberOfFebs*_channelsPerFeb];
_PEsTemperatureCorrectedReflectedPulse = new float[_numberOfFebs*_channelsPerFeb];
_pulseHeightReflectedPulse = new float[_numberOfFebs*_channelsPerFeb];
_betaReflectedPulse = new float[_numberOfFebs*_channelsPerFeb];
_timeReflectedPulse = new float[_numberOfFebs*_channelsPerFeb];
_LEtimeReflectedPulse = new float[_numberOfFebs*_channelsPerFeb];
_recoStartBinReflectedPulse = new int[_numberOfFebs*_channelsPerFeb];
_recoEndBinReflectedPulse = new int[_numberOfFebs*_channelsPerFeb];
recoTree->Branch("runNumber", &_run, "runNumber/I");
recoTree->Branch("subrunNumber", &_subrun, "subrunNumber/I");
recoTree->Branch("spillIndex", &_spillIndex, "spillIndex/I");
recoTree->Branch("spillNumber", &_spillNumber, "spillNumber/I");
recoTree->Branch("boardStatus", _boardStatus, Form("boardStatus[%i][%i]/I",_numberOfFebs,BOARD_STATUS_REGISTERS));
recoTree->Branch("FPGABlocks", _FPGABlocks, Form("FPGABlocks[%i][%i][%i]/I",_numberOfFebs,FPGA_BLOCKS,FPGA_BLOCK_REGISTERS));
recoTree->Branch("spillTimestamp", &_timestamp, "spillTimestamp/L");
recoTree->Branch("eventNumber", &_eventNumber, "eventNumber/I");
recoTree->Branch("tdcSinceSpill", _tdcSinceSpill, Form("tdcSinceSpill[%i][%i]/L",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("timeSinceSpill", _timeSinceSpill, Form("timeSinceSpill[%i][%i]/D",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("fitStatus", _fitStatus, Form("fitStatus[%i][%i]/I",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("PEs", _PEs, Form("PEs[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("PEsTemperatureCorrected", _PEsTemperatureCorrected, Form("PEsTemperatureCorrected[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("temperature", _temperature, Form("temperature[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("pulseHeight", _pulseHeight, Form("pulseHeight[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("beta", _beta, Form("beta[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("time", _time, Form("time[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("LEtime", _LEtime, Form("LEtime[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("adc", _adc, Form("adc[%i][%i][%i]/S",_numberOfFebs,_channelsPerFeb,_numberOfSamples));
recoTree->Branch("recoStartBin", _recoStartBin, Form("recoStartBin[%i][%i]/I",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("recoEndBin", _recoEndBin, Form("recoEndBin[%i][%i]/I",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("pedestal", _pedestal, Form("pedestal[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("fitStatusReflectedPulse", _fitStatusReflectedPulse, Form("fitStatusReflectedPulse[%i][%i]/I",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("PEsReflectedPulse", _PEsReflectedPulse, Form("PEsReflectedPulse[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("PEsTemperatureCorrectedReflectedPulse", _PEsTemperatureCorrectedReflectedPulse, Form("PEsTemperatureCorrectedReflectedPulse[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("pulseHeightReflectedPulse", _pulseHeightReflectedPulse, Form("pulseHeightReflectedPulse[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("betaReflectedPulse", _betaReflectedPulse, Form("betaReflectedPulse[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("timeReflectedPulse", _timeReflectedPulse, Form("timeReflectedPulse[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("LEtimeReflectedPulse", _LEtimeReflectedPulse, Form("LEtimeReflectedPulse[%i][%i]/F",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("recoStartBinReflectedPulse", _recoStartBinReflectedPulse, Form("recoStartBinReflectedPulse[%i][%i]/I",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("recoEndBinReflectedPulse", _recoEndBinReflectedPulse, Form("recoEndBinReflectedPulse[%i][%i]/I",_numberOfFebs,_channelsPerFeb));
recoTree->Branch("trackSlope", &_trackSlope, "trackSlope/F");
recoTree->Branch("trackIntercept", &_trackIntercept, "trackIntercept/F");
recoTree->Branch("trackChi2", &_trackChi2, "trackChi2/F");
recoTree->Branch("trackPoints", &_trackPoints, "trackPoints/I");
recoTree->Branch("trackPEs", &_trackPEs, "trackPEs/F");
_canvas.resize(_numberOfFebs*_channelsPerFeb);
_plot.resize(_numberOfFebs*_channelsPerFeb);
_histPEs.resize(_numberOfFebs*_channelsPerFeb);
_histPEsTemperatureCorrected.resize(_numberOfFebs*_channelsPerFeb);
_histTemperatures.resize(_numberOfFebs*_channelsPerFeb);
_histTemperaturesFEB.resize(_numberOfFebs*_channelsPerFeb);
for(int i=0; i<_numberOfFebs; i++)
{
for(int j=0; j<_channelsPerFeb; j++)
{
int index=i*_channelsPerFeb+j; //used for _variable[i][j]
_maxedOut[index]=0;
_lastSpillIndex[index]=-1;
_plot[index]=4;
_canvas[index]=new TCanvas();
_canvas[index]->Divide(2,2);
for(int k=0; k<4; ++k)
{
_canvas[index]->cd(k+1);
// gPad->SetPad(0,0,0.5,1);
}
_canvas[index]->cd(3);
gPad->Divide(2,2);
_canvas[index]->cd(4);
gPad->Divide(1,2);
_histPEs[index]=new TH1F(Form("PEs_%i_%i",i,j),Form("PE Distribution Run %s FEB %i Channel %i;PE;Counts",_runNumber.c_str(),i,j),75,0,150);
_histPEsTemperatureCorrected[index]=new TH1F(Form("PEsTempCorrected_%i_%i",i,j),Form("PE Distribution (temp. corrected) Run %s FEB %i Channel %i;PE;Counts",_runNumber.c_str(),i,j),75,0,150);
_histTemperatures[index]=new TGraph();
_histTemperaturesFEB[index]=new TGraph();
_histPEs[index]->SetLineColor(kBlack);
_histPEsTemperatureCorrected[index]->SetLineColor(kBlack);
_histTemperatures[index]->SetMarkerStyle(20);
_histTemperatures[index]->SetMarkerSize(0.5);
_histTemperatures[index]->SetMarkerColor(kBlack);
_histTemperatures[index]->SetNameTitle(Form("hT_%i",index),Form("Temperature Run %s FEB %i Channel %i;Spill;Temperature [deg C]",_runNumber.c_str(),i,j));
_histTemperaturesFEB[index]->SetMarkerStyle(20);
_histTemperaturesFEB[index]->SetMarkerSize(0.5);
_histTemperaturesFEB[index]->SetMarkerColor(kBlack);
_histTemperaturesFEB[index]->SetNameTitle(Form("hTFEB_%i",index),Form("FEB Temperature Run %s FEB %i Channel %i;Spill;Temperature [deg C]",_runNumber.c_str(),i,j));
}
}
}
void CrvEvent::Reconstruct(int entry, const Calibration &calib)
{
CrvRecoEvent reco(_signalRegionStart,_signalRegionEnd);
_tree->GetEntry(entry);
if(entry%1000==0) std::cout<<"R "<<entry<<std::endl;
for(int i=0; i<_numberOfFebs; i++)
{
for(int j=0; j<_channelsPerFeb; j++)
{
reco.Init();
int index=i*_channelsPerFeb+j; //used for _variable[i][j]
bool draw = (_plot[index]>0);
if(draw)
{
_canvas[index]->cd(3);
gPad->cd(_plot[index]);
}
float pedestal = calib.GetPedestals().at(index);
float calibrationFactor = calib.GetCalibrationFactors().at(index);
float calibrationFactorTemperatureCorrected = calib.GetCalibrationFactorsTemperatureCorrected().at(index);
if(!isnan(_timeSinceSpill[index])) //missing FEB/channel in raw data
reco.PeakFitter(&(_adc[index*_numberOfSamples]), _numberOfSamples, pedestal, calibrationFactor, draw);
//main pulse
_fitStatus[index] = reco._fitStatus[0];
_PEs[index] = reco._PEs[0];
_PEsTemperatureCorrected[index] = -1;
double temperatureFEB=-1000;
if(_boardStatus[i*BOARD_STATUS_REGISTERS]!=-1) //i-th FEB was read for this spill
{
//temperature of i-th FEB
temperatureFEB=_boardStatus[i*BOARD_STATUS_REGISTERS+2]*0.01; //TODO: document seems to indicate a factor of 10.0
}
if(fabs(_temperature[index])<100 && fabs(temperatureFEB)<100) //temperature of -1000 means no temperature found
{
//overvoltage difference for actual CMB and FEB temperatures w.r.t. reference CMB and FEB temperatures
//deltaOvervoltage = overvoltageTemperatureChangeCMB*(TCMB-TrefCMB) + overvoltageTemperatureChangeFEB*(TFEB-TrefFEB)
float deltaOvervoltage = _tc.overvoltageTemperatureChangeCMB*(_temperature[index]-_tc.referenceTemperatureCMB)
+ _tc.overvoltageTemperatureChangeFEB*(temperatureFEB-_tc.referenceTemperatureFEB);
_PEsTemperatureCorrected[index] = reco._PEs[0];
if(calib.IsNewFormat() && calibrationFactorTemperatureCorrected!=0) //The old calib format doesn't have temperature-corrected calib constants.
{
//The stored temperature corrected calibration constants were adjusted to CMB/FEB reference temperatures TrefCMB/TrefFEB of e.g. 20 degC / 40 degC.
//The calibration constant needs to be adjusted to the temperature T at which the signal pulse happened.
//calibConst(TCMB,TFEB) = calibConst(TrefCMB,TrefFEB) + calibOvervoltageChange*deltaOvervoltage(TCMB,TFEB) + calibTempChangeAFE*(TFEB-TrefFEB)
float calibrationFactorAtT = calibrationFactorTemperatureCorrected + _tc.calibOvervoltageChange*deltaOvervoltage
+ _tc.calibTemperatureChangeAFE*(temperatureFEB-_tc.referenceTemperatureFEB);
//The PeakFitter used the un-corrected calibration factor to calculate the PEs
//This needs to be undone first to get the pulse area
//pulseArea = PEsFromPeakFitter * uncorrectedCalibrationFactor
//PEsWithCorrectCalibConstant = pulseArea / calibrationFactorAtT
_PEsTemperatureCorrected[index]*=calibrationFactor/calibrationFactorAtT;
}
//The PEs that were measured at a particular temperature need to be adjusted to the reference temperature of e.g. 20 degC
//PE(TCMB,TFEB)/PE(TrefCMB,TrefFEB) = 1.0 + PEOvervoltageChange*deltaOvervoltage(TCMB,TFEB)
_PEsTemperatureCorrected[index]/=1.0 + _tc.PEOvervoltageChange*deltaOvervoltage;
}
_pulseHeight[index] = reco._pulseHeight[0];
_beta[index] = reco._beta[0];
_time[index] = reco._time[0];
_LEtime[index] = reco._LEtime[0];
_recoStartBin[index] = reco._recoStartBin[0]+_signalRegionStart;
_recoEndBin[index] = reco._recoEndBin[0]+_signalRegionStart;
_pedestal[index] = pedestal;
if(draw && reco._fitStatus[0]==1) _plot[index]--;
//reflected pulse
_fitStatusReflectedPulse[index] = reco._fitStatus[1];
_PEsReflectedPulse[index] = reco._PEs[1];
_PEsTemperatureCorrectedReflectedPulse[index] = -1;
if(_temperature[index]>-300 && temperatureFEB>-300) //temperature of -1000 means no temperature found
{
float deltaOvervoltage = _tc.overvoltageTemperatureChangeCMB*(_temperature[index]-_tc.referenceTemperatureCMB)
+ _tc.overvoltageTemperatureChangeFEB*(temperatureFEB-_tc.referenceTemperatureFEB);
_PEsTemperatureCorrectedReflectedPulse[index] = reco._PEs[1];
if(calib.IsNewFormat() && calibrationFactorTemperatureCorrected!=0)
{
float calibrationFactorAtT = calibrationFactorTemperatureCorrected + _tc.calibOvervoltageChange*deltaOvervoltage
+ _tc.calibTemperatureChangeAFE*(temperatureFEB-_tc.referenceTemperatureFEB);
_PEsTemperatureCorrectedReflectedPulse[index]*=calibrationFactor/calibrationFactorAtT;
}
_PEsTemperatureCorrectedReflectedPulse[index]/=1.0 + _tc.PEOvervoltageChange*deltaOvervoltage;
}
_pulseHeightReflectedPulse[index] = reco._pulseHeight[1];
_betaReflectedPulse[index] = reco._beta[1];
_timeReflectedPulse[index] = reco._time[1];
_LEtimeReflectedPulse[index] = reco._LEtime[1];
_recoStartBinReflectedPulse[index] = reco._recoStartBin[1]+_signalRegionStart;
_recoEndBinReflectedPulse[index] = reco._recoEndBin[1]+_signalRegionStart;
//fill histograms
if(reco._fitStatus[0]==1)
{
_histPEs[index]->Fill(reco._PEs[0]);
_histPEsTemperatureCorrected[index]->Fill(_PEsTemperatureCorrected[index]);
}
if(_temperature[index]>-300 && _lastSpillIndex[index]!=_spillIndex) //temperature of -1000 means no temperature found
{
_histTemperatures[index]->SetPoint(_histTemperatures[index]->GetN(),_spillIndex,_temperature[index]);
double temperatureFEB=_boardStatus[i*BOARD_STATUS_REGISTERS+2]*0.01; //TODO: document seems to indicate a factor of 10.0
_histTemperaturesFEB[index]->SetPoint(_histTemperaturesFEB[index]->GetN(),_spillIndex,temperatureFEB);
_lastSpillIndex[index]=_spillIndex;
}
//check, if ADCs have maxed-out
for(int iSample=0; iSample<_numberOfSamples; ++iSample)
{
if(_adc[index*_numberOfSamples+iSample]==2047) {++_maxedOut[index]; break;}
}
}
}
//Fit track, if channel map is provided
if(!_channelMap.empty())
{
TrackFit();
}
_recoTree->Fill();
}
void CrvEvent::TrackFit()
{
float sumX =0;
float sumY =0;
float sumXY =0;
float sumYY =0;
_trackSlope =0;
_trackIntercept=0;
_trackPEs =0;
_trackPoints =0;
_trackChi2 =-1;
//loop through the channel map
for(ChannelMapType::iterator channelIter=_channelMap.begin(); channelIter!=_channelMap.end(); ++channelIter)
{
int feb=channelIter->first.first;
int channel1=channelIter->first.second.first;
int channel2=channelIter->first.second.second;
// float side=channelIter->second.first;
float x=channelIter->second.second.first;
float y=channelIter->second.second.second;
if(feb<0 || feb>=_numberOfFebs) continue; //feb not in event tree
if(channel1<0 || channel2<0 || channel1>=_channelsPerFeb || channel2>=_channelsPerFeb) continue; //channels not in event tree
int index1=feb*_channelsPerFeb+channel1; //used for _variable[i][j]
int index2=feb*_channelsPerFeb+channel2; //used for _variable[i][j]
float PE1 = _PEsTemperatureCorrected[index1];
float PE2 = _PEsTemperatureCorrected[index2];
if(PE1<=0 || _fitStatus[index1]==0) PE1=0;
if(PE2<=0 || _fitStatus[index2]==0) PE2=0;
//collect information for the fit
//uses hit information from both sides
float PE = PE1+PE2;
if(PE<_PEcut) continue;
sumX +=x*PE;
sumY +=y*PE;
sumXY+=x*y*PE;
sumYY+=y*y*PE;
_trackPEs+=PE;
++_trackPoints;
}
//do the fit
if(_trackPEs>=2*_PEcut && _trackPoints>1)
{
if(_trackPEs*sumYY-sumY*sumY!=0)
{
_trackSlope=(_trackPEs*sumXY-sumX*sumY)/(_trackPEs*sumYY-sumY*sumY);
_trackIntercept=(sumX-_trackSlope*sumY)/_trackPEs;
//find chi2
_trackChi2=0;
for(ChannelMapType::iterator channelIter=_channelMap.begin(); channelIter!=_channelMap.end(); ++channelIter)
{
int feb=channelIter->first.first;
int channel1=channelIter->first.second.first;
int channel2=channelIter->first.second.second;
// float side=channelIter->second.first;
float x=channelIter->second.second.first;
float y=channelIter->second.second.second;
if(feb<0 || feb>=_numberOfFebs) continue; //feb not in event tree
if(channel1<0 || channel2<0 || channel1>=_channelsPerFeb || channel2>=_channelsPerFeb) continue; //channels not in event tree
int index1=feb*_channelsPerFeb+channel1; //used for _variable[i][j]
int index2=feb*_channelsPerFeb+channel2; //used for _variable[i][j]
float PE1 = _PEsTemperatureCorrected[index1];
float PE2 = _PEsTemperatureCorrected[index2];
if(PE1<=0 || _fitStatus[index1]==0) PE1=0;
if(PE2<=0 || _fitStatus[index2]==0) PE2=0;
float PE = PE1+PE2;
if(PE<_PEcut) continue;
float xFit = _trackSlope*y + _trackIntercept;
_trackChi2+=(xFit-x)*(xFit-x)*PE; //PE-weighted chi2
}
_trackChi2/=_trackPEs;
}
}
}
void CrvEvent::ReadChannelMap(const std::string &channelMapFile)
{
std::ifstream file(channelMapFile);
if(!file.is_open()) {std::cout<<"Channel map file could not be opened."<<std::endl; return;}
std::string header;
getline(file,header);
int febA, channelA1, channelA2;
int febB, channelB1, channelB2;
float x, y;
while(file >> febA >> channelA1 >> channelA2 >> febB >> channelB1 >> channelB2 >> x >> y)
{
std::pair<int,int> channelPairA(channelA1,channelA2);
std::pair<int,int> channelPairB(channelB1,channelB2);
std::pair<int,std::pair<int,int> > counterA(febA,channelPairA);
std::pair<int,std::pair<int,int> > counterB(febB,channelPairB);
std::pair<float,float> counterPos(x,y);
_channelMap[counterA]=std::pair<int, std::pair<float,float> >(0,counterPos);
_channelMap[counterB]=std::pair<int, std::pair<float,float> >(1,counterPos);
}
file.close();
}
double LandauGaussFunction(double *x, double *par)
{
//From $ROOTSYS/tutorials/fit/langaus.C
//Fit parameters:
//par[0]=Width (scale) parameter of Landau density
//par[1]=Most Probable (MP, location) parameter of Landau density
//par[2]=Total area (integral -inf to inf, normalization constant)
//par[3]=Width (sigma) of convoluted Gaussian function
//
//In the Landau distribution (represented by the CERNLIB approximation),
//the maximum is located at x=-0.22278298 with the location parameter=0.
//This shift is corrected within this function, so that the actual
//maximum is identical to the MP parameter.
// Numeric constants
Double_t invsq2pi = 0.3989422804014; // (2 pi)^(-1/2)
Double_t mpshift = -0.22278298; // Landau maximum location
// Control constants
Double_t np = 100.0; // number of convolution steps
Double_t sc = 5.0; // convolution extends to +-sc Gaussian sigmas
// Variables
Double_t xx;
Double_t mpc;
Double_t fland;
Double_t sum = 0.0;
Double_t xlow,xupp;
Double_t step;
Double_t i;
// MP shift correction
mpc = par[1] - mpshift * par[0];
// Range of convolution integral
xlow = x[0] - sc * par[3];
xupp = x[0] + sc * par[3];
step = (xupp-xlow) / np;
// Convolution integral of Landau and Gaussian by sum
for(i=1.0; i<=np/2; i++)
{
xx = xlow + (i-.5) * step;
fland = TMath::Landau(xx,mpc,par[0]) / par[0];
sum += fland * TMath::Gaus(x[0],xx,par[3]);
xx = xupp - (i-.5) * step;
fland = TMath::Landau(xx,mpc,par[0]) / par[0];
sum += fland * TMath::Gaus(x[0],xx,par[3]);
}
return (par[2] * step * sum * invsq2pi / par[3]);
}
void LandauGauss(TH1F &h, float &mpv, float &fwhm, float &signals, float &chi2, float &error)
{
std::multimap<float,float> bins; //binContent,binCenter
for(int i=8; i<=h.GetNbinsX(); i++) bins.emplace(h.GetBinContent(i),h.GetBinCenter(i)); //ordered from smallest to largest bin entries
if(bins.size()<4) return;
if(bins.rbegin()->first<20) return; //low statistics
int nBins=0;
float binSum=0;
for(auto bin=bins.rbegin(); bin!=bins.rend(); ++bin)
{
nBins++;
binSum+=bin->second;
if(nBins==4) break;
}
float maxX=binSum/4;
float fitRangeStart=0.7*maxX; //0.6 @ 24
float fitRangeEnd =2.0*maxX;
if(fitRangeStart<15.0) fitRangeStart=15.0;
//Parameters
Double_t startValues[4], parLimitsLow[4], parLimitsHigh[4];
//Most probable value
startValues[1]=maxX;
parLimitsLow[1]=fitRangeStart;
parLimitsHigh[1]=fitRangeEnd;
//Area
startValues[2]=h.Integral(h.FindBin(fitRangeStart),h.FindBin(fitRangeEnd));
parLimitsLow[2]=0.01*startValues[2];
parLimitsHigh[2]=100*startValues[2];
//Other parameters
startValues[0]=5.0; startValues[3]=10.0;
parLimitsLow[0]=2.0; parLimitsLow[3]=2.0;
parLimitsHigh[0]=15.0; parLimitsHigh[3]=20.0; //7 and 15 @ 21 //6 and 13 @ 23
TF1 fit("LandauGauss",LandauGaussFunction,fitRangeStart,fitRangeEnd,4);
fit.SetParameters(startValues);
fit.SetLineColor(kRed);
fit.SetParNames("Width","MP","Area","GSigma");
for(int i=0; i<4; i++) fit.SetParLimits(i, parLimitsLow[i], parLimitsHigh[i]);
TFitResultPtr fr = h.Fit(&fit,"LQRS");
fit.Draw("same");
mpv = fit.GetMaximumX(); //fit.GetParameter(1) does not give the correct result, probably because it is not a pure Landau function
chi2 = (fr->Ndf()>0?fr->Chi2()/fr->Ndf():NAN);
if(mpv==fitRangeStart) {mpv=0; return;}
float halfMaximum = fit.Eval(mpv)/2.0;
float leftX = fit.GetX(halfMaximum,0.0,mpv);
float rightX = fit.GetX(halfMaximum,mpv,10.0*mpv);
fwhm = rightX-leftX;
signals = fit.Integral(0,150)/h.GetBinWidth(1); //need to divide by bin width.
//if the bin width is 2 and one has e.g. 20 events for 50PEs and 20 events for 51PEs,
//the combined bin of x=50/51 gets 40 entries and the integral assumes that there are 40 entries for x=50 and x=51.
error = fit.GetParError(1);
}
double PoissonFunction(double *x, double *par)
{
//par[1] is the single parameter of the original TMath::Poisson
//par[0] is the vertial scaling factor
//par[2] is the horizontal scaling factor
if(par[0]<0 || par[1]<0 || par[2]<0) return TMath::QuietNaN();
if(x[0]<0) return 0;
if(x[0]==0.0) return TMath::Exp(-par[1]);
const double scaledX=x[0]*par[2];
return par[0]*TMath::Exp(scaledX * log(par[1]) - TMath::LnGamma(scaledX + 1.) - par[1]);
}
void Poisson(TH1F &h, float &mpv, float &fwhm, float &signals, float &chi2, float &error)
{
float maxX=0;
float maxValue=0;
for(int i=0; i<=h.GetNbinsX(); i++)
{
if(h.GetBinContent(i)>maxValue) {maxValue=h.GetBinContent(i); maxX=h.GetBinCenter(i);}
}
if(maxValue<20) return;
//Fit range
Double_t fitRangeStart=0.7*maxX;
Double_t fitRangeEnd =1.3*maxX;
TF1 fit("Poisson",PoissonFunction,fitRangeStart,fitRangeEnd,3);
fit.SetParameter(0,maxValue/TMath::Poisson(maxX,maxX)); //"height" of the function
fit.SetParameter(1,maxX); //expected value
fit.SetParameter(2,1.0); //horizontal (x) scaling factor
fit.SetLineColor(kRed);
fit.SetParNames("Const","mean");
TFitResultPtr fr = h.Fit(&fit,"QRS");
fit.Draw("same");
mpv = fit.GetMaximumX();
chi2 = (fr->Ndf()>0?fr->Chi2()/fr->Ndf():NAN);
if(mpv==fitRangeStart) {mpv=0; return;}
float halfMaximum = fit.Eval(mpv)/2.0;
float leftX = fit.GetX(halfMaximum,0.0,mpv);
float rightX = fit.GetX(halfMaximum,mpv,10.0*mpv);
fwhm = rightX-leftX;
signals = fit.Integral(0,150)/h.GetBinWidth(1); //explanation see Landau-Gauss
error = fit.GetParError(1);
if(fit.GetParameter(2)!=0) error/=fit.GetParameter(2);
}
void BoardRegisters(TTree *treeSpills, std::ofstream &txtFile, const int numberOfFebs, int *febID, int &nSpillsActual, int *nFebSpillsActual,
float *febTemperaturesAvg, float *supplyMonitorsAvg, float *biasVoltagesAvg, int *pipeline, int *samples, Long64_t ×tamp) //time_t
{
if(!treeSpills->GetBranch("spill_boardStatus")) return; //older file: board status was not stored
Long64_t timestampTmp; //time_t
int nEventsExpected;
int nEventsActual;
bool spillStored;
bool timestampFound=false;
int *boardStatus = new int[numberOfFebs*BOARD_STATUS_REGISTERS];
treeSpills->SetBranchAddress("spill_nevents", &nEventsExpected);
treeSpills->SetBranchAddress("spill_neventsActual", &nEventsActual);
treeSpills->SetBranchAddress("spill_stored", &spillStored);
treeSpills->SetBranchAddress("spill_boardStatus", boardStatus);
treeSpills->SetBranchAddress("spill_timestamp", ×tampTmp);
int nEventsExpectedTotal=0; //of the spills that were stored
int nEventsActualTotal=0;
int nSpillsExpected=treeSpills->GetEntries();
nSpillsActual=0;
bool foundFeb[numberOfFebs]={false};
for(int feb=0; feb<numberOfFebs; ++feb)
{
febID[feb]=0;
febTemperaturesAvg[feb]=0;
pipeline[feb]=0;
samples[feb]=0;
nFebSpillsActual[feb]=0;
}
for(int i=0; i<numberOfFebs*8; ++i)
{
supplyMonitorsAvg[i]=0;
biasVoltagesAvg[i]=0;
}
float febTemperatures[numberOfFebs]={0};
float supplyMonitors[numberOfFebs][8]={0};
float biasVoltages[numberOfFebs][8]={0};
for(int iSpill=0; iSpill<nSpillsExpected; ++iSpill)
{
treeSpills->GetEntry(iSpill);
if(timestampTmp!=0 && !timestampFound) //the first spill may not have a time stamp
{
timestampFound=true;
timestamp=timestampTmp; //to get the time stamp, that gets overwritten at the next GetEntry calls
long timestampL=timestamp; //long required below
txtFile<<"timestamp: "<<ctime(×tampL);
}
if(spillStored)
{
nEventsExpectedTotal+=nEventsExpected;