Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackParametrization.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TrackParametrization.C
1 int
2 TrackParametrization( TString csvfile="fitslices_out.csv" )
3 {
4 
5  /* Read data from input file */
6  TTree *tres = new TTree();
7  tres->ReadFile( csvfile, "ptrue:etatrue:psig:psig_err:pmean:pmean_err:norm", ',' );
8 
9  /* Print read-in tree */
10  tres->Print();
11 
12  /* colors array */
13  unsigned colors[8] = {1,2,3,4,6,7,14,16};
14 
15  /* Create vector of theta values to include for visualization*/
16  vector< double > etas_vis;
17  etas_vis.push_back(-2.75);
18  etas_vis.push_back(-2.25);
19  etas_vis.push_back(-1.75);
20  etas_vis.push_back(-0.25);
21  etas_vis.push_back( 0.25);
22  etas_vis.push_back( 1.75);
23  etas_vis.push_back( 2.25);
24 
25 // etas_vis.push_back(-3.25);
26 // etas_vis.push_back(-2.25);
27 // etas_vis.push_back(-1.25);
28 // etas_vis.push_back(-0.25);
29 // etas_vis.push_back( 0.25);
30 // etas_vis.push_back( 1.25);
31 // etas_vis.push_back( 2.25);
32 // etas_vis.push_back( 3.25);
33 
34  /* Create vector of theta values to include for fitting*/
35  vector< double > etas_fit;
36  for ( double eta = -4.45; eta < 4.5; eta += 0.1 )
37  etas_fit.push_back( eta );
38 
39  /* Create fit function */
40  TF1* f_momres = new TF1("f_momres", "sqrt( [0]*[0] + [1]*[1]*x*x )" );
41 
42  cout << "\nFit function: " << f_momres->GetTitle() << "\n" << endl;
43 
44  /* Create scratch canvas */
45  TCanvas *cscratch = new TCanvas("cscratch");
46 
47  /* Create framehistogram */
48  TH1F* hframe = new TH1F("hframe","",100,0,40);
49  hframe->GetYaxis()->SetRangeUser(0,0.15);
50  hframe->GetYaxis()->SetNdivisions(505);
51  hframe->GetXaxis()->SetTitle("Momentum (GeV/c)");
52  hframe->GetYaxis()->SetTitle("#sigma_{p}/p");
53 
54  /* create combined canvas plot */
55  TCanvas *c1 = new TCanvas();
56  hframe->Draw();
57 
58  /* Create legend */
59  TLegend* leg_eta = new TLegend( 0.2, 0.6, 0.5, 0.9);
60  leg_eta->SetNColumns(2);
61 
62  /* Create ofstream to write fit parameter results */
63  ofstream ofsfit("track_momres_new.csv");
64  ofsfit<<"eta,par1,par1err,par2,par2err"<<endl;
65 
66  /* Create resolution-vs-momentum plot with fits for each selected theta value */
67  for ( int i = 0; i < etas_fit.size(); i++ )
68  {
69  /* Switch to scratch canvas */
70  cscratch->cd();
71 
72  double eta = etas_fit.at(i);
73 
74  /* No tracking outside -4 < eta < 4 */
75  if ( eta < -4 || eta > 4 )
76  continue;
77 
78  cout << "\n***Eta = " << eta << endl;
79 
80  /* Define range of theta because float comparison with fixed value doesn't work
81  too well for cuts in ROOT trees */
82  double eta_min = eta * 0.999;
83  double eta_max = eta * 1.001;
84 
85  /* Cut for tree */
86  TCut cutx( Form("ptrue > 1 && ( (etatrue > 0 && (etatrue > %f && etatrue < %f)) || (etatrue < 0 && (etatrue < %f && etatrue > %f)) )", eta_min, eta_max, eta_min, eta_max) );
87 
88  /* "Draw" tree on scratch canvas to fill V1...V4 arrays */
89  tres->Draw("psig:ptrue:psig_err:0", cutx );
90 
91  /* Create TGraphErrors with selected data from tree */
92  TGraphErrors *gres = new TGraphErrors( tres->GetEntries(cutx),
93  &(tres->GetV2())[0],
94  &(tres->GetV1())[0],
95  &(tres->GetV4())[0],
96  &(tres->GetV3())[0] );
97 
98  /* reset function parameters before fit */
99  f_momres->SetParameter(0,0.1);
100  f_momres->SetParameter(1,0.1);
101 
102  /* Only plot pseudorapidities listed on etas_vis; if not plotting, still do the fit */
103  bool vis = false;
104  int vi = 0;
105 
106  for ( vi = 0; vi < etas_vis.size(); vi++ )
107  {
108  if ( abs( etas_vis.at(vi) - eta ) < 0.001 )
109  {
110  vis = true;
111  break;
112  }
113  }
114 
115  if ( vis )
116  {
117  /* Add graph to legend */
118  leg_eta->AddEntry(gres, Form("#eta = %.1f", eta), "P");
119 
120  /* Add graph to plot */
121  c1->cd();
122  gres->SetMarkerColor(colors[vi]);
123  gres->Draw("Psame");
124  f_momres->SetLineColor(colors[vi]);
125  gres->Fit(f_momres);
126  }
127  else
128  {
129  gres->Fit(f_momres);
130  }
131 
132  /* Write fir results to file */
133  double par1 = f_momres->GetParameter(0);
134  double par1err = f_momres->GetParError(0);
135  double par2 = f_momres->GetParameter(1);
136  double par2err = f_momres->GetParError(1);
137  ofsfit << eta << "," << par1 << "," << par1err << "," << par2 << "," << par2err << endl;
138 
139  }
140 
141  /* Draw legend */
142  c1->cd();
143  //TCanvas *c2 = new TCanvas();
144  //hframe->Draw();
145  leg_eta->Draw();
146 
147  /* Print plots */
148  c1->Print("track_momres_vareta.eps");
149  //c2->Print("track_momres_vareta_legend.eps");
150 
151  /* Close output stream */
152  ofsfit.close();
153 
154  return 0;
155 }