ROOTPWA
roottops.C
Go to the documentation of this file.
1 #include "TString.h"
2 #include "TSystem.h"
3 #include "TFile.h"
4 #include "TCanvas.h"
5 #include "TList.h"
6 #include "TGraph.h"
7 #include "TAxis.h"
8 #include "TGaxis.h"
9 #include "TROOT.h"
10 #include "TKey.h"
11 #include "TStyle.h"
12 #include "TLatex.h"
13 #include "TH2D.h"
14 #include "TMultiGraph.h"
15 
16 #include <vector>
17 #include <iostream>
18 #include <sstream>
19 #include <cmath>
20 
21 using namespace std;
22 
23 
24 
25 
26 
27 TString parseTitle(TString l, unsigned int level=10){
28  // setup isobar dictionary key->tex
29  map<TString, TString> isobars;
30  isobars["pi+" ] = "\\pi^{+}";
31  isobars["pi-" ] = "\\pi^{-}";
32  isobars["pi+-" ] = "\\pi^{\\pm}";
33  isobars["pi-+" ] = "\\pi^{\\mp}";
34  isobars["sigma" ] = "\\sigma";
35  isobars["rho770" ] = "\\rho(770)";
36  isobars["a11269" ] = "a_{1}(1269)";
37  isobars["a21320" ] = "a_{2}(1320)";
38  isobars["rho1450" ] = "\\rho(1450)";
39  isobars["rho1700" ] = "\\rho(1700)";
40  isobars["pi1300" ] = "\\pi(1300)";
41  isobars["pi1800" ] = "\\pi(1800)";
42  isobars["pi21670" ] = "\\pi_2(1670)";
43  isobars["f01370" ] = "f_{0}(1370)";
44  isobars["f01500" ] = "f_{0}(1500)";
45  isobars["f01700" ] = "f_{0}(1700)";
46  isobars["f11285" ] = "f_{1}(1285)";
47  isobars["f11420" ] = "f_{1}(1420)";
48  isobars["b11235" ] = "b_{1}(1235)";
49  isobars["b21800" ] = "b_{2}(1800)";
50  isobars["b11800" ] = "b_{1}(1800)";
51  isobars["b11500" ] = "b_{1}(1500)";
52  isobars["f21270" ] = "f_{2}(1270)";
53  isobars["f21950" ] = "f_{2}(1950)";
54  isobars["f21565" ] = "f_{2}(1565)";
55  isobars["f21270" ] = "f_{2}(1270)";
56  isobars["f22010" ] = "f_{2}(2010)";
57  isobars["f11420" ] = "f_{1}(1420)";
58  isobars["eta1440" ] = "\\eta(1420)";
59  isobars["eta11600" ] = "\\eta_{1}(1600)";
60  isobars["eta21645"] = "\\eta_{2}(1645)";
61  isobars["rho31690"] = "\\rho_{3}(1690)";
62  isobars["rho1600"] = "\\rho(1600)";
63 
64  // remove file extension
65  l.Remove(l.Length() - 4);
66  // extract X quantum numbers
67  const TString head = l(0, 7);
68  const TString I = head(0, 1);
69  const TString G = head(1, 1);
70  const TString J = head(2, 1);
71  const TString P = head(3, 1);
72  const TString C = head(4, 1);
73  const TString M = head(5, 1);
74  const TString refl = head(6, 1);
75  l.Remove(0, 7);
76  // print X quantum numbers
77 
78  stringstream res;
79  res << I << "^{" << G <<"}(" << J << "^{" << P << C << "}" << M << "^{" << refl << "})";
80 
81  // tokenize input
82  TObjArray* tokens = l.Tokenize("_=");
83  int mode = 0;
84  unsigned int counter=0;
85  for (int i = 0; i < tokens->GetEntries(); ++i) {
86  const TString token = ((TObjString*)tokens->At(i))->GetString();
87  cerr << " " << mode << ": '" << token << "'" << endl;
88  if (mode == 0) { // isobar mode
89  if (isobars.find(token) != isobars.end())
90  res << isobars[token];
91  else
92  res << token;
93  res << " ";
94  // check which mode to switch to depending whether we get _ or =
95  l.Remove(0, token.Length());
96  if (l(0, 1) == "_")
97  mode = 1;
98  else
99  mode = 2;
100  } else if (mode == 1) { // ls mode
101  if (token.Length() == 1) // only l
102  res << "[" << token << "] ";
103  else
104  res << "\\left[#splitline{" << token(0, 1) << "}{"
105  << token(1, 1) << "}\\right]";
106  l.Remove(0, token.Length());
107  mode = 0;
108  } else if (mode == 2) {
109  if(level<=counter) break;
110  ++counter;
111  res << "\\rightarrow ";
112  if (isobars.find(token) != isobars.end())
113  res << isobars[token];
114  else
115  res << token;
116  res << " ";
117  l.Remove(0, token.Length());
118  if (l(0, 1) == "_" )
119  mode = 1;
120  else
121  mode = 2;
122  }
123  l.Remove(0, 1); // remove delimiter
124  }
125  //res;
126 
127  tokens->Delete();
128  delete tokens;
129  TString result(res.str().c_str());
130  return result;
131 }
132 
133 
134 
135 
136 void annotatePlot(TVirtualPad* pad){
137 
138  pad->cd();
139 
140  double xcenter=0.5;
141  double ycenter=0.5;
142 
143  double x=xcenter-0.2;
144  double y=xcenter-0.2;
145 
146  // preliminary
147  TLatex* prelim=new TLatex(x,y,"preliminary");
148  prelim->SetNDC();
149  prelim->SetTextColor(kGray);
150  prelim->SetTextSize(0.1);
151  prelim->SetTextAngle(20);
152  prelim->Draw();
153 
154  // compass 2004
155  double xc=xcenter+0.08;
156  //if(right)xc=xcenter+0.1;
157  double yc=ycenter+0.35;
158  TLatex* com04=new TLatex(xc,yc,"COMPASS 2004");
159  com04->SetNDC();
160  com04->SetTextSize(0.05);
161  com04->Draw();
162 
163  // 5 pi on pb
164  xc=xcenter+0.08;
165  //xc=xcenter+0.1;
166  yc=ycenter+0.31;
167  TLatex* react=new TLatex(xc,yc,"#pi^{-} Pb #rightarrow #pi^{-}#pi^{+}#pi^{-}#pi^{+}#pi^{-} Pb");
168  react->SetNDC();
169  react->SetTextSize(0.039);
170  react->Draw();
171 
172  // add waves
173  cout << "####### Title:" << endl;
174  TMultiGraph* gr=dynamic_cast<TMultiGraph*>(pad->GetListOfPrimitives()->At(0));
175  TH2D* h2=dynamic_cast<TH2D*>(pad->GetListOfPrimitives()->At(0));
176  TString title;
177  if(gr!=NULL){
178  title=gr->GetName();
179  }
180  else {
181  title=h2->GetName();
182  title.Remove(0,1);
183  }
184  cout << title << endl;
185  // check if this is intensity or off diagonal
186  TString wave;
187  if(title.Contains("amp")){
188  if(!title.Contains("---")){
189  cout << "Processing Intensity:" << endl;
190  wave=parseTitle(title,0);
191  cout << wave << endl;
192  yc=ycenter+0.24;
193  }
194  else {
195  // Split
196  unsigned int i = title.Index("---");
197  TString wave1=title(3,i-3);
198  TString wave2=title(i+3,title.Length());
199 
200  cout << parseTitle(wave1,0) << endl;
201  cout << parseTitle(wave2,0) << endl;
202 
203 
204 
205  wave="#splitline{Interference - ";
206  wave+= title.Contains("Re") ? "real part" : "imaginary part";
207  wave+="}{#splitline{";
208  wave+=parseTitle(wave1,0);
209  wave+="}{";
210  wave+=parseTitle(wave2,0);
211  wave+="}}";
212  // check if we put text up or down
213  double max=gr->GetYaxis()->GetXmax();
214  double min=gr->GetYaxis()->GetXmin();
215  // get last data point
216  TGraph* g=(TGraph*)gr->GetListOfGraphs()->At(0);
217  double yg,xg;
218  g->GetPoint((int)(g->GetN()*0.6),xg,yg);
219  if(fabs(max-yg)>fabs(min-yg)){
220  yc=ycenter+0.24;
221  }
222  else{
223  yc=ycenter-0.25;
224  }
225  }
226  } // end if title.Contains amp
227  else {
228  wave=title;
229  yc=ycenter+0.24;
230  }
231 
232  xc=xcenter+0.08;
233  //xc=xcenter+0.1;
234 
235  TLatex* waveL=new TLatex(xc,yc,wave);
236  waveL->SetNDC();
237  waveL->SetTextSize(0.03);
238  waveL->Draw();
239 
240 
241  // fitname
242  // xc=xcenter-0.4;
243  //if(right)xc=xcenter+0.1;
244  // yc=ycenter+0.42;
245  // TLatex* fitNameL=new TLatex(xc,yc,fitname);
246  //fitNameL->SetNDC();
247  //fitNameL->SetTextSize(0.025);
248  //fitNameL->Draw();
249  double xmax, xmin;
250  if(gr!=NULL){
251  xmax=gr->GetXaxis()->GetXmax();
252  xmin=gr->GetXaxis()->GetXmin();
253  }
254  else {
255  xmax=h2->GetXaxis()->GetXmax();
256  xmin=h2->GetXaxis()->GetXmin();
257  }
258 
259 
260  TLine* zeroline=new TLine(xmin,0,xmax,0);
261  zeroline->SetLineStyle(7);
262  zeroline->Draw();
263 
264  if(gr!=NULL){
265  gr->GetXaxis()->SetTitle("mass (GeV/c^{2})");
266  gr->Draw();
267  }
268  if(h2!=NULL){
269  h2->GetXaxis()->SetTitle("mass (GeV/c^{2})");
270  //h2->Draw("COL");
271  }
272  pad->UseCurrentStyle();
273  pad->Update();
274  //cpopup->Flush();
275 }
276 
277 
278 
279 
280 
281 
282 
283 
284 
285 void roottops(const TString& infilename, const TString& plotdir="", const TString& normfilename=""){
286 
287  gStyle->SetFrameFillStyle(4000);
288 
289  bool savePlots=plotdir.Length()>1;
290 
291 
292  TString name=infilename;
293  name.ReplaceAll(".root","");
294 
295  const int nmbPadsPerCanvMin = 6; // minimum number of pads each canvas is subdivided into
296 
297  TFile* infile=TFile::Open(infilename,"READ");
298 
299  Int_t font=132;
300  gStyle->SetTextFont(font);
301  gStyle->SetLabelFont(font,"xyz");
302  gStyle->SetTitleFont(font,"xyz");
303  gStyle->SetOptStat(0);
304  gStyle->SetOptTitle(0);
305  gStyle->SetStripDecimals(1);
306  TGaxis::SetMaxDigits(4);
307 
308  gROOT->ForceStyle();
309 
310 
311 
312 
313  TFile* normfile=0;
314  if(normfilename.Length()>1)normfile=TFile::Open(normfilename,"READ");
315 
316  TList* keylist=infile->GetListOfKeys();
317  unsigned int num=keylist->GetEntries();
318 
319  cout << num << " entries in keylist." << endl;
320 
321  const int nmbPadsHor = (int)floor(sqrt(nmbPadsPerCanvMin));
322  const int nmbPadsVert = (int)ceil((double)nmbPadsPerCanvMin / (double)nmbPadsHor);
323  const int nmbPadsPerCanv = nmbPadsHor * nmbPadsVert;
324 
325 
326  vector<TCanvas*> canvases;
327  TCanvas* current=NULL;
328  unsigned int padcounter=0;
329  for(unsigned int i=0;i<num;++i){ // loop over keys
330 
331  if(padcounter%(nmbPadsPerCanv+1) ==0){ // create new canvas
332  TString cname=name;cname+=canvases.size();
333  current=new TCanvas(cname,cname, 10, 10, 800, 1000);
334  current->Divide(nmbPadsHor,nmbPadsVert);
335  canvases.push_back(current);
336  padcounter=1;
337  }
338  current->cd(padcounter);
339 
340 
341  // TString type("TMultiGraph");
342  TString type("TH2D");
343 
344 
345 
346  if(TString(((TKey*)keylist->At(i))->GetClassName())==type){
347  cout << "Found " << type << endl;
348  TString keyname(((TKey*)keylist->At(i))->GetName());
349  if(keyname.Contains("PHI"))continue;
350  if(type=="TH2D"){
351  ((TKey*)keylist->At(i))->ReadObj()->Draw("COL");
352  if(keyname.Contains("rho1")){
353  ((TH2D*)(((TKey*)keylist->At(i))->ReadObj()))->GetYaxis()->SetRangeUser(-100,15000);}
354  }
355  else ((TKey*)keylist->At(i))->ReadObj()->Draw("AP");
356  annotatePlot(gPad);
357  if(savePlots){
358  TPad* mypad=(TPad*)gPad;
359  mypad->Dump();
360  TString filename=plotdir+"plot_"+keyname;
361  filename.ReplaceAll(".amp",".eps");
362  filename.ReplaceAll("+","p");
363  filename.ReplaceAll("-","m");
364  filename.ReplaceAll("=","_to_");
365  TCanvas* cplot=new TCanvas("cplot","cplot",10,10,700,700);
366  cplot->cd();
367  TVirtualPad* clone=(TVirtualPad*)mypad->Clone();
368  clone->Draw();
369  clone->SetPad("pu","PopUp",0,0,1,1,0);
370  cplot->cd(1);
371  cplot->Update();
372  cplot->SaveAs(filename);
373  cplot;
374  }
375  // plot normgraphs if available
376  if(normfile!=0){
377  TString name=((TKey*)keylist->At(i))->ReadObj()->GetName();
378  if(name.Contains("amp")){
379  TH2D* h=(TH2D*)((TKey*)keylist->At(i))->ReadObj();
380  double xmin=h->GetXaxis()->GetXmin()*1000;
381  double xmax=h->GetXaxis()->GetXmax()*1000;
382  cout << xmin << " " << xmax << endl;
383  TString wavename=name(1,name.Length());
384  TGraph* g=(TGraph*)normfile->Get(wavename);
385 
386  TPad* p2=new TPad("pad","PS",gPad->GetX1(),gPad->GetY1(),gPad->GetX2(),gPad->GetY2());
387  //p2->RangeAxis(xmin,0,xmax,1);
388  p2->SetFillStyle(4000);
389  p2->Draw();
390  p2->cd();
391  g->Draw("APC");
392  g->GetXaxis()->SetRangeUser(xmin,xmax);
393  g->GetXaxis()->SetTitle("Mass (GeV/c^{2}");
394  }
395  }
396  ++padcounter;
397  }
398 
399  }// end loop over keys
400  int nc1=canvases.size();
401  // loop again to get 3-plots for phases
402  for(unsigned int i=0;i<num;++i){ // loop over keys
403  TString keyname(((TKey*)keylist->At(i))->GetName());
404  if(keyname.Contains("PHI")){
405  continue;
406  TMultiGraph* g=(TMultiGraph*)((TKey*)keylist->At(i))->ReadObj();
407  if(g==NULL)continue;
408  cout << "found Phase Graph!" << TString(((TKey*)keylist->At(i))->GetName()) << endl;
409  int divider=keyname.Index("---");
410  TString key1=keyname(3,divider-3);
411  TString key2=keyname(divider+6,keyname.Length());
412  cout << key1 << endl;
413  cout << key2 << endl;
414  TMultiGraph* g1=(TMultiGraph*) infile->Get(key1);
415  TMultiGraph* g2=(TMultiGraph*) infile->Get(key2);
416  if(g1==NULL || g2==NULL) continue;
417  current=new TCanvas("C"+keyname,"C"+keyname, 10, 10, 800, 1000);
418  current->Divide(1,3);
419  canvases.push_back(current);
420  current->cd(1);
421  gPad->SetGridy();
422  // plot phasegraph
423 
424  if(g->GetListOfGraphs()->GetSize()==0){}
425  else {
426 
427  g->Draw("AN");
428  //g->GetYaxis()->Set(8,-720,720);
429  //g->GetYaxis()->SetRangeUser(-720,720);
430  g->GetYaxis()->SetRangeUser(-200,200);
431 
432  g->Draw("A");
433  }
434  // get waves
435 
436  current->cd(2);if(g1!=NULL)g1->Draw("APC");
437  current->cd(3);if(g2!=NULL)g2->Draw("APC");
438  } // endif found PHI hist
439  }
440 
441 
442  TString psFileName = name; psFileName += ".ps";
443  TCanvas dummyCanv("dummy", "dummy");
444  TString option="Portrait";
445  dummyCanv.Print((psFileName + "["),option);
446  for(unsigned int ic=0;ic<canvases.size();++ic){
447  if(ic>=nc1)option="Portrait";
448  canvases[ic]->Print(psFileName,option);
449  delete canvases[ic];
450  }
451  dummyCanv.Print((psFileName + "]"),option);
452  gSystem->Exec(("gv " + psFileName));
453 }
454 
455 
456 
457 
458 
459 // const string psFileName = outPath + "waveIntensities.ps";
460 // TCanvas dummyCanv("dummy", "dummy");
461 // dummyCanv.Print((psFileName + "[").c_str());
462 // for (map<string, TCanvas*>::iterator i = canvases.begin(); i != canvases.end(); ++i) {
463 // i->second->Print(psFileName.c_str());
464 // delete i->second;
465 // i->second = 0;
466 // }
467 // dummyCanv.Print((psFileName + "]").c_str());
468 // gSystem->Exec(("gv " + psFileName).c_str());
469 // }
470 
471 // return wavePads;
472 
473 
474 
475 
476 // }