1 //-------------------------------------------------------------------------
2 // Implementation of Class AliHEPDataParser
4 // This class is used to save the content of hisograms/graphs in the
5 // HEP data format and viceversa. The HEP data format is not strictly
6 // defined and there are many variants, the class only support a few
7 // of them. More will be added as needed. The input can be a set of
8 // 2 TH1, TGraphAsymmErrors or TGraphErrors (one for the stat and one
9 // for the syst error). If the second one is a null pointer, only the
10 // stat error is printed. The class can also import hepdata ascii
11 // file (very preliminary)
13 // Author: Michele Floris, CERN
14 //-------------------------------------------------------------------------
17 #include "AliHEPDataParser.h"
19 #include "TGraphAsymmErrors.h"
21 #include "TGraphErrors.h"
23 #include "TObjArray.h"
24 #include "TObjString.h"
29 ClassImp(AliHEPDataParser)
31 AliHEPDataParser::AliHEPDataParser() : TObject(), fHistStat(0), fHistSyst(0), fGraphStat(0), fGraphSyst(0), fHEPDataFileLines(0), fValueName("")
37 AliHEPDataParser::AliHEPDataParser(TH1 * hStat, TH1 * hSyst): TObject(), fHistStat(0), fHistSyst(0), fGraphStat(0), fGraphSyst(0), fHEPDataFileLines(0), fValueName("y")
42 fHEPDataFileLines = new TObjArray;
45 AliHEPDataParser::AliHEPDataParser(TGraph * grStat, TGraph * grSyst): TObject(), fHistStat(0), fHistSyst(0), fGraphStat(0), fGraphSyst(0), fHEPDataFileLines(0), fValueName("")
50 fHEPDataFileLines = new TObjArray;
53 AliHEPDataParser::AliHEPDataParser(const char * hepfileName): TObject(), fHistStat(0), fHistSyst(0), fGraphStat(0), fGraphSyst(0), fHEPDataFileLines(0), fValueName("y")
56 // Put results in graphs
57 fGraphSyst = new TGraphAsymmErrors();
58 fGraphStat = new TGraphAsymmErrors();
60 infile.open(hepfileName);
63 while (line.ReadLine(infile)) {
64 TObjArray * tokens = line.Tokenize(" ");
65 if(tokens->GetEntries() < 1) {
67 AliError("not enough columns");
70 // TODO: Assumes the format
71 // binmin binmax y +-stat +-syst. Try to make it smarter...
72 TObjString * binMin = (TObjString*) tokens->At(0);
73 TObjString * binMax = (TObjString*) tokens->At(1);
74 TObjString * value = (TObjString*) tokens->At(2);
75 TObjString * stat = (TObjString*) tokens->At(3);
76 TObjString * syst = (TObjString*) tokens->At(4);
77 stat->String().ReplaceAll("+-","");
78 if(syst) syst->String().ReplaceAll("+-","");
79 if (!binMin->String().Atof()) continue; // skip headers
80 Float_t binCenter = (binMax->String().Atof() + binMin->String().Atof())/2;
81 Float_t binWidth = (binMax->String().Atof() - binMin->String().Atof())/2;
82 cout << line.Data() << endl;//<< " " << binMin->String().Atof() <<" " << binCenter << " " << binWidth << endl;
85 fGraphStat->SetPoint(ipoints, binCenter, value->String().Atof());
86 fGraphSyst->SetPoint(ipoints, binCenter, value->String().Atof());
87 ((TGraphAsymmErrors*)fGraphStat)->SetPointError(ipoints,
90 stat->String().Atof(),
91 stat->String().Atof());
92 if(syst) ((TGraphAsymmErrors*)fGraphSyst)->SetPointError(ipoints,
95 syst->String().Atof(),
96 syst->String().Atof());
104 AliHEPDataParser::~AliHEPDataParser(){
106 if(fHistStat) delete fHistStat;
107 if(fHistSyst) delete fHistSyst;
108 if(fGraphStat) delete fGraphStat;
109 if(fGraphSyst) delete fGraphSyst;
110 if(fHEPDataFileLines) delete fHEPDataFileLines;
113 void AliHEPDataParser::SaveHEPDataFile(const char * hepfileName, Bool_t trueUseGraphFalesUseHisto) {
114 // Fills fHEPDataFileLines and saves its content to a file
115 if(!fHEPDataFileLines) fHEPDataFileLines = new TObjArray;
116 if(trueUseGraphFalesUseHisto) {
118 AliError("Graph not set");
121 Bool_t asym = kFALSE; // check if this has asymmetric errors
122 if (!strcmp(fGraphStat->ClassName(), "TGraphErrors")) asym = kFALSE;
123 else if (!strcmp(fGraphStat->ClassName(), "TGraphAsymmErrors")) asym = kTRUE;
124 else {AliError("Unsupported graph type"); return;}
125 Int_t npoint = fGraphStat->GetN();
126 if(asym) AliInfo("Assymmetric errors");
127 for(Int_t ipoint = 0; ipoint < npoint; ipoint++){
131 fHEPDataFileLines->Add(new TObjString(Form("BinCenter %s +stat -stat +syst -syst", fValueName.Data())));
133 fHEPDataFileLines->Add(new TObjString(Form("BinCenter %s +-stat +-syst", fValueName.Data())));
137 fHEPDataFileLines->Add(new TObjString(Form("BinCenter %s +stat -stat", fValueName.Data())));
139 fHEPDataFileLines->Add(new TObjString(Form("BinCenter %s +-stat", fValueName.Data())));
143 if(!fGraphStat->GetY()[ipoint]) continue;
144 TObjString * line = new TObjString;
147 line->String().Form("%f %f +%f -%f +%f -%f",
148 fGraphStat->GetX()[ipoint], fGraphStat->GetY()[ipoint],
149 ((TGraphAsymmErrors*)fGraphStat)->GetEYhigh()[ipoint],
150 ((TGraphAsymmErrors*)fGraphStat)->GetEYlow()[ipoint],
151 ((TGraphAsymmErrors*)fGraphSyst)->GetEYhigh()[ipoint],
152 ((TGraphAsymmErrors*)fGraphSyst)->GetEYlow()[ipoint]);
154 line->String().Form("%f %f +-%f +-%f",
155 fGraphStat->GetX()[ipoint], fGraphStat->GetY()[ipoint],
156 ((TGraphErrors*)fGraphStat)->GetEY()[ipoint],
157 ((TGraphErrors*)fGraphSyst)->GetEY()[ipoint]);
159 fHEPDataFileLines->Add(line);
162 line->String().Form("%f %f +%f -%f",
163 fGraphStat->GetX()[ipoint], fGraphStat->GetY()[ipoint],
164 ((TGraphAsymmErrors*)fGraphStat)->GetEYhigh()[ipoint], ((TGraphAsymmErrors*)fGraphStat)->GetEYlow()[ipoint]);
166 line->String().Form("%f %f +-%f",
167 fGraphStat->GetX()[ipoint], fGraphStat->GetY()[ipoint],
168 ((TGraphErrors*)fGraphStat)->GetEY()[ipoint]);
171 fHEPDataFileLines->Add(line);
177 AliError("Hist not set");
180 Int_t nbin = fHistStat->GetNbinsX();
182 for(Int_t ibin = 1; ibin <= nbin; ibin++){
185 fHEPDataFileLines->Add(new TObjString(Form("BinLow BinHigh %s +-stat +-syst", fValueName.Data())));
187 fHEPDataFileLines->Add(new TObjString(Form("BinLow BinHigh %s +-stat", fValueName.Data())));
190 if(!fHistStat->GetBinContent(ibin)) continue;
191 TObjString * line = new TObjString;
193 line->String().Form("%f %f %f +-%f +-%f",
194 fHistStat->GetBinLowEdge(ibin),
195 fHistStat->GetBinLowEdge(ibin)+fHistStat->GetBinWidth(ibin),
196 fHistStat->GetBinContent(ibin), fHistStat->GetBinError(ibin), fHistSyst->GetBinError(ibin));
197 fHEPDataFileLines->Add(line);
199 line->String().Form("%f %f %f +-%f",
200 fHistStat->GetBinLowEdge(ibin),
201 fHistStat->GetBinLowEdge(ibin)+fHistStat->GetBinWidth(ibin),
202 fHistStat->GetBinContent(ibin), fHistStat->GetBinError(ibin));
203 fHEPDataFileLines->Add(line);
209 TIterator * lineIter = fHEPDataFileLines->MakeIterator();
210 TObjString * obj = 0;
212 outfile.open (hepfileName);
213 cout << "Saving HEP File " << hepfileName << endl;
215 while ((obj = (TObjString*) lineIter->Next())) {
216 cout << obj->String().Data() << endl;
217 outfile << obj->String().Data() << endl;