13 #include "StMultiH1F.h"
17 #include "TDirectory.h"
23 StMultiH1F::StMultiH1F(
const char *name,
const char *title,Int_t nbinsx,
24 Axis_t xlow,Axis_t xup ,Int_t nbinsy) :
25 TH2F(name,title,nbinsx,xlow,xup,nbinsy,-0.5,-0.5+nbinsy),
26 fMOffset(0.), subHists(0), aHist(0) {}
28 StMultiH1F::StMultiH1F(
const char *name,
const char *title,Int_t nbinsx,
29 Double_t *xbins,Int_t nbinsy) :
30 TH2F(name,title,nbinsx,xbins,nbinsy,-0.5,-0.5+nbinsy),
31 fMOffset(0.), subHists(0), aHist(0) {}
33 StMultiH1F::~StMultiH1F() {
35 int ybins = TMath::Min(GetNbinsY(),StMultiH1FMaxBins);
36 for (
int ybin=0; ybin<ybins; ybin++)
delete subHists[ybin];
40 void StMultiH1F::Draw(Option_t *option) {
42 int x0 = fXaxis.GetFirst();
43 int x1 = fXaxis.GetLast();
45 int ybins = TMath::Min(GetNbinsY(),StMultiH1FMaxBins);
48 if (!subHists) { subHists =
new TH1F*[ybins]; memset(subHists,0,ybins*
sizeof(TH1F*)); }
52 subHists[0] = XProjection(GetName());
53 subHists[0]->SetStats((!TestBit(kNoStats)));
54 TAxis* taxis = subHists[0]->GetXaxis();
56 taxis->SetRange(x0,x1);
57 fXaxis.SetRange(x0,x1);
58 if (fMinimum != -1111) subHists[0]->SetMinimum(fMinimum);
59 if (fMaximum != -1111) subHists[0]->SetMaximum(fMaximum);
68 TLegend *legend =
new TLegend(0.80,0.84,0.98,0.98,
"Legend",
"NDC");
69 legend->SetFillColor(0);
70 legend->SetFillStyle(0);
71 legend->SetMargin(0.25);
74 double maxval = -1e31;
78 float offset = fMOffset;
79 if (fMOffset && gPad->GetLogy()) {
80 float max_offset = TMath::Power(
81 1.0e10*GetNonZeroMinimum()/GetNonZeroMaximum(),
83 if (offset > max_offset) offset = max_offset;
87 for (ybin=0; ybin<ybins; ybin++) {
88 if (names[ybin].IsNull()) n0 = GetName();
89 else n0 = names[ybin];
91 delete subHists[ybin];
92 subHists[ybin] = XProjection(n0.Data(),slice);
93 subHists[ybin]->SetLineStyle(slice);
94 subHists[ybin]->SetStats(kFALSE);
95 TAxis* taxis = subHists[ybin]->GetXaxis();
97 taxis->SetRange(x0,x1);
99 if (fMOffset && ybin) {
100 subHists[ybin]->SetLineColor(slice);
101 if (gPad->GetLogy()) {
102 subHists[ybin]->Scale(TMath::Power(offset,ybin));
104 for (
int xbin=0; xbin<GetNbinsX(); xbin++)
105 subHists[ybin]->AddBinContent(xbin,offset*ybin);
109 double binmax = subHists[ybin]->GetMaximum();
110 double binmin = subHists[ybin]->GetMinimum();
111 if (binmax > maxval) { maxval = binmax; maxbin = ybin; }
112 if (binmin < minval) { minval = binmin; minbin = ybin; }
113 legend->AddEntry(subHists[ybin],n0.Data(),
"l");
118 if (maxbin == minbin) {
119 if (fMinimum != -1111) subHists[maxbin]->SetMinimum(fMinimum);
120 if (fMaximum != -1111) subHists[maxbin]->SetMaximum(fMaximum);
121 subHists[maxbin]->Draw();
124 aHist =
new TH1F(*(subHists[maxbin]));
125 aHist->SetName(Form(
"%s_%d",GetName(),ybins+1));
126 aHist->SetBinContent(1,maxval);
127 aHist->SetBinContent(2,minval);
128 aHist->SetMarkerStyle(1); aHist->SetMarkerColor(0);
129 if (fMinimum != -1111) aHist->SetMinimum(fMinimum);
130 if (fMaximum != -1111) aHist->SetMaximum(fMaximum);
134 for (ybin=0; ybin<ybins; ybin++) {
135 if (ybin != maxbin) subHists[ybin]->Draw(
"same");
139 if (!TestBit(kNoStats)) {
140 subHists[0] = XProjection(GetName());
141 subHists[0]->SetEntries(GetEntries());
142 subHists[0]->SetStats(kTRUE);
143 subHists[0]->Draw(
"boxsames");
150 fXaxis.SetRange(x0,x1);
153 TH1F* StMultiH1F::XProjection(
const char* name, Int_t ybin) {
154 static char buf[256];
155 if (ybin<0) sprintf(buf,
"%s.",name);
156 else sprintf(buf,
"%s_%d_%s",GetName(),ybin,name);
158 TList* tgList = gDirectory->GetList();
159 TH1F* temp = (TH1F*) tgList->FindObject(buf);
160 if (temp) tgList->Remove(temp);
162 if (ybin<0) temp = (TH1F*) ProjectionX(buf);
163 else temp = (TH1F*) ProjectionX(buf,ybin,ybin);
164 TAttLine::Copy(*temp);
165 TAttFill::Copy(*temp);
166 TAttMarker::Copy(*temp);
170 void StMultiH1F::SetBarOffset(Float_t offset) {
171 if (offset == 0.25) {
172 fMOffset = 1.2 * (GetMaximum() - GetMinimum());
173 if (!fMOffset) fMOffset = 10.0;
179 Double_t StMultiH1F::GetNonZeroMinimum()
const {
180 Double_t value, minimum = GetMinimum();
181 if (minimum)
return minimum;
182 minimum = GetMaximum();
183 int bin, binx, biny, binz;
184 int xfirst = fXaxis.GetFirst();
185 int xlast = fXaxis.GetLast();
186 int yfirst = fYaxis.GetFirst();
187 int ylast = TMath::Min(fYaxis.GetLast(),StMultiH1FMaxBins);
188 int zfirst = fZaxis.GetFirst();
189 int zlast = fZaxis.GetLast();
190 for (binz=zfirst;binz<=zlast;binz++) {
191 for (biny=yfirst;biny<=ylast;biny++) {
192 for (binx=xfirst;binx<=xlast;binx++) {
193 bin = GetBin(binx,biny,binz);
194 value = GetBinContent(bin);
195 if (value && value < minimum) minimum = value;
199 if (!minimum) minimum = -1.0;
203 Double_t StMultiH1F::GetNonZeroMaximum()
const {
204 Double_t value, maximum = GetMaximum();
205 if (maximum)
return maximum;
206 maximum = GetMinimum();
207 int bin, binx, biny, binz;
208 int xfirst = fXaxis.GetFirst();
209 int xlast = fXaxis.GetLast();
210 int yfirst = fYaxis.GetFirst();
211 int ylast = TMath::Min(fYaxis.GetLast(),StMultiH1FMaxBins);
212 int zfirst = fZaxis.GetFirst();
213 int zlast = fZaxis.GetLast();
214 for (binz=zfirst;binz<=zlast;binz++) {
215 for (biny=yfirst;biny<=ylast;biny++) {
216 for (binx=xfirst;binx<=xlast;binx++) {
217 bin = GetBin(binx,biny,binz);
218 value = GetBinContent(bin);
219 if (value && value > maximum) maximum = value;
223 if (!maximum) maximum = -1.0;
227 void StMultiH1F::SavePrimitive(ostream& out, Option_t* option) {
230 bool nonEqiX = kFALSE;
235 if (GetXaxis()->GetXbins()->fN && GetXaxis()->GetXbins()->fArray) {
237 out <<
" Double_t xAxis[" << GetXaxis()->GetXbins()->fN
239 for (i = 0; i < GetXaxis()->GetXbins()->fN; i++) {
240 if (i != 0) out <<
", ";
241 out << GetXaxis()->GetXbins()->fArray[i];
243 out <<
"}; " << endl;
248 out <<
" TH1 *" << GetName() <<
" = new " << ClassName() <<
"("
249 << quote << GetName() << quote <<
"," << quote << GetTitle() << quote
250 <<
"," << GetXaxis()->GetNbins();
254 out <<
"," << GetXaxis()->GetXmin()
255 <<
"," << GetXaxis()->GetXmax();
256 out <<
"," << GetYaxis()->GetNbins() <<
");" << endl;
260 for (bin=0;bin<fNcells;bin++) {
261 double bc = GetBinContent(bin);
263 out<<
" "<<GetName()<<
"->SetBinContent("<<bin<<
","<<bc<<
");"<<endl;
269 for (bin=0;bin<fNcells;bin++) {
270 double be = GetBinError(bin);
272 out <<
" "<<GetName()<<
"->SetBinError("<<bin<<
","<<be<<
");"<<endl;
277 for (bin=0;bin<GetYaxis()->GetNbins();bin++) {
278 if (!(names[bin].IsNull()))
279 out <<
" "<<GetName()<<
"->Rebin(" << bin <<
","
280 << quote << names[bin] << quote <<
");" << endl;
283 out <<
" "<<GetName()<<
"->SetBarOffset(" << fMOffset <<
");" << endl;
285 TH1::SavePrimitiveHelp(out, option);
291 int ybins = TMath::Min(GetNbinsY(),StMultiH1FMaxBins);
293 for (
int ybin=0; ybin<ybins; ybin++)
295 TString projection_name( names[ybin].IsNull() ? GetName() : names[ybin].Data() );
296 XProjection(projection_name.Data(), ybin+1)->
Write();
299 return TH2F::Write();
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Saves subhistograms (TH1F) along with this composite object.