BOSS 7.1.1
BESIII Offline Software System
Loading...
Searching...
No Matches
HadronInterface.cc
Go to the documentation of this file.
1#include "HadronInterface.h"
2
4 // set up a vector of particle names
5 m_types.push_back("pion");
6 m_types.push_back("kaon");
7 m_types.push_back("proton");
8 m_types.push_back("muon");
9 m_types.push_back("electron");
10
11 // if no files are specified, assume sample is BESIII MC
12 m_mcFlag = 1;
13 m_type = 0;
14
15 // set up the values in the map of particle names
16 for( int i = 0; i < 5; ++i ){
17 m_index[m_types[i]] = i;
18 }
19
20 // and masses
21 m_mass["pion"] = Widget::mpion;
22 m_mass["kaon"] = Widget::mkaon;
23 m_mass["proton"] = Widget::mproton;
24 m_mass["muon"] = Widget::mmuon;
25 m_mass["electron"] = Widget::melectron;
26
27 // initialize various parameters
28 for( int i = 0; i < 5; ++i ){
29 m_filenames[i] = "";
30 m_nevents[i] = 0;
31 m_bgbins[i] = 15;
32 m_upperbg[i] = 5.0;
33 m_lowerbg[i] = 0.1;
34 m_cosbins[i] = 18;
35 m_uppercos[i] = 0.93;
36 m_lowercos[i] = 0.0;
37 }
38
39 m_paramfile = "parameters.txt";
40}
41
42void
43HadronInterface::SetupFromConfigFile( std::string configfile ){
44
45 cout << "HadronInterface: setting up from " << configfile << endl;
46
47 std::ifstream input(configfile.c_str());
48
49 int type;
50 std::string varname, vartype, line;
51 while( std::getline(input, line) ){
52
53 // skip comment lines
54 line.erase( std::find( line.begin(), line.end(), '#' ), line.end() );
55 if( line.empty() ) continue;
56
57 // parse the lines in the configuration file
58 type = -1;
59 std::istringstream iss(line);
60 iss >> vartype;
61
62 // check for a mcFlag
63 if( vartype == "mcFlag" ){
64 iss >> m_mcFlag;
65 continue;
66 }
67 else if( vartype == "type" ){
68 iss >> m_type;
69 continue;
70 }
71 else if( vartype == "pion") // the first word in the line should be the particle type
72 type = 0;
73 else if( vartype == "kaon")
74 type = 1;
75 else if( vartype == "proton")
76 type = 2;
77 else if( vartype == "muon")
78 type = 3;
79 else if( vartype == "electron")
80 type = 4;
81 else{
82 cout << "HadronInterface: ERROR: unknown particle type " << vartype << " - this line will not be read" << endl;
83 continue;
84 }
85
86 // the next word should be the variable type
87 iss >> varname;
88 if( varname == "filename")
89 iss >> m_filenames[type];
90 else if( varname == "nevents")
91 iss >> m_nevents[type];
92 else if( varname == "bgbins")
93 iss >> m_bgbins[type];
94 else if( varname == "upperbg")
95 iss >> m_upperbg[type];
96 else if( varname == "lowerbg")
97 iss >> m_lowerbg[type];
98 else if( varname == "upperp"){
99 iss >> m_upperbg[type];
100 m_upperbg[type] = m_upperbg[type]/m_mass[m_types[type]];
101 }
102 else if( varname == "lowerp"){
103 iss >> m_lowerbg[type];
104 m_lowerbg[type] = m_lowerbg[type]/m_mass[m_types[type]];
105 }
106 else if( varname == "cosbins")
107 iss >> m_cosbins[type];
108 else if( varname == "uppercos")
109 iss >> m_uppercos[type];
110 else if( varname == "lowercos")
111 iss >> m_lowercos[type];
112 else{
113 cout << "HadronInterface: ERROR: unknown variable type " << vartype << " - this line will not be read" << endl;
114 continue;
115 }
116 }
117
118 cout << "HadronInterface: Done parsing configuration file..." << endl;
119
120 m_paramfile = "parameters.txt";
121}
122
123void
125
126 cout << endl << endl << "Printing parameters" << endl << endl;
127
128 for( int i = 0; i < 5; ++i ){
129 cout << m_types[i] << endl;
130 cout << "\tFilename: " << m_filenames[i] << endl;
131 cout << "\tBeta-gamma range: " <<
132 m_bgbins[i] << " bins, from " << m_upperbg[i] << " to " << m_lowerbg[i] << endl;
133 cout << "\tCos(theta) range: " <<
134 m_cosbins[i] << " bins, from " << m_uppercos[i] << " to " << m_lowercos[i] << endl;
135 }
136}
137
138void
139HadronInterface::SetParamFile( std::string paramfile ){
140 m_paramfile = paramfile;
141}
142
143void
144HadronInterface::AddParticle( TString particle, TString filename, int nevents, int bgbins, double upperbg, double lowerbg, int cosbins, double uppercos, double lowercos ){
145 int index = m_index[particle];
146
147 m_filenames[index] = filename;
148 m_nevents[index] = nevents;
149 m_bgbins[index] = bgbins;
150 m_upperbg[index] = upperbg;
151 m_lowerbg[index] = lowerbg;
152 m_cosbins[index] = cosbins;
153 m_uppercos[index] = uppercos;
154 m_lowercos[index] = lowercos;
155}
156
157void
158HadronInterface::QualityCheck( TString outfilename ) {
159
160 cout << "HadronInterface: making quality plots..." << endl;
161
162 std::vector<TString> filenames;
163 for( int i = 0; i < 5; ++i ){
164 filenames.push_back(m_filenames[i]);
165 }
166
167 WidgetQuality gq(m_type,m_upperbg,m_lowerbg);
168 gq.makeHistograms(filenames, outfilename);
169 gq.plotHistograms(outfilename);
170}
171
172void
173HadronInterface::GenerateSample( TString particle = "pion" ) {
174
175 cout << "HadronInterface: generating sample events..." << endl;
176
177 int index = m_index[particle];
178
179 // generate a sample of events
180 WidgetGenerator generator(m_nevents[index], m_upperbg[index], m_lowerbg[index]);
181 generator.generateEvents(m_filenames[index],m_mass[particle]);
182
183 cout << "\t generated " << m_nevents[index] << "..." << endl;
184}
185
186void
187HadronInterface::SimulateSample( TString infilename, TString particle ) {
188
189 cout << "HadronInterface: simulating the dE/dx response for sample events..." << endl;
190
191 int index = m_index[particle];
192
193 // simulate the dE/dx response for a sample of events
194 WidgetGenerator generator(m_nevents[index], m_upperbg[index], m_lowerbg[index]);
195 generator.simulateDedx(infilename,m_filenames[index],m_mass[particle]);
196
197 cout << "\t simulated " << m_nevents[index] << "..." << endl;
198}
199
200void
201HadronInterface::SimulateReconstruction( TString infilename, TString outfilename ) {
202
203 cout << "HadronInterface: simulating the dE/dx reconstruction..." << endl;
204
205 // simulate the dE/dx response for a sample of events
206 WidgetGenerator generator;
207 generator.simulateReconstruction(infilename,outfilename);
208
209 cout << "\t simulated reconstruction finished..." << endl;
210}
211
212// -------------------- HADRON CORRECTIONS --------------------
213
214
215/* Fit the truncated mean distributions in bins of \beta\gamma and cos(theta) and
216 save the means and widths in the output file. */
217
218void
219HadronInterface::PrepareSample( TString outfilename, bool correct, int particle ) {
220
221 cout << "HadronInterface: fitting in bins of beta-gamma and cos(theta)..." << endl;
222
223 TFile* outfile = new TFile(outfilename,"RECREATE");
224 if( particle == -1 ){
225 // make sure the samples exist
226 for( int i = 0; i < 5; ++i ){ // <---- adding electrons...
227 TFile* testfile = new TFile(m_filenames[i]);
228 if( testfile->GetListOfKeys()->IsEmpty() ){
229 cout << "\t No hadron sample for " << m_types[i] << "... " << endl;
230 continue;
231 }
232 testfile->Close();
233
234 // prepare the sample by fitting in bins of beta-gamma and cos(theta)
235 HadronPrep prep(m_filenames[i], m_mcFlag, m_type, m_bgbins[i], m_upperbg[i], m_lowerbg[i], m_cosbins[i], m_uppercos[i], m_lowercos[i]);
236 prep.bgCosThetaFits(m_types[i],outfile,correct,m_paramfile);
237 }
238 }
239 else{
240 // make sure the samples exist
241 TFile* testfile = new TFile(m_filenames[particle]);
242 if( testfile->GetListOfKeys()->IsEmpty() ){
243 cout << "\t No hadron sample for " << m_types[particle] << "... " << endl;
244 }
245 testfile->Close();
246
247 // prepare the sample by fitting in bins of beta-gamma and cos(theta)
248 HadronPrep prep(m_filenames[particle], m_mcFlag, m_type, m_bgbins[particle], m_upperbg[particle], m_lowerbg[particle], m_cosbins[particle], m_uppercos[particle], m_lowercos[particle]);
249 prep.bgCosThetaFits(m_types[particle],outfile,correct,m_paramfile);
250 }
251
252 outfile->Close();
253}
254
255
256/* Fit the truncated mean distributions in bins of \beta\gamma and save the means
257 and widths in the output file. */
258
259void
260HadronInterface::PrepareResults( TString outfilename, bool correct ) {
261
262 cout << "HadronInterface: getting the universal beta-gamma fits..." << endl;
263
264 TFile* outfile = new TFile(outfilename,"RECREATE");
265 // make sure the samples exist
266 for( int i = 0; i < 5; ++i ){
267 TFile* testfile = new TFile(m_filenames[i]);
268 if( testfile->GetListOfKeys()->IsEmpty() ){
269 cout << "\t No hadron sample for " << m_types[i] << "... " << endl;
270 continue;
271 }
272 testfile->Close();
273
274 // prepare the sample by fitting in bins of beta-gamma and cos(theta)
275 HadronPrep prep(m_filenames[i], m_mcFlag, m_type, m_bgbins[i], m_upperbg[i], m_lowerbg[i], m_cosbins[i], m_uppercos[i], m_lowercos[i]);
276 prep.bgFits(m_types[i],outfile,correct,m_paramfile);
277 }
278 outfile->Close();
279}
280
281void
282HadronInterface::SaturationCorrection( TString prepfilename, std::string parfile ) {
283
284 // do not do the correction on mc samples...
285 if( m_mcFlag ) return;
286
287 cout << "HadronInterface: performing the hadron saturation correction..." << endl;
288
289 // make sure the file exists
290 TFile* testfile = new TFile(prepfilename);
291 if( testfile->GetListOfKeys()->IsEmpty() ){
292 cout << "\t CAUTION: Fitted file does not exist... " << endl;
293 exit(1);
294 }
295 testfile->Close();
296
297 // create the HadronSaturation object and fill the prepared samples
298 HadronSaturation hadsat;
299 hadsat.setParameters(parfile);
300 hadsat.fillSample(prepfilename);
301 hadsat.printEvents(0,10);
302
303 // perform the hadron saturation correction
304 hadsat.fitSaturation();
305}
306
307void
308HadronInterface::BetaGammaFits( std::vector< TString > particles, TString filename ) {
309
310 cout << "HadronInterface: performing the beta-gamma curve fits..." << endl;
311
312 // make sure the file exists
313 TFile* testfile = new TFile(filename);
314 if( testfile->GetListOfKeys()->IsEmpty() ){
315 cout << "\t CAUTION: Fitted file does not exist... " << endl;
316 exit(1);
317 }
318 testfile->Close();
319
320 // create the HadronCalibration object and fit the prepared samples
321 HadronCalibration hadcal;
322 hadcal.fitBGCurve(particles,filename,m_paramfile);
323 SetParamFile("parameters.bgcurve.fit");
324}
325
326void
327HadronInterface::PlotBGCurve( std::vector< TString > particles, TString filename ) {
328
329 cout << "HadronInterface: performing the beta-gamma curve fits..." << endl;
330
331 // make sure the file exists
332 TFile* testfile = new TFile(filename);
333 if( testfile->GetListOfKeys()->IsEmpty() ){
334 cout << "\t CAUTION: Fitted file does not exist... " << endl;
335 exit(1);
336 }
337 testfile->Close();
338
339 // create the HadronCalibration object and fit the prepared samples
340 HadronCalibration hadcal;
341 hadcal.plotBGCurve(particles,filename,m_paramfile);
342}
343
344void
345HadronInterface::SigmaFits( TString filename ) {
346
347 cout << "HadronInterface: performing the sigma curve fits..." << endl;
348
349 // make sure the file exists
350 TFile* testfile = new TFile(filename);
351 if( testfile->GetListOfKeys()->IsEmpty() ){
352 cout << "\t CAUTION: Fitted file does not exist... " << endl;
353 exit(1);
354 }
355 testfile->Close();
356
357 // create the HadronCalibration object and fit the prepared samples
358 // only the pion samples are used for the sigma fits
359 HadronCalibration hadcal;
360 hadcal.fitSigmaVsNHit(m_filenames[0],m_paramfile,m_mcFlag,m_type);
361 SetParamFile("parameters.sigmanhit.fit");
362 hadcal.fitSigmaVsSin(m_filenames[0],m_paramfile,m_mcFlag,m_type);
363 SetParamFile("parameters.sigmasin.fit");
364}
365
366
367void
369
370 cout << "HadronInterface: plotting the efficiency..." << endl;
371
372 // create the HadronCalibration object and fill histograms
373 HadronCalibration hadcal;
374 SetParamFile("parameters.bgcurve.fit");
375
376 TString flavor("pion");
377 hadcal.plotEfficiency(m_filenames,saveas,m_paramfile,m_mcFlag,m_type);
378}
void fitSigmaVsSin(TString filename, std::string paramfile, int mcFlag, int type)
void plotEfficiency(TString filenames[5], TString saveas, std::string paramfile, int mcFlag, int type)
void plotBGCurve(std::vector< TString > particles, TString filename, std::string paramfile)
void fitBGCurve(std::vector< TString > particles, TString filename, std::string paramfile)
void fitSigmaVsNHit(TString filename, std::string paramfile, int mcFlag, int type)
void BetaGammaFits(std::vector< TString > particles, TString filename)
void SetParamFile(std::string paramfile)
void SimulateSample(TString infilename, TString particle)
void QualityCheck(TString outfilename)
void PrepareResults(TString outfilename, bool correct)
void PlotEfficiency(TString saveas)
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 GenerateSample(TString particle)
void AddParticle(TString particle, TString filename, int nevents, int bgbins, double upperbg, double lowerbg, int cosbins, double uppercos, double lowercos)
void SigmaFits(TString filename)
void SimulateReconstruction(TString infilename, TString outfilename)
void bgCosThetaFits(TString particle, TFile *outfile, bool correct, std::string paramfile)
Definition HadronPrep.cc:55
void bgFits(TString particle, TFile *outfile, bool correct, std::string paramfile)
void printEvents(int firstevent, int nevents)
void fillSample(TString infilename)
void setParameters(double par[])
void simulateDedx(TString infilename, TString filename, double genmass)
void simulateReconstruction(TString infilename, TString outfilename)
void generateEvents(TString filename, double genmass)
void makeHistograms(std::vector< TString > filenames, TString outfilename)
void plotHistograms(TString outfilename)