BOSS 7.1.1
BESIII Offline Software System
Loading...
Searching...
No Matches
xe/HadronCalibration.cc
Go to the documentation of this file.
1//********************************************************************************
2// This file is part of the Widget, a package for performing dE/dx calibration.
3//
4// Author: Jake Bennett
5// Date: July 8, 2015
6//
7// HadronCalibration is a program that performs the generation, simulation,
8// fitting, and correction tasks necessary for dE/dx calibration.
9//
10// For additional details, see the Widget document.
11//
12//********************************************************************************
13
14#include <fstream>
15#include <iostream>
16#include <string>
17#include <cmath>
18#include <vector>
19
20#include "TFile.h"
21#include "TTree.h"
22#include "TH1F.h"
23#include "TH2F.h"
24#include "TF1.h"
25#include "TString.h"
26#include "TCanvas.h"
27#include "TRandom.h"
28#include "TMath.h"
29#include "TFitter.h"
30#include "TLegend.h"
31#include "TApplication.h"
32
34
35int main( int argc, char* argv[] ) {
36
37 std::cout << std::endl << "*** Running the calibration ***" << std::endl << std::endl;
38
39 std::string configfile("");
40 TString inputfile("");
41 int part = -1;
42 std::string parfile("parameters.txt");
43 int ask = 1;
44
45 // ***************************
46 // Parse command line options
47 // ***************************
48
49 for (int i = 1; i < argc; i++){
50
51 std::string arg(argv[i]);
52
53 if (arg == "-c"){
54 if ((i+1 == argc) || (argv[i+1][0] == '-')) arg = "-h";
55 else configfile = argv[++i]; }
56 if (arg == "-f"){
57 if ((i+1 == argc) || (argv[i+1][0] == '-')) arg = "-h";
58 else inputfile = argv[++i]; }
59 if (arg == "-p"){
60 if ((i+1 == argc) || (argv[i+1][0] == '-')) arg = "-h";
61 else parfile = argv[++i]; }
62 if (arg == "-t"){
63 if ((i+1 == argc) || (argv[i+1][0] == '-')) arg = "-h";
64 else part = atoi(argv[++i]); }
65 if (arg == "-i"){
66 if ((i+1 == argc) || (argv[i+1][0] == '-')) arg = "-h";
67 else ask = atoi(argv[++i]); }
68 if (arg == "-h"){
69 std::cout << std::endl << " Usage for: " << argv[0] << std::endl << std::endl;
70 std::cout << "\t -c <file>\t Configuration file" << std::endl;
71 std::cout << "\t -f <file>\t Input file" << std::endl;
72 std::cout << "\t -p <file>\t Curve parameter file" << std::endl;
73 std::cout << "\t -t <int>\t Particle type (-1 = all)" << std::endl;
74 std::cout << "\t -i <int>\t Ask before each step? (0 = no, 1 = yes)" << std::endl;
75 exit(1);
76 }
77 }
78
79 if( configfile == "" && inputfile == "" ){
80 std::cout << "ERROR: No input file was given..." << std::endl;
81 return 1;
82 }
83
84 // Hesitate after each if ask flag is true
85 std::string ready("y");
86
87 // Create a calibration interface
88 HadronInterface widget;
89
90 std::vector< TString > types;
91 types.push_back("pion");
92 types.push_back("kaon");
93 types.push_back("proton");
94 types.push_back("muon");
95 types.push_back("electron");
96
97 // ---------------------------------------------------------------------------
98 // 0. Set the parameters either from a config file or use the defaults
99
100 if( configfile != "" ){
101 widget.SetupFromConfigFile(configfile);
102 widget.PrintParameters();
103 }
104 else{
105 std::cout << "No configuration file provided, ";
106 std::cout << "are you sure you want to use the default values (y/n)? ";
107 std::cin >> ready; std::cout << std::endl;
108 if( ready != "y" ) return 1;
109
110 int bgbins[5];
111 double bgmax[5];
112 double bgmin[5];
113
114 // BESIII XYZ 2014 DATA
115 bgbins[0] = 30;
116 bgmax[0] = 2.12/Widget::mpion;
117 bgmin[0] = 0.12/Widget::mpion;
118 bgbins[1] = 10;
119 bgmax[1] = 1.8/Widget::mkaon;
120 bgmin[1] = 0.35/Widget::mkaon;
121 bgbins[2] = 10;
122 bgmax[2] = 1.7/Widget::mproton;
123 bgmin[2] = 0.5/Widget::mproton;
124 bgbins[3] = 15;
125 bgmax[3] = 1.92/Widget::mmuon;
126 bgmin[3] = 1.76/Widget::mmuon;
127 bgbins[4] = 30;
128 bgmax[4] = 2.3/Widget::melectron;
129 bgmin[4] = 0.3/Widget::melectron;
130
131 int cosbins[5];
132 double cosmax[5];
133 double cosmin[5];
134 for( int i = 0; i < 4; ++i ){
135 cosbins[i] = 18;
136 cosmax[i] = 0.93;
137 cosmin[i] = 0.0;
138 }
139 cosbins[4] = 18;
140 cosmax[4] = 0.93;
141 cosmin[4] = 0.5;
142
143 // add particles
144 for( int i = 0; i < 5; ++i ){
145 if( inputfile != "" )
146 widget.AddParticle(types[i],inputfile+types[i]+".root",0.0,
147 bgbins[i],bgmax[i],bgmin[i],
148 cosbins[i],cosmax[i],cosmin[i]);
149 else
150 widget.AddParticle(types[i],"widget."+types[i]+".root",0.0,
151 bgbins[i],bgmax[i],bgmin[i],
152 cosbins[i],cosmax[i],cosmin[i]);
153 }
154 }
155
156 // set the file that holds the calibration constants
157 widget.SetParamFile(parfile);
158 // ---------------------------------------------------------------------------
159
160 // redirect output to log file, but save a pointer to the old
161 // buffer associated with std::cout and restore it for some output
162 std::streambuf *coutbuf = std::cout.rdbuf();
163 std::ofstream out("widget.log");
164
165 // ---------------------------------------------------------------------------
166 // 1. Make histograms for quality validation and monitoring
167
168 if( ask == 1 ){
169 std::cout << "Do you want to make quality plots (y/n)? ";
170 std::cin >> ready; std::cout << std::endl;
171 }
172 if( ask == 0 || ready == "y" )
173 widget.QualityCheck( "widget.quality.root" );
174 // ---------------------------------------------------------------------------
175
176 // ---------------------------------------------------------------------------
177 // 2. Do fits of truncated means in bins of beta-gamma and cos(theta)
178 // Do not include the electron sample here
179
180 if( ask == 1 ){
181 std::cout << "Do you want to fit in bins of beta-gamma and cos(theta) (y/n)? ";
182 std::cin >> ready; std::cout << std::endl;
183 }
184 if( ask == 0 || ready == "y" ){
185 std::cout.rdbuf( out.rdbuf() );
186 if( part == -1 ) // using pre-defined sample (default)
187 widget.PrepareSample( "widget.uncorrected.root", false, -1 );
188 else if( part == -2 ){ // no samples pre-defined so use fake samples
189 for( int i = 0; i < 4; ++i ){
190 widget.PrepareSample( "widget.uncorrected.root", false, i );
191 std::cout.rdbuf( out.rdbuf() );
192 std::cout << "Ready to move on to the next particle (y/n)? ";
193 std::cin >> ready; std::cout << std::endl;
194 std::cout.rdbuf( coutbuf );
195 if( ready != "y" ) return 1;
196 }
197 return 1; // short circuit here for now
198 }
199 else{ // supplying individual sample for diagnostic purposes
200 widget.PrepareSample( "widget.uncorrected.root", false, part );
201 return 1; // short circuit here for now
202 }
203 std::cout.rdbuf( coutbuf );
204 }
205 // ---------------------------------------------------------------------------
206
207 // ---------------------------------------------------------------------------
208 // 3. Determine the calibration constants for the hadron correction.
209 // SaturationCorrection does nothing for MC samples.
210
211 if( ask == 1 ){
212 std::cout << "Do you want to do to the hadron correction (y/n)? ";
213 std::cin >> ready; std::cout << std::endl;
214 }
215 if( ask == 0 || ready == "y" ){
216 std::cout.rdbuf( out.rdbuf() );
217 widget.SaturationCorrection( "widget.uncorrected.root", "sat-pars.txt" );
218 widget.PrepareSample( "widget.corrected.root", true, part );
219 std::cout.rdbuf( coutbuf );
220 }
221
222 // ---------------------------------------------------------------------------
223 // 4. Repeat the 2D correction using the previous results as a seed
224 // SaturationCorrection does nothing for MC samples.
225
226 if( ask == 1 ){
227 std::cout << "Do you want to repeat to the hadron correction (y/n)? ";
228 std::cin >> ready; std::cout << std::endl;
229 }
230 if( ask == 0 || ready == "y" ){
231 std::cout.rdbuf( out.rdbuf() );
232 widget.SaturationCorrection( "widget.uncorrected.root", "sat-pars.fit.txt" );
233 widget.PrepareSample( "widget.corrected.root", true, part );
234 std::cout.rdbuf( coutbuf );
235 }
236 // ---------------------------------------------------------------------------
237
238 // ---------------------------------------------------------------------------
239 // 5. Fit the corrected mean values in bins of beta-gamma
240
241 if( ask == 1 ){
242 std::cout << "Do you want to perform the fits in bins of beta-gamma (y/n)? ";
243 std::cin >> ready; std::cout << std::endl;
244 }
245 if( ask == 0 || ready == "y" ){
246 std::cout.rdbuf( out.rdbuf() );
247 widget.PrepareResults("widget.corrected_bgbins.root",true);
248 widget.SetParamFile(parfile);
249 std::cout.rdbuf( coutbuf );
250 }
251 // ---------------------------------------------------------------------------
252
253 // ---------------------------------------------------------------------------
254 // 6. Fit the means in bins of beta-gamma and use the results to
255 // fit the beta-gamma curve and determine the curve parameters
256 if( ask == 1 ){
257 std::cout << "Do you want to fit the beta-gamma curve (y/n)? ";
258 std::cin >> ready; std::cout << std::endl;
259 }
260 if( ask == 0 || ready == "y" ){
261 std::cout.rdbuf( out.rdbuf() );
262 widget.BetaGammaFits(types,"widget.corrected_bgbins.root");
263 widget.SigmaFits("widget.corrected.root");
264 widget.SigmaFits("widget.corrected.root");
265 widget.SigmaFits("widget.corrected.root");
266 std::cout.rdbuf( coutbuf );
267 }
268 // ---------------------------------------------------------------------------
269
270 // ---------------------------------------------------------------------------
271 // 6b. Make some plots
272 if( ask == 1 ){
273 std::cout << "Do you want to plot the beta-gamma curve (y/n)? ";
274 std::cin >> ready; std::cout << std::endl;
275 }
276 if( ask == 0 || ready == "y" ){
277 std::cout.rdbuf( out.rdbuf() );
278 widget.PlotBGCurve(types,"widget.corrected_bgbins.root");
279 std::cout.rdbuf( coutbuf );
280 }
281 // ---------------------------------------------------------------------------
282
283 // ---------------------------------------------------------------------------
284 // 7. Fit the corrected mean values in bins of beta-gamma and repeat
285 // the fits for the beta-gamma and sigma curves
286
287 if( ask == 1 ){
288 std::cout << "Do you want to repeat the fits in bins of beta-gamma (y/n)? ";
289 std::cin >> ready; std::cout << std::endl;
290 }
291 if( ask == 0 || ready == "y" ){
292 std::cout.rdbuf( out.rdbuf() );
293 widget.PrepareResults("widget.corrected_bgbins.root",true);
294 widget.BetaGammaFits(types,"widget.corrected_bgbins.root");
295 widget.SigmaFits("widget.corrected.root");
296 widget.PrepareResults("widget.corrected_bgbins.root",true);
297 widget.BetaGammaFits(types,"widget.corrected_bgbins.root");
298 std::cout.rdbuf( coutbuf );
299 }
300 // ---------------------------------------------------------------------------
301}
double arg(const EvtComplex &c)
void BetaGammaFits(std::vector< TString > particles, TString filename)
void SetParamFile(std::string paramfile)
void QualityCheck(TString outfilename)
void PrepareResults(TString outfilename, bool correct)
void PrepareSample(TString outfilename, bool correct, int particle)
void SaturationCorrection(TString prepfilename, std::string parfile)
void PlotBGCurve(std::vector< TString > particles, TString filename)
void SetupFromConfigFile(std::string configfile)
void AddParticle(TString particle, TString filename, int nevents, int bgbins, double upperbg, double lowerbg, int cosbins, double uppercos, double lowercos)
void SigmaFits(TString filename)
int main()