Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
calc_thresh.cxx
Go to the documentation of this file.
1// Author Eric Pooser, pooser@jlab.org
2// Script to calculate pedestal values on crate, slot, channel basis
3
4#include <iostream>
5#include <fstream>
6#include <cstdlib>
7#include <cmath>
8#include <numeric>
9#include "TH1.h"
10#include "TFile.h"
11#include "TF1.h"
12
13#define SLOTMIN 1
14#define NUMSLOTS 22
15#define NADCCHAN 16
16#define NPOINTS 100
17#define NSIGMAFIT 5
18#define NSIGMATHRESH 5
19
20using namespace std;
21
33
35
36 Int_t fadc_mode = 0;
37 // Acquire number of channels above baseline the thresholds should be
38 if (fadc_mode == 0) {
39 cout << "\nEnter The Mode of The F250's: ";
40 cin >> fadc_mode;
41 if (fadc_mode <= 0) {
42 cerr << "...Invalid entry\n" << endl;
43 exit(EXIT_FAILURE);
44 }
45 }
46
47 Int_t nchan = 0;
48 // Acquire number of channels above baseline the thresholds should be
49 if (nchan == 0) {
50 cout << "\nEnter The Number of Channels Above Baseline the Threshold Should Be (40 recommended): ";
51 cin >> nchan;
52 if (nchan <= 0) {
53 cerr << "...Invalid entry\n" << endl;
54 exit(EXIT_FAILURE);
55 }
56 }
57
58 // Get the input file and declare the output file
59 rif = new TFile("fadc_data.root");
60 rof = new TFile("fadc_ped_data.root", "RECREATE", "FADC Pedestal Data");
61
62 // Declare the output text file
63 ofstream outputfile;
64
65 mode_dir = dynamic_cast <TDirectory*> (rof->Get(Form("mode_%d_data", fadc_mode)));
66 if(!mode_dir) {
67 mode_dir = rof->mkdir(Form("mode_%d_data", fadc_mode));
68 rof->cd(Form("/mode_%d_data", fadc_mode));
69 }
70 else rof->cd(Form("/mode_%d_data", fadc_mode));
71
72 for (UInt_t islot = SLOTMIN; islot < NUMSLOTS; islot++) {
73 if (islot == 11 || islot == 12) continue;
74
75 slot_dir[islot] = dynamic_cast <TDirectory*> (mode_dir->Get(Form("slot_%u", islot)));
76 if(!slot_dir[islot]) {
77 slot_dir[islot] = rof->mkdir(Form("mode_%d_data/slot_%u", fadc_mode, islot));
78 rof->cd(Form("/mode_%d_data/slot_%u", fadc_mode, islot));
79 }
80 else rof->cd(Form("/mode_%d_data/slot_%u", fadc_mode, islot));
81
82 for (UInt_t ichan = 0; ichan < NADCCHAN; ichan++) {
83
84 // Write pedestal histos and fits to rof
85 chan_dir[ichan] = dynamic_cast <TDirectory*> (slot_dir[islot]->Get(Form("chan_%u", ichan)));
86 if( !chan_dir[ichan] ) {
87 chan_dir[ichan] = rof->mkdir(Form("mode_%d_data/slot_%u/chan_%u", fadc_mode, islot, ichan));
88 }
89 rof->cd(Form("/mode_%d_data/slot_%u/chan_%u", fadc_mode, islot, ichan));
90
91 if( !h_pped[islot][ichan] )
92 h_pped[islot][ichan] = (TH1I*)(rif->Get(Form("mode_%d_data/slot_%u/chan_%u/h_pped", fadc_mode, islot, ichan)));
93 // Acquire the number of entries as a restriction for a good fit
94 if( h_pped[islot][ichan] )
95 nentries[islot][ichan] = h_pped[islot][ichan]->GetEntries();
96 // cout << "CHANNEL NO PED = " << ichan << ", HISTO PTR = "
97 // << h_pped[islot][ichan] << ", NENTRIES = " << nentries[islot][ichan] << endl;
98 if( h_pped[islot][ichan] && (nentries[islot][ichan] >= NPOINTS) ) {
99
100 //cout << "Found Pedestal Histo for Slot " << islot << ", Channel " << ichan << endl;
101 // Define the fit
102 init_gfit[islot][ichan] = new TF1(Form("init_fit_slot_%u_chan_%u", islot, ichan), "gaus(0)");
103 init_gfit[islot][ichan]->SetLineColor(2);
104
105 // Obtain intial fit parameters
106 Int_t init_max_bin = h_pped[islot][ichan]->GetMaximumBin();
107 Double_t init_max_val = h_pped[islot][ichan]->GetMaximum();
108 Double_t init_max_bin_center = h_pped[islot][ichan]->GetBinCenter(init_max_bin);
109
110 Int_t curr_bin = init_max_bin+1;
111 while (true) {
112 Double_t curr_val = h_pped[islot][ichan]->GetBinContent(curr_bin);
113 if (curr_val < init_max_val/2.) break;
114 ++curr_bin;
115 }
116 Double_t init_sigma = h_pped[islot][ichan]->GetBinCenter(curr_bin) - init_max_bin_center;
117
118 // Define the initial fit parameters and initialize the fit
119 init_max[islot][ichan] = init_max_val;
120 init_mean[islot][ichan] = init_max_bin_center;
121 init_stddev[islot][ichan] = init_sigma;
122 init_gfit[islot][ichan]->SetParameters(init_max[islot][ichan],
123 init_mean[islot][ichan],
124 init_stddev[islot][ichan]);
125
126 // Perform the initial fit over the full range of the histo
127 init_fr_low[islot][ichan] = init_mean[islot][ichan] - NSIGMAFIT*init_stddev[islot][ichan];
128 init_fr_high[islot][ichan] = init_mean[islot][ichan] + NSIGMAFIT*init_stddev[islot][ichan];
129 init_gfit[islot][ichan]->SetRange(init_fr_low[islot][ichan], init_fr_high[islot][ichan]);
130 h_pped[islot][ichan]->Fit(Form("init_fit_slot_%u_chan_%u", islot, ichan), "QR");
131 //h_pped[islot][ichan]->Fit(Form("init_fit_slot_%u_chan_%u", islot, ichan));
132 init_gfit[islot][ichan]->Draw();
133 // Declare and initialize the second and final fit
134 gfit[islot][ichan] = new TF1(Form("iter_fit_slot_%u_chan_%u", islot, ichan), "gaus(0)");
135 gfit[islot][ichan]->SetLineColor(4);
136 // Acquire the fit parameters from the first fit and initialize the second fit
137 iter_max[islot][ichan] = init_gfit[islot][ichan]->GetParameter(0);
138 iter_mean[islot][ichan] = init_gfit[islot][ichan]->GetParameter(1);
139 iter_stddev[islot][ichan] = init_gfit[islot][ichan]->GetParameter(2);
140 gfit[islot][ichan]->SetParameters(iter_max[islot][ichan],
141 iter_mean[islot][ichan],
142 iter_stddev[islot][ichan]);
143 gfit[islot][ichan]->SetParNames("Amp.", "#mu (ADC)", "#sigma (ADC)");
144 // Fit over a mean +/- NSIGMAFIT*mean range
145 fr_low[islot][ichan] = iter_mean[islot][ichan] - NSIGMAFIT*iter_stddev[islot][ichan];
146 fr_high[islot][ichan] = iter_mean[islot][ichan] + NSIGMAFIT*iter_stddev[islot][ichan];
147 // cout << "MEAN = " << iter_mean[islot][ichan]
148 // << ", FR LOW = " << fr_low[islot][ichan]
149 // << ", FR HIGH = " << fr_high[islot][ichan] << endl;
150 gfit[islot][ichan]->SetRange(fr_low[islot][ichan], fr_high[islot][ichan]);
151 // Perform the fit and store the parameters
152 h_pped[islot][ichan]->Fit(Form("iter_fit_slot_%u_chan_%u", islot, ichan), "QR");
153 //h_pped[islot][ichan]->Fit(Form("iter_fit_slot_%u_chan_%u", islot, ichan), "R");
154 gfit[islot][ichan]->Draw("same");
155 // Acquire the final fit parameters
156 finl_max[islot][ichan] = gfit[islot][ichan]->GetParameter(0);
157 finl_mean[islot][ichan] = gfit[islot][ichan]->GetParameter(1);
158 finl_stddev[islot][ichan] = gfit[islot][ichan]->GetParameter(2);
159
160 long istddev = lround(finl_stddev[islot][ichan]);
161 long imean = lround(finl_mean[islot][ichan]);
162 if( istddev < 0 ) istddev = 0;
163 if( imean < 0 ) imean = 0;
164 if( NSIGMATHRESH * istddev < nchan )
165 threshold[islot][ichan] = imean + nchan;
166 else
167 threshold[islot][ichan] = imean + NSIGMATHRESH * istddev;
168 // cout << "Slot = " << islot << ", Channel = " << ichan
169 // << ", Mean = " << finl_mean[islot][ichan]
170 // << ", Threshold = " << threshold[islot][ichan] << endl;
171 // Acquire the final fit parameter errors
172 finl_max_err[islot][ichan] = gfit[islot][ichan]->GetParError(0);
173 finl_mean_err[islot][ichan] = gfit[islot][ichan]->GetParError(1);
174 finl_stddev_err[islot][ichan] = gfit[islot][ichan]->GetParError(2);
175
176 // Write histos and fits to root output file
177 h_pped[islot][ichan]->Write();
178 } // Histo object and nentries requirement
179 } // Channel loop
180 } // Slot loop
181
182 //rof->Write();
183
184 outputfile.open("thresholds.dat");
185 for (UInt_t islot = SLOTMIN; islot < NUMSLOTS; islot++) {
186 if (islot == 11 || islot == 12) continue;
187 outputfile << "slot=" << islot << endl;
188 for (UInt_t ichan = 0; ichan < NADCCHAN; ichan++)
189 if ( threshold[islot][ichan] == 0)
190 outputfile << 4095 << endl;
191 else
192 outputfile << threshold[islot][ichan] << endl;
193 } // Slot loop
194
195}
196
int Int_t
unsigned int UInt_t
double Double_t
char * Form(const char *fmt,...)
#define NPOINTS
void calc_thresh()
#define SLOTMIN
TF1 * gfit[NUMSLOTS][NADCCHAN]
Double_t init_fr_low[NUMSLOTS][NADCCHAN]
Double_t finl_mean_err[NUMSLOTS][NADCCHAN]
Double_t finl_stddev_err[NUMSLOTS][NADCCHAN]
Double_t finl_max[NUMSLOTS][NADCCHAN]
TH1I * h_pped[NUMSLOTS][NADCCHAN]
TF1 * init_gfit[NUMSLOTS][NADCCHAN]
Double_t finl_stddev[NUMSLOTS][NADCCHAN]
Double_t init_mean[NUMSLOTS][NADCCHAN]
TDirectory * chan_dir[NADCCHAN]
Double_t finl_max_err[NUMSLOTS][NADCCHAN]
Double_t iter_max[NUMSLOTS][NADCCHAN]
Double_t init_stddev[NUMSLOTS][NADCCHAN]
TDirectory * slot_dir[NUMSLOTS]
TFile * rof
#define NADCCHAN
UInt_t nentries[NUMSLOTS][NADCCHAN]
#define NSIGMAFIT
TDirectory * mode_dir
UInt_t threshold[NUMSLOTS][NADCCHAN]
Double_t fr_low[NUMSLOTS][NADCCHAN]
Double_t iter_mean[NUMSLOTS][NADCCHAN]
Double_t finl_mean[NUMSLOTS][NADCCHAN]
Double_t init_max[NUMSLOTS][NADCCHAN]
#define NUMSLOTS
#define NSIGMATHRESH
Double_t fr_high[NUMSLOTS][NADCCHAN]
TFile * rif
Double_t iter_stddev[NUMSLOTS][NADCCHAN]
Double_t init_fr_high[NUMSLOTS][NADCCHAN]
virtual void SetLineColor(Color_t lcolor)
Bool_t cd() override
T * Get(const char *namecycle)
TDirectory * mkdir(const char *name, const char *title="", Bool_t returnExistingDirectory=kFALSE) override
virtual TObject * Get(const char *namecycle)
virtual Double_t GetParameter(const TString &name) const
virtual Double_t GetParError(Int_t ipar) const
virtual void SetRange(Double_t xmin, Double_t xmax)
void Draw(Option_t *option="") override
virtual void SetParameters(const Double_t *params)
virtual void SetParNames(const char *name0="p0", const char *name1="p1", const char *name2="p2", const char *name3="p3", const char *name4="p4", const char *name5="p5", const char *name6="p6", const char *name7="p7", const char *name8="p8", const char *name9="p9", const char *name10="p10")
virtual Double_t GetBinCenter(Int_t bin) const
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
virtual Double_t GetEntries() const
virtual Int_t GetMaximumBin() const
virtual Double_t GetBinContent(Int_t bin) const
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
RVec< PromoteType< T > > lround(const RVec< T > &v)
STL namespace.