StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PlotVtxSeed.C
1 // //
3 // PlotVtxSeed.C macro //
4 // Author: G. Van Buren, BNL //
5 // Description: uses output ntuples from StVertexSeedMaker to display //
6 // results of BeamLine calibration //
7 // Usage: root PlotVtxSeed.C //
8 // ...without arguments, will attempt to use ntuples in current //
9 // directory with automatic plot ranges //
10 // argument : purpose //
11 // dataName : histogram title, saves graphics to file //
12 // dir : directory where to look for ntuples //
13 // do_stats : show bars representing statistics for each point //
14 // ilow,ihigh : define vertical range for intercept plot //
15 // slow,shigh : define vertical range for slope plot //
16 // dlow,dhigh : define horizontal range for both plots //
17 // //
19 
20 
21 // globals
22 const float NA = -99.;
23 TTree* BLpars = 0;
24 TGraphErrors* gr_x0 = 0;
25 TGraphErrors* gr_y0 = 0;
26 TGraphErrors* gr_dxdz = 0;
27 TGraphErrors* gr_dydz = 0;
28 TGraphErrors* gr_stats = 0;
29 TCanvas* c1 = 0;
30 TPad* ints = 0;
31 TPad* slos = 0;
32 TPad* legs = 0;
33 TLegend* leg = 0;
34 TText* t1 = 0;
35 
36 float int_lo = NA;
37 float int_hi = NA;
38 float slo_lo = NA;
39 float slo_hi = NA;
40 float day_lo = NA;
41 float day_hi = NA;
42 
44 // function declarations
45 
46 // main function
47 TTree* PlotVtxSeed(const char* dataName=0,
48  const char* dir=0,
49  int do_stats=0,
50  float ilow=NA, float ihigh=NA,
51  float slow=NA, float shigh=NA,
52  float dlow=NA, float dhigh=NA);
53 
54 // user functions
55 void draw(const char* dataName=0, int do_stats=0);
56 void setInt(float lo,float hi) {int_lo = lo; int_hi = hi;}
57 void setSlo(float lo,float hi) {slo_lo = lo; slo_hi = hi;}
58 void setDay(float lo,float hi) {day_lo = lo; day_hi = hi;}
59 void unsetInt() {int_lo = NA; int_hi = NA;}
60 void unsetSlo() {slo_lo = NA; slo_hi = NA;}
61 void unsetDay() {day_lo = NA; day_hi = NA;}
62 
63 // helper functions
64 void init(const char* dir);
65 void prep();
66 void roundIt(float& min, float& max, float r);
67 void Waiting();
68 
70 // function implementations
71 
72 TTree* PlotVtxSeed(const char* dataName,
73  const char* dir,
74  int do_stats,
75  float ilow, float ihigh,
76  float slow, float shigh,
77  float dlow, float dhigh) {
78  init(dir);
79  setInt(ilow,ihigh);
80  setSlo(slow,shigh);
81  setDay(dlow,dhigh);
82  draw(dataName,do_stats);
83  return BLpars;
84 }
85 
86 void init(const char* dir) {
87  TString dirStr = (dir ? dir : ".");
88  cout << "In directory: " << dirStr << endl;
89  cout << "Looking for BLpars.root first..." << endl;
90  TFile* ff = TFile::Open(Form("file:%s/BLpars.root",dirStr.Data()));
91  if (ff) {
92  TKey* BLkey = ff->GetKey("BLpars");
93  if (BLkey) BLpars = (TTree*) (BLkey->ReadObj());
94  else cerr << "Found file, but no BLpars key!" << endl;
95  } else {
96  cout << "Looking for vertexseedhist.*.root second..." << endl;
97  TChain* BLchain = new TChain("BLpars");
98  if (BLchain->Add(Form("%s/vertexseedhist.*.root",dirStr.Data())))
99  BLpars = (TTree*) BLchain;
100  }
101  if (!BLpars) {
102  cerr << "No luck finding BeamLine ntuples!" << endl;
103  exit(-1);
104  }
105  BLpars->SetMarkerStyle(22);
106  c1 = new TCanvas("BeamLine","BeamLine Constraint",600,600);
107  gStyle->SetGridColor(kGray);
108 }
109 
110 void prep() {
111  c1->Clear();
112  c1->Draw();
113  c1->cd();
114  ints = new TPad("ints","Intercepts",0.01,0.468,0.99,0.9);
115  ints->SetBottomMargin(0.);
116  c1->cd();
117  slos = new TPad("slos","Slopes",0.01,0.04,0.99,0.472);
118  slos->SetTopMargin(0.);
119  c1->cd();
120  legs = new TPad("legs","Legends",0.84,0.87,0.99,0.99);
121  legs->SetBottomMargin(0.);
122  legs->SetTopMargin(0.);
123  legs->SetLeftMargin(0.);
124  legs->SetRightMargin(0.);
125 
126  ints->Draw();
127  slos->Draw();
128  legs->Draw();
129 }
130 
131 void draw(const char* dataName, int do_stats) {
132 
133  prep();
134 
135  //Declaration of leaves types
136  Double_t days;
137  Double_t x0;
138  Double_t err_x0;
139  Double_t y0;
140  Double_t err_y0;
141  Double_t dxdz;
142  Double_t err_dxdz;
143  Double_t dydz;
144  Double_t err_dydz;
145  Double_t stats;
146  Double_t date;
147  Double_t fill;
148  Double_t zdc;
149 
150  // Set branch addresses.
151  BLpars->SetBranchAddress("days",&days);
152  BLpars->SetBranchAddress("x0",&x0);
153  BLpars->SetBranchAddress("err_x0",&err_x0);
154  BLpars->SetBranchAddress("y0",&y0);
155  BLpars->SetBranchAddress("err_y0",&err_y0);
156  BLpars->SetBranchAddress("dxdz",&dxdz);
157  BLpars->SetBranchAddress("err_dxdz",&err_dxdz);
158  BLpars->SetBranchAddress("dydz",&dydz);
159  BLpars->SetBranchAddress("err_dydz",&err_dydz);
160  BLpars->SetBranchAddress("stats",&stats);
161  BLpars->SetBranchAddress("date",&date);
162  BLpars->SetBranchAddress("fill",&fill);
163  BLpars->SetBranchAddress("zdc",&zdc);
164 
165  Int_t nentries = (Int_t) (BLpars->GetEntries());
166  gr_x0 = new TGraphErrors(nentries);
167  gr_y0 = new TGraphErrors(nentries);
168  gr_dxdz = new TGraphErrors(nentries);
169  gr_dydz = new TGraphErrors(nentries);
170  gr_stats = new TGraphErrors(nentries);
171 
172  gr_x0->SetMarkerColor(2);
173  gr_y0->SetMarkerColor(4);
174  gr_dxdz->SetMarkerColor(6);
175  gr_dydz->SetMarkerColor(kGreen+2);
176  gr_x0->SetMarkerStyle(25);
177  gr_y0->SetMarkerStyle(27);
178  gr_dxdz->SetMarkerStyle(25);
179  gr_dydz->SetMarkerStyle(27);
180  gr_x0->SetMarkerSize(0.4);
181  gr_y0->SetMarkerSize(0.7);
182  gr_dxdz->SetMarkerSize(0.4);
183  gr_dydz->SetMarkerSize(0.7);
184  gr_stats->SetFillColor(41);
185 
186  legs->cd();
187  leg = new TLegend(0.001,0.04,0.960,0.999);
188  leg->AddEntry(gr_x0,"x0","p");
189  leg->AddEntry(gr_y0,"y0","p");
190  leg->AddEntry(gr_dxdz,"dxdz","p");
191  leg->AddEntry(gr_dydz,"dydz","p");
192  leg->SetFillColor(42);
193 
194  float min_day = 366.0;
195  float max_day = 0;
196  float min_int = 0;
197  float max_int = 0;
198  float min_slo = 0;
199  float max_slo = 0;
200 
201 
202  for (Int_t i=0; i<nentries;i++) {
203  BLpars->GetEntry(i);
204  gr_x0->SetPoint(i,days,x0);
205  gr_x0->SetPointError(i,0,err_x0);
206  gr_y0->SetPoint(i,days,y0);
207  gr_y0->SetPointError(i,0,err_y0);
208  gr_dxdz->SetPoint(i,days,dxdz);
209  gr_dxdz->SetPointError(i,0,err_dxdz);
210  gr_dydz->SetPoint(i,days,dydz);
211  gr_dydz->SetPointError(i,0,err_dydz);
212  gr_stats->SetPoint(i,days,days);
213  if (stats!=0)
214  gr_stats->SetPointError(i,0.1,TMath::Sqrt(10.0/stats)); // stats=1000 -> err=0.1
215 
216  min_day = TMath::Min(min_day,days);
217  max_day = TMath::Max(max_day,days);
218  min_int = TMath::Min(min_int,TMath::Min(x0-err_x0,y0-err_y0));
219  max_int = TMath::Max(max_int,TMath::Max(x0+err_x0,y0+err_y0));
220  min_slo = TMath::Min(min_slo,TMath::Min(dxdz-err_dxdz,dydz-err_dydz));
221  max_slo = TMath::Max(max_slo,TMath::Max(dxdz+err_dxdz,dydz+err_dydz));
222  }
223 
224  float day_round = 5.0;
225  float int_round = 0.05;
226  float slo_round = 0.001;
227 
228  if (day_lo == NA) roundIt(min_day,max_day,day_round);
229  else { min_day = day_lo; max_day = day_hi; }
230  if (int_lo == NA) roundIt(min_int,max_int,int_round);
231  else { min_int = int_lo; max_int = int_hi; }
232  if (slo_lo == NA) roundIt(min_slo,max_slo,slo_round);
233  else { min_slo = slo_lo; max_slo = slo_hi; }
234 
235  for (Int_t i=0; i<nentries;i++) {
236  double d1,d2;
237  gr_stats->GetPoint(i,d1,d2);
238  gr_stats->SetPoint(i,d1,min_int);
239  }
240 
241  ints->cd();
242  TH1F* h_ints = ints->DrawFrame(min_day,min_int,max_day,max_int);
243  h_ints->SetTitleSize(0.05,"Y");
244  h_ints->SetYTitle("Intercepts [cm] ");
245  if (do_stats) gr_stats->Draw("2");
246  gr_x0->Draw("P");
247  gr_y0->Draw("P");
248 
249 
250  slos->cd();
251  TH1F* h_slos = slos->DrawFrame(min_day,min_slo,max_day,max_slo);
252  h_slos->SetTitleSize(0.05,"Y");
253  h_slos->SetYTitle("Slopes ");
254  //slos->Clear();
255  slos->Draw();
256  slos->cd();
257  h_slos->Draw("Y+");
258  gr_dxdz->Draw("P");
259  gr_dydz->Draw("P");
260 
261  legs->cd();
262  leg->Draw();
263 
264  c1->cd();
265  TText* t1 = new TText;
266  t1->SetTextAlign(32);
267  t1->SetTextSize(0.035);
268  t1->DrawText(0.5,0.02,"Day");
269  t1->SetTextAlign(12);
270  t1->SetTextSize(0.020);
271  t1->DrawText(0.55,0.02,"[1.0,2.0] = Jan 1 EDT");
272  t1->SetTextAlign(22);
273  t1->SetTextSize(0.045);
274  t1->DrawText(0.4,0.97,"BeamLine Constraint");
275  if (dataName) t1->DrawText(0.4,0.925,dataName);
276 
277  c1->Update(); c1->Draw();
278 
279  if (dataName) c1->SaveAs(Form("%s.gif",dataName));
280 }
281 
282 void roundIt(float& min, float& max, float r) {
283  float pad = 2.0;
284  float near = 0.5;
285  max = r * ( ((int) (max/r - near + pad)) + ( (max>0) ? 1. : 0. ) + near );
286  min = r * ( ((int) (min/r + near - pad)) - ( (min<0) ? 1. : 0. ) - near );
287 }
288 
289 void Waiting() {
290  c1->Update(); c1->Draw();
291  int temp;
292  printf("Waiting...\n");
293  cin >> temp;
294 }
295 
297 // $Id: PlotVtxSeed.C,v 2.1 2012/08/22 22:31:56 genevb Exp $
298 // $Log: PlotVtxSeed.C,v $
299 // Revision 2.1 2012/08/22 22:31:56 genevb
300 // Place into CVS
301 //
302 //