33int main(
int argc,
char** argv)
36 std::cout <<
"Usage: " << argv[0] <<
" <analysis tree file(s)>" << std::endl;
40 for(
int file_num = 1; file_num < argc; ++file_num) {
41 auto* f =
new TFile(argv[file_num]);
43 std::cout <<
"Reading from file: " << f << std::endl;
46 double calibrator_period = 10000.;
47 double tac_range = 50000.;
48 double ps_per_chan = 10.;
49 int first_TAC_channel = 75;
50 int number_of_peaks =
static_cast<int>(tac_range / calibrator_period);
54 inname = argv[file_num];
56 num.append(inname, inname.length() - 14, 9);
57 std::string calfile =
"TAC_calibration_run" + num +
".cal";
61 auto* tree =
static_cast<TTree*
>(f->Get(
"AnalysisTree"));
65 TLaBr* labr =
nullptr;
69 if(tree->FindBranch(
"TLaBr") !=
nullptr) {
70 tree->SetBranchAddress(
"TLaBr", &labr);
72 if(tree->FindBranch(
"TZeroDegree") !=
nullptr) {
73 tree->SetBranchAddress(
"TZeroDegree", &zds);
75 if(tree->FindBranch(
"TTAC") ==
nullptr) {
76 std::cout <<
"Exiting the program because there are no TACs to calibrate " << std::endl;
79 tree->SetBranchAddress(
"TTAC", &tac);
82 std::array<TH1F*, 8> raw_tac_calibrator = {};
83 for(
int i = 0; i < 8; i++) {
84 raw_tac_calibrator[i] =
new TH1F(Form(
"raw_TAC_calibrator%d", i), Form(
"raw_TAC_calibrator%d", i),
static_cast<int>(tac_range / ps_per_chan), 0, tac_range);
85 list.Add(raw_tac_calibrator[i]);
88 for(
int i = 0; i < tree->GetEntries(); i++) {
94 if(histnum >= 0 && histnum <= 7) {
96 raw_tac_calibrator[histnum]->Fill(charge);
102 std::array<TH1F*, 8> calibration_hist;
103 for(
int i = 0; i < 8; i++) {
104 calibration_hist[i] =
new TH1F(Form(
"calibration_hist%d", i), Form(
"calibration_hist%d", i),
static_cast<Int_t
>(tac_range / ps_per_chan), 0, tac_range);
105 list.Add(calibration_hist[i]);
108 for(
int j = 0; j <= 7; j++) {
110 spec.Search(raw_tac_calibrator[j], 1,
"", 0.10);
111 Int_t nfound = spec.Search(raw_tac_calibrator[j], 1,
"", 0.10);
112 if(nfound > number_of_peaks) {
113 std::cout <<
" " << std::endl;
114 std::cout <<
"WARNING!!! Tspectrum found " << (nfound - number_of_peaks) <<
" too many peaks in TAC #" << j << std::endl;
115 nfound = number_of_peaks;
118 std::vector<double> vec;
120 for(
int i = 0; i < nfound; i++) { vec.at(i) = spec.GetPositionX()[i]; }
121 std::sort(vec.begin(), vec.end());
123 for(
int k = 0; k < nfound; k++) {
124 raw_tac_calibrator[j]->GetXaxis()->SetRangeUser((vec.at(k) - 100) / ps_per_chan, (vec.at(k) + 100) / ps_per_chan);
125 vec[k] = raw_tac_calibrator[j]->GetMean();
129 for(
int i = 0; i < nfound; i++) {
131 g.SetPoint(i, vec.at(i), i * calibrator_period);
132 calibration_hist[j]->SetBinContent(
static_cast<int>(vec.at(i) + 1), i * calibrator_period);
135 auto* fitfunc =
new TF1(
"fitfunc",
"[0] + [1]*x");
138 calibration_hist[j]->Fit(fitfunc,
"Q");
141 gm->SetFitFunction(fitfunc);
142 gm->SetChannel(j + first_TAC_channel);
144 gm->GetFitFunction()->SetParameter(0, 0);
146 gm->WriteToChannel();
153 std::cout <<
"" << std::endl;
154 std::cout <<
"--->>> cal file written to: " << calfile << std::endl;
155 std::cout <<
"" << std::endl;