-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.C
161 lines (140 loc) · 5.87 KB
/
temp.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
{
// Look at data with many more samples in PC_Waveform.root
//void fitData(TString dataName)
//{
TString dataName = "SWF396_1;1";
TFile f("PC_waveform.root");
f.cd("makeSD");
const double shapingTime = 25.0;
cout << dataName << endl;
TH1F *histData = (TH1F*) gDirectory->Get(dataName);
int option = 0;
TF1 *fittingFunction = new TF1();
std::vector<unsigned int> *adc = new std::vector<unsigned int>;
float mcenergy;
float mctrigenergy;
const int numEntries = histData->GetEntries();
Double_t qMeasurementTimes[numEntries];
Double_t qAdc[numEntries];
Double_t errorY[numEntries];
Double_t errorX[numEntries];
float qMcenergy;
float qMctrigenergy;
// Convert histogram to TGraph
for (int i = 0; i < histData->GetEntries(); ++i)
{
qAdc[i] = histData->GetBinContent(i);
qMeasurementTimes[i] = histData->GetBinCenter(i);
errorY[i] = 0.75;
errorX[i] = 0.0;
}
TGraphErrors *graph = new TGraphErrors(histData->GetEntries(),qMeasurementTimes,qAdc,errorX,errorY);
std::vector<Float_t> tPeak;
std::vector<Float_t> adcPeak;
findPeaks(graph,tPeak,adcPeak,2.0);
if (tPeak.size() == 0){
cout << graph->GetX()[0] << endl;
cout << "fail " << endl;}
fittingFunction = new TF1();
const double firstBin = graph->GetX()[0];
const double lastBin = graph->GetX()[graph->GetN()-1];
// If we have a dynamic pedestal
if (tPeak[0] == 0.0)
{
if (tPeak.size() == 1)
{
option = 1;
const double Q = TMath::Max(qAdc[0],0.0);
Double_t newData[8];
// Subtract dynamic pedestal from data
for (int i = 0; i < graph->GetN(); ++i)
{
newData[i] = qAdc[i] - Q*exp(-qMeasurementTimes[i]/shapingTime);
}
const Double_t newAdcPeak = TMath::MaxElement(numEntries,newData);
const Double_t newTimePeak = TMath::LocMax(numEntries,newData);
fittingFunction = new TF1("fittingFunction",fittingFunction7Uniform,firstBin,lastBin,5);
const double timeShift = newTimePeak - shapingTime;
const double scalingFactor = TMath::Max(newAdcPeak /0.015, 1000.0);
const double sigma = 10.0;
fittingFunction->SetParameters(timeShift,scalingFactor,Q,sigma,shapingTime);
//fittingFunction->SetParLimits(2,0.0,1000.0);
//fittingFunction->SetParLimits(0,0.0-shapingTime,140.0-shapingTime); //This is a concession at 0.0
//fittingFunction->SetParLimits(1,1000.0, 1.0e9);
//fittingFunction->SetParLimits(3,0.0,30.0);
fittingFunction->FixParameter(4,shapingTime);
}
if (tPeak.size() == 2)
{
option = 2;
fittingFunction = new TF1("fittingFunction",fittingFunction7Uniform,firstBin,lastBin,5);
const double timeShift = tPeak[1] - shapingTime;
const double scalingFactor = TMath::Max(adcPeak[1] /0.015, 1000.0);
const double Q = TMath::Max(qAdc[0],0.0);
const double sigma = 10.0;
fittingFunction->SetParameters(timeShift,scalingFactor,Q,sigma,shapingTime);
//fittingFunction->SetParLimits(2,0.0,1000.0);
//fittingFunction->SetParLimits(0,0.0-shapingTime,140.0-shapingTime); //This is a concession at 0.0
//fittingFunction->SetParLimits(1,1000.0, 1.0e9);
//fittingFunction->SetParLimits(3,0.0,30.0);
fittingFunction->FixParameter(4,shapingTime);
}
if (tPeak.size() == 3)
{
option = 3;
fittingFunction = new TF1("fittingFunction", fittingFunction8,firstBin,lastBin, 6);
const double timeShift0 = tPeak[1] - shapingTime;
const double scalingFactor0 = TMath::Max(adcPeak[1] /0.015, 1000.0);
const double Q = TMath::Max(qAdc[0],0.0);
const double timeShift1 = tPeak[2] - tPeak[1];
const double scalingFactor1 = TMath::Max(adcPeak[2] /0.015, 1000.0);
fittingFunction->SetParameters(timeShift0, scalingFactor0, Q, timeShift1, scalingFactor1, shapingTime);
//fittingFunction->SetParLimits(0,0.0-shapingTime,80.0-shapingTime);
//fittingFunction->SetParLimits(1,1000.0,1.0e9);
//fittingFunction->SetParLimits(3,25.0,80.0);
//fittingFunction->SetParLimits(4,1000.0,1.0e9);
fittingFunction->FixParameter(5,shapingTime);
}
}
else
{
if (tPeak.size() == 1)
{
option = 4;
fittingFunction = new TF1("fittingFunction",fittingFunction4Uniform,firstBin,lastBin,6);
const double timeShift = tPeak[0] - shapingTime;
const double scalingFactor = TMath::Max(adcPeak[0] /0.015, 1000.0);
const double verticalShift = 0.5 * (qAdc[0] + qAdc[1]);
const double sigma = 10.0;
const double truncationLevel = 1024.0 - 64.0;
fittingFunction->SetParameters(timeShift,scalingFactor,verticalShift,sigma,truncationLevel,shapingTime);
//fittingFunction->SetParLimits(0,20.0-shapingTime,140.0-shapingTime);
//fittingFunction->SetParLimits(1,1000.0, 1.0e9);
//fittingFunction->SetParLimits(2,-20.0,1000.0); // This bound currently works but is questionable
//fittingFunction->SetParLimits(3,0.0,30.0);
//fittingFunction->FixParameter(4,1024.0-64.0);
fittingFunction->FixParameter(5,shapingTime);
}
if (tPeak.size() == 2)
{
option = 5;
fittingFunction = new TF1("fittingFunction",fittingFunction10,firstBin,lastBin,6);
const double timeShift0 = tPeak[0] - shapingTime;
const double scalingFactor0 = TMath::Max(adcPeak[0] /0.015, 1000.0);
const double verticalShift = 0.5 * (qAdc[0] + qAdc[1]);
const double timeShift1 = tPeak[1] - tPeak[0];
const double scalingFactor1 = TMath::Max(adcPeak[1] /0.015, 1000.0);
fittingFunction->SetParameters(timeShift0, scalingFactor0, verticalShift, timeShift1, scalingFactor1, shapingTime);
//fittingFunction->SetParLimits(0,20.0-shapingTime,100.0-shapingTime);
//fittingFunction->SetParLimits(1,1000.0,1.0e9);
//fittingFunction->SetParLimits(2,-20.0,1000.0); // This bound currently works but is questionable
//fittingFunction->SetParLimits(3,25.0,100.0);
//fittingFunction->SetParLimits(4,1000.0,1.0e9);
//fittingFunction->FixParameter(5,shapingTime);
}
}
graph->Fit(fittingFunction,"QN");
graph->Draw("A*");
fittingFunction->Draw("same");
//}
}