-
Notifications
You must be signed in to change notification settings - Fork 2
/
noise.cpp
177 lines (158 loc) · 7.61 KB
/
noise.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
#include <array>
#include <chrono>
#include <iostream>
#include <fstream>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <utility>
#include <cerrno>
#include "TF1.h"
#include "TFile.h"
#include "TH1D.h"
#include "TH2D.h"
#include "TStyle.h"
#include "TTree.h"
#include "ChargeData.h"
#include "ChargeHits.h"
#include "NoiseFilter.h"
#include "PrincipalComponentsCluster.h"
#include "RunParams.h"
#include "KalmanFit.h"
void analyseNoise(const pixy_roimux::ChargeData &data,
std::ofstream &noiseParamsFile,
const pixy_roimux::RunParams &runParams,
const std::string &histoNamePrefix) {
const unsigned nAdcBits = 14;
const std::array<unsigned, 2> nChannels{runParams.getNPixels(), runParams.getNRois()};
const std::array<std::set<unsigned>, 2> blindedChannels = {std::set<unsigned>{9}, std::set<unsigned>{}};
std::array<TH2D, 2> channelNoiseHistos;
std::array<TH1D, 2> noiseHistos;
for (unsigned channelType = 0; channelType < 2; ++channelType) {
std::ostringstream histoName;
histoName << histoNamePrefix << pixy_roimux::ChannelTypeString.at(channelType) << "ChannelNoiseHisto";
channelNoiseHistos.at(channelType) = TH2D(histoName.str().c_str(), histoName.str().c_str(), (1 << nAdcBits),
- (1 << (nAdcBits - 1)), (1 << (nAdcBits - 1)),
nChannels.at(channelType), 0, nChannels.at(channelType));
histoName.str("");
histoName << histoNamePrefix << pixy_roimux::ChannelTypeString.at(channelType) << "NoiseHisto";
noiseHistos.at(channelType) = TH1D(histoName.str().c_str(), histoName.str().c_str(), (1 << nAdcBits),
- (1 << (nAdcBits - 1)), (1 << (nAdcBits - 1)));
}
auto eventId = data.getEventIds().cbegin();
for (const auto &event : data.getReadoutHistos()) {
std::cout << "Analysing event number " << *eventId << "...\n";
for (unsigned channelType = 0; channelType < 2; ++channelType) {
for (unsigned channel = 0; channel < nChannels.at(channelType); ++channel) {
const bool blinded = (blindedChannels.at(channelType).find(channel)
!= blindedChannels.at(channelType).cend());
for (unsigned sample = 0; sample < runParams.getNSamples(); ++sample) {
double amplitude = event.at(channelType).GetBinContent((sample + 1), (channel + 1));
channelNoiseHistos.at(channelType).Fill(amplitude, channel);
if (!blinded) {
noiseHistos.at(channelType).Fill(amplitude);
}
}
}
}
++eventId;
}
for (unsigned channelType = 0; channelType < 2; ++channelType) {
channelNoiseHistos.at(channelType).Write();
noiseHistos.at(channelType).Write();
}
for (unsigned channelType = 0; channelType < 2; ++channelType){
for (unsigned channel = 0; channel <= nChannels.at(channelType); ++channel) {
std::ostringstream channelString;
channelString << pixy_roimux::ChannelTypeString.at(channelType) << ' ';
std::unique_ptr<TH1D> histo;
bool blinded = false;
if (channel == nChannels.at(channelType)) {
channelString << 'S';
histo = std::unique_ptr<TH1D>(dynamic_cast<TH1D*>(noiseHistos.at(channelType).Clone()));
}
else {
channelString << channel;
histo = std::unique_ptr<TH1D>(channelNoiseHistos.at(channelType).ProjectionX(
"channelHisto", (channel + 1), (channel + 1)));
blinded = (blindedChannels.at(channelType).find(channel) != blindedChannels.at(channelType).cend());
}
const double mean = histo->GetMean();
const double stdDev = histo->GetStdDev();
TF1 gauss("gauss", "gaus");
//histo->GetXaxis()->SetRangeUser((mean - stdDev), (mean + stdDev));
histo->Fit(&gauss, "QN");
noiseParamsFile << channelString.str() << ":\t" << mean << ", " << stdDev << ", "
<< gauss.GetParameter(1) << ", " << gauss.GetParameter(2);
if (blinded) {
noiseParamsFile << "\tBLINDED";
}
noiseParamsFile << std::endl;
}
}
}
int main(int argc, char** argv) {
// Start time point for timer.
auto clkStart = std::chrono::high_resolution_clock::now();
if (argc < 4) {
std::cerr << "Usage: " << program_invocation_short_name << " runParamsFileName dataFileName outputPath [nEvents]" << std::endl;
exit(1);
}
const std::string runParamsFileName(argv[1]);
const std::string dataFileName(argv[2]);
const std::string outputPath = std::string(argv[3]) + '/';
const unsigned nEvents = ((argc > 4) ? static_cast<unsigned>(std::stoul(argv[4])) : 0);
const unsigned subrunId = 0;
std::vector<unsigned> eventIds;
for (unsigned event = 0; event < nEvents; ++event) {
eventIds.emplace_back(event);
}
gStyle->SetPalette(kViridis);
gStyle->SetNumberContours(255);
// Create the VIPER runParams containing all the needed run parameters.
const pixy_roimux::RunParams runParams(runParamsFileName);
// Load events directly from ROOT file.
std::cout << "Extracting chargeData...\n";
pixy_roimux::ChargeData chargeData = (eventIds.empty()
? pixy_roimux::ChargeData(dataFileName, subrunId, runParams)
: pixy_roimux::ChargeData(dataFileName, eventIds, subrunId, runParams));
std::ostringstream rootFileName;
rootFileName << outputPath << "noise.root";
TFile rootFile(rootFileName.str().c_str(), "RECREATE");
if (!rootFile.IsOpen()) {
std::cerr << "ERROR: Failed to open ROOT file " << rootFileName.str() << '!' << std::endl;
exit(1);
}
std::ostringstream noiseParamFileName;
noiseParamFileName << outputPath << "noiseParams.csv";
std::ofstream noiseParamsFile(noiseParamFileName.str(), std::ofstream::out);
noiseParamsFile << "Noise Parameters" << std::endl;
noiseParamsFile << "================" << std::endl;
noiseParamsFile << "Histogram Mean, Histogram Standard Deviation, Gaussian Mu, Gaussian Sigma" << std::endl;
std::cout << "Analysing noise of unfiltered data...\n";
noiseParamsFile << std::endl;
noiseParamsFile << "Unfiltered data:" << std::endl;
noiseParamsFile << "----------------" << std::endl;
analyseNoise(chargeData, noiseParamsFile, runParams, "unfiltered");
// Noise filter
std::cout << "Filtering chargeData...\n";
pixy_roimux::NoiseFilter noiseFilter(runParams);
noiseFilter.filterData(chargeData);
std::cout << "Analysing noise of filtered data...\n";
noiseParamsFile << std::endl;
noiseParamsFile << "Filtered data:" << std::endl;
noiseParamsFile << "--------------" << std::endl;
analyseNoise(chargeData, noiseParamsFile, runParams, "filtered");
rootFile.Close();
noiseParamsFile.close();
std::cout << "Done.\n";
// Stop time point for timer.
auto clkStop = std::chrono::high_resolution_clock::now();
// Calculate difference between timer start and stop.
auto clkDuration = std::chrono::duration_cast<std::chrono::milliseconds>(clkStop - clkStart);
std::cout << "Elapsed time for " << chargeData.getReadoutHistos().size() << " processed events is: "
<< clkDuration.count() << "ms\n";
return 0;
}