sipm-characterisation 0.1.0
SiPM characterisation for ePIC — IV/DCR/gain, laser, readout, irradiation
Loading...
Searching...
No Matches
finetune.h
Go to the documentation of this file.
1#pragma once
2#include "alcor.h"
3
11
12// Addition of fine tune class
13namespace alcor
14{
15 // [0] Slope, [1] Intercept
16 typedef std::array<float, 2> calibration_unit;
17
18 // Class for the fine calibration
20 {
21 public:
22 // --- Variables ---
23 // --- cindex: tdc + eo_channel*4 + chip*4*32
24 // calibration_scheme[device][cindex] [0] Intercept
25 // [1] Slope
26 const std::vector<std::string> calibration_csv_fields = {"device", "cindex", "Intercept", "Slope"};
27 std::map<std::string, std::map<int, calibration_unit>> calibration_database;
28
29 // --- Generators ---
32
33 // --- Methods ---
34 // --- Getters
35 float get_intercept(std::string device, int cindex) { return calibration_database[device][cindex][0]; }
36 float get_slope(std::string device, int cindex) { return calibration_database[device][cindex][1]; }
37 // --- Setters
39 void set_slope(std::string device, int cindex, float slope) { calibration_database[device][cindex][1] = slope; }
40 // --- Calculate phase
41 float get_phase(std::string device, int cindex, float fine) { return fine * get_slope(device, cindex) + get_intercept(device, cindex); }
42 // --- Calculate correction parameters from pulsed data
44 void calculate_correction_from_pulser(std::string filename, std::string device, int cindex);
45 // --- I/O calibration scheme from file
46 void read_calibration(std::string filename);
47 void write_calibration(std::string filename);
48
49 private:
50 };
51}
52
53std::map<int, std::array<float, 2>> get_guesstimate_correction_from_pulser(Tree *data_tree, alcor::t_hit &current_hit, std::vector<int> cindex_target = {0, 1, 2, 3})
54{
55 // Run on data tree
56 std::map<int, std::pair<int, std::vector<alcor::t_hit>>> calibration_map;
57 std::map<int, std::array<float, 2>> result_map;
58 for (int iev = 0; iev < nev; ++iev)
59 {
60 data_tree->GetEntry(iev);
61 auto current_cindex = current_hit.tdc + current_hit.channel * 4 + ((int)(current_hit.fifo / 4)) * 4 * 32;
62 if (!std::find(cindex_target.begin(), cindex_target.end(), current_cindex) != cindex_target.end())
63 continue;
65 }
66 return result_map;
67}
68
70{
71 // Load pulsed signals from data file
75 auto nev = data_tree->GetEntries();
76
77 // Minimisation function
78 auto periodic_sigma_chi2 = [&](const double *parameters)
79 {
80 // Define result
81 double current_chi2 = 0.;
82
83 // Do the analysis spill by spill
84 bool has_first_hit_in_spill = false;
86
87 // Run on data tree
88 for (int iev = 0; iev < nev; ++iev)
89 {
90 data_tree->GetEntry(iev);
91
92 // Start of spill reset
93 if (current_hit.type == start_spill)
94 {
96 continue;
97 }
98
99 // Operate only if alcro hit events
100 if (current_hit.type != alcor_hit)
101 continue;
102
103 // Select the requested channel
104 if (current_hit.column * 4 + current_hit.pixel != (int)(cindex / 4))
105 continue;
106
107 // Calculate corrected time
108 int coarse = current_hit.coarse + alcor::rollover_to_coarse * current_hit.rollover;
110 current_hit.time = (double)coarse - current_correction;
111
114 {
117 continue;
118 }
119
120 // Compute the delta of times
121 double current_delta = current_hit.time - previous_hit.time - parameters[8];
122
123 // this is a safety measure, because sometimes there is some crap
124 if (fabs(coarse - (previous_hit.coarse + alcor::rollover_to_coarse * previous_hit.rollover) - parameters[8]) < 5)
125 {
128 }
129
130 // Assign current to previous hit and loop continues
132 }
133
134 return current_chi2;
135 };
136
137 ROOT::Math::Functor minimisation_function(periodic_sigma_chi2, 9);
138 ROOT::Fit::Fitter fitter;
139 double pStart[9] = {0.5, 0.5, 0.5, 0.5, 0.0156, 0.0156, 0.0156, 0.0156, 320.};
141 fitter.Config().ParSettings(0).SetName("off_0");
142 fitter.Config().ParSettings(1).SetName("off_1");
143 fitter.Config().ParSettings(2).SetName("off_2");
144 fitter.Config().ParSettings(3).SetName("off_3");
145 fitter.Config().ParSettings(4).SetName("iif_0");
146 fitter.Config().ParSettings(5).SetName("iif_1");
147 fitter.Config().ParSettings(6).SetName("iif_2");
148 fitter.Config().ParSettings(7).SetName("iif_3");
149 fitter.Config().ParSettings(8).SetName("period");
150 fitter.Config().ParSettings(0).Fix();
151
152 bool ok = fitter.FitFCN();
153 auto result = fitter.Result();
154 result.Print(std::cout);
155 double par[9], pare[9];
156 auto hParam = new TH1F("hParam", "", 9, 0., 9.);
157 for (int ipar = 0; ipar < 9; ++ipar)
158 {
159 par[ipar] = result.Parameter(ipar);
160 pare[ipar] = result.ParError(ipar);
161 }
162};
163
165{
166 std::ifstream data_stream(filename);
167 std::string current_line;
168 while (std::getline(data_stream, current_line))
169 {
170 // Skip comment characters
171 if (current_line[0] == '#' || current_line[0] == ' ')
172 continue;
173 // Read database
174 std::stringstream string_in_stream(current_line);
175 std::map<std::string, std::string> data_by_field;
176 std::string current_data;
178 {
181 }
182 this->set_intercept(data_by_field["device"], std::stoi(data_by_field["cindex"]), std::stof(data_by_field["Intercept"]));
183 this->set_slope(data_by_field["device"], std::stoi(data_by_field["cindex"]), std::stof(data_by_field["Slope"]));
184 }
185};
186
188{
189 std::ofstream data_stream(filename);
190 data_stream << "#\tCalibration file for Fine Tune\n";
191 auto current_time = std::chrono::system_clock::now();
192 std::time_t current_time_stamp = std::chrono::system_clock::to_time_t(current_time);
193 data_stream << "#\tGenerated on " << std::ctime(&current_time_stamp);
194 data_stream << "#\t## Device ## Calibration index ## Intercept ## Slope ##\n";
195 for (auto [device, calibration_of_device] : calibration_database)
196 {
198 {
199 data_stream << device << "\t" << cindex << "\t" << current_calibration_unit[0] << "\t" << current_calibration_unit[1] << "\n";
200 }
201 }
202};
Definition finetune.h:20
void calculate_correction_from_pulser(std::string filename, std::string device, int cindex)
Definition finetune.h:69
float get_slope(std::string device, int cindex)
Definition finetune.h:36
finecalibration()
Definition finetune.h:30
float get_phase(std::string device, int cindex, float fine)
Definition finetune.h:41
float get_intercept(std::string device, int cindex)
Definition finetune.h:35
std::map< std::string, std::map< int, calibration_unit > > calibration_database
Definition finetune.h:27
void write_calibration(std::string filename)
Definition finetune.h:187
std::vector< float > get_guesstimate_correction_from_pulser(alcor::t_hit previous_hit, alcor::t_hit current_hit)
void read_calibration(std::string filename)
Definition finetune.h:164
void set_intercept(std::string device, int cindex, float intercept)
Definition finetune.h:38
void set_slope(std::string device, int cindex, float slope)
Definition finetune.h:39
finecalibration(std::string filename)
Definition finetune.h:31
const std::vector< std::string > calibration_csv_fields
Definition finetune.h:26
@ alcor_hit
Definition deltat.C:17
@ start_spill
Definition deltat.C:17
type_t
Definition fine_analysis.C:32
TH2_Type * build_fine_tune_raw_histogram(std::vector< TString > kInputFileNames, TString kRunTag, TString kOutputFileName, bool kRecalculate)
Functions -------------------------------------------------------------------------------------------...
Definition fine_analysis.h:80
std::map< int, std::array< float, 2 > > get_guesstimate_correction_from_pulser(Tree *data_tree, alcor::t_hit &current_hit, std::vector< int > cindex_target={0, 1, 2, 3})
Definition finetune.h:53
@ alcor_hit
Definition finetune.h:6
@ end_spill
Definition finetune.h:9
@ start_spill
Definition finetune.h:8
@ trigger_tag
Definition finetune.h:7
Definition alcor.h:5
std::array< float, 2 > calibration_unit
Definition finetune.h:16
const int rollover_to_coarse
— — Rollover
Definition alcor.h:55
TTree * load_alcor_data_tree(const std::string infilename, t_hit &hit)
Definition alcor.h:68
Definition alcor.h:63