/
CalcPeriod.C
180 lines (134 loc) · 4.64 KB
/
CalcPeriod.C
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
#define anaChannel 5
#define DOMINO_NCELL 1024
#define DOMINO_NCH 8
#define DOMINO_DEPTH 12 // number of bit
#include "datastructure.h"
#include "io.h"
#include "functions.h"
const Double_t Pi = TMath::Pi();
void CalcPeriod(char *DataFile = "drs4_peds_5buffers.dat", Int_t nevt,
Int_t startEv = 1, char *PedFile) {
// create progress bar
TGHProgressBar *gProgress = ProgressBar("Calcolo periodo");
// Redefine DOMINO Depth in ADC counts
const Float_t DominoDepthADC = pow(2, DOMINO_DEPTH);
// open file
FILE *fdata = OpenDataFile(DataFile);
struct channel_struct *p;
struct channel_struct *dep;
// create list of graphs for pedestals
TList *grPedList = new TList();
TGraphErrors *grPed;
// create period histogram
TString title = "Period histogram";
TH1 *hPeriod = new TH1F(title,title, 2*((Int_t) DOMINO_NCELL), (Double_t) -DOMINO_NCELL, (Double_t) DOMINO_NCELL);
// calculate or read pedestals from file
grPedList = OpenPedestals(PedFile);
grPed = (TGraphErrors *) grPedList->At(anaChannel);
// Count number of events in data file
int nevtDataMax = 0;
while (!feof(fdata)) {
fread((void *) &event_data, 1, sizeof(event_data), fdata);
nevtDataMax++;
}
printf("nevtDataMax: %d\n", nevtDataMax - 1);
if (nevt > (nevtDataMax - startEv) || nevt == 0)
nevt = nevtDataMax - startEv;
cout << endl << "==>> Processing " << nevt << " events from file "
<< DataFile << endl;
rewind(fdata);
Int_t ievt = 1;
// go to first event (startEv)
while (ievt < startEv) {
fread((void *) &event_data, 1, sizeof(event_data), fdata);
if (feof(fdata))
break;
ievt++;
}
ievt = 1;
Int_t flagEnd = 0;
Int_t fitusati = 0;
Double_t chtmp;
Double_t PedVal, itmp;
Double_t mean, rms;
Double_t ratio;
//debug canvas
TCanvas *cfitTest = new TCanvas("cfitTest", "fit tests", 1200, 780);
cfitTest->Divide(1,nevt);
// loop on events
gProgress->Reset();
gProgress->SetMax(nevt);
gSystem->ProcessEvents();
while (ievt <= nevt && !flagEnd) {
fread((void *) &event_data, 1, sizeof(event_data), fdata);
if (feof(fdata))
flagEnd = 1;
p = (struct channel_struct *) &event_data.ch[0]; // read bunch of data
dep = (struct channel_struct *) &event_data.ch[1]; // read bunch of data
// goes to channel to analyze
p += anaChannel;
// read data, subtract pedestals values and save results in grAnaChDataTemp graph with
// fixed error for each point (x = 0.5 and y = 2.1). Also generate an array with Domino
// X and Y values
TGraphErrors *grAnaChDataTemp = new TGraphErrors(DOMINO_NCELL);
for (int ch = 0; ch < DOMINO_NCELL; ch++) {
// Read pedestal value for this cell
grPed->GetPoint(ch, itmp, PedVal);
chtmp = (Double_t)(p->data[ch]); // data value
chtmp = chtmp - PedVal;
grAnaChDataTemp->SetPoint(ch, (Double_t) ch, chtmp);
grAnaChDataTemp->SetPointError(ch, 0.5, 2.1);
}
// create fit functions
TF1 *fsin = new TF1("fsin", sigSin, 0., 1024., 4);
fsin->SetParameters(600., 255., 150., 150.);
fsin->SetParNames("amplitude", "Period", "Phase", "DC-Offset");
grAnaChDataTemp->Fit("fsin", "Q");
TF1 *fsinFit = grAnaChDataTemp->GetFunction("fsin");
fsinFit->SetParNames("amplitude", "Period", "Phase", "DC-Offset");
// debug
cfitTest->cd(ievt);
grAnaChDataTemp->SetMarkerStyle(20);
grAnaChDataTemp->SetMarkerSize(0.3);
grAnaChDataTemp->GetYaxis()->SetLabelSize(0.12);
grAnaChDataTemp->GetXaxis()->SetLabelSize(0.12);
grAnaChDataTemp->Draw("APE");
Double_t fitPeriod, fitAmplitude, chisquare;
fitPeriod = fsinFit->GetParameter("Period");
fitAmplitude = TMath::Abs(fsinFit->GetParameter("amplitude"));
chisquare = fsinFit->GetChisquare();
cout << "period: " << fitPeriod << " amplitude: " << fitAmplitude << " chisquare: " << chisquare << endl;
if(chisquare > 0.1e+06) {
gProgress->Increment(1);
gSystem->DispatchOneEvent(kTRUE);
ievt++;
continue;
}
gProgress->Increment(1);
gSystem->DispatchOneEvent(kTRUE);
hPeriod->Fill(fitPeriod);
fitusati++;
ievt++;
}
cout << "fit scartati :" << nevt - fitusati << endl;
//draw
TString Title = "Period distribution for nevt events";
TCanvas *cPeriod = new TCanvas("cPeriod", Title, 700, 700);
hPeriod->Draw();
hPeriod->Fit("gaus");
TF1 *fgausFit = hPeriod->GetFunction("gaus");
//mean = fgausFit->GetParameter(1);
// rms = fgausFit->GetParameter(2);
mean = hPeriod->GetMean();
rms = hPeriod->GetRMS();
TString OutFile = "Period";
OutFile += nevt;
OutFile += "events.dat";
FILE *f = fopen(OutFile.Data(), "w");
fwrite(&mean, sizeof(mean), 1, f);
fwrite(&rms, sizeof(rms), 1, f);
((TGMainFrame *) gProgress->GetParent())->CloseWindow();
fclose(f);
cout << "mean: " << mean << " rms: " << rms << endl;
fclose(fdata);
}