00001
00002
00003
00004
00005
00007
00008 #ifdef __HP_aCC
00009 #include <Stiostream.h>
00010 #else
00011 #include "Stiostream.h"
00012 #endif
00013 #include "StMultiH1F.h"
00014 #include "TString.h"
00015 #include "TLegend.h"
00016 #include "TPad.h"
00017 #include "TDirectory.h"
00018 #include "TMath.h"
00019 ClassImp(StMultiH1F)
00020
00021 StMultiH1F::StMultiH1F() : fMOffset(0.) {}
00022
00023 StMultiH1F::StMultiH1F(const char *name,const char *title,Int_t nbinsx,
00024 Axis_t xlow,Axis_t xup ,Int_t nbinsy) :
00025 TH2F(name,title,nbinsx,xlow,xup,nbinsy,-0.5,-0.5+nbinsy), fMOffset(0.) {}
00026
00027 StMultiH1F::StMultiH1F(const char *name,const char *title,Int_t nbinsx,
00028 Double_t *xbins,Int_t nbinsy) :
00029 TH2F(name,title,nbinsx,xbins,nbinsy,-0.5,-0.5+nbinsy), fMOffset(0.) {}
00030
00031 void StMultiH1F::Draw(Option_t *option) {
00032
00033 TAxis* axis = GetXaxis();
00034 Int_t x0 = axis->GetFirst();
00035 Int_t x1 = axis->GetLast();
00036 axis->SetRange();
00037 Int_t ybins = GetNbinsY();
00038 if (ybins == 1) {
00039 TH1F* temp0 = XProjection(GetName());
00040 temp0->SetStats((!TestBit(kNoStats)));
00041 TAxis* taxis = temp0->GetXaxis();
00042 axis->Copy(*taxis);
00043 taxis->SetRange(x0,x1);
00044 axis->SetRange(x0,x1);
00045 temp0->Draw();
00046 return;
00047 }
00048
00049
00050
00051
00052
00053 TLegend *legend = new TLegend(0.80,0.84,0.98,0.98,"Legend","NDC");
00054 legend->SetFillColor(0);
00055 legend->SetFillStyle(0);
00056 legend->SetMargin(0.25);
00057
00058 Int_t ybin;
00059 Double_t maxval = -1e31;
00060 Double_t minval = 1e31;
00061 Int_t maxbin = -1;
00062 Int_t minbin = -1;
00063 Float_t offset = fMOffset;
00064 if (fMOffset && gPad->GetLogy()) {
00065 Float_t max_offset = TMath::Power(
00066 1.0e10*GetNonZeroMinimum()/GetNonZeroMaximum(),
00067 1.0/(ybins-1.0));
00068 if (offset > max_offset) offset = max_offset;
00069 }
00070
00071
00072 TH1F** temp = new TH1F*[ybins];
00073
00074 TString n0;
00075 for (ybin=0; ybin<ybins; ybin++) {
00076 if ((ybin >= 10) || (names[ybin].IsNull())) n0 = GetName();
00077 else n0 = names[ybin];
00078 Int_t slice = ybin+1;
00079 temp[ybin] = XProjection(n0.Data(),slice);
00080 temp[ybin]->SetLineStyle(slice);
00081 temp[ybin]->SetStats(kFALSE);
00082 TAxis* taxis = temp[ybin]->GetXaxis();
00083 axis->Copy(*taxis);
00084 taxis->SetRange(x0,x1);
00085
00086 if (fMOffset && ybin) {
00087 temp[ybin]->SetLineColor(slice);
00088 if (gPad->GetLogy()) {
00089 temp[ybin]->Scale(TMath::Power(offset,ybin));
00090 } else {
00091 for (Int_t xbin=0; xbin<GetNbinsX(); xbin++)
00092 temp[ybin]->AddBinContent(xbin,offset*ybin);
00093 }
00094 }
00095
00096 Double_t binmax = temp[ybin]->GetMaximum();
00097 Double_t binmin = temp[ybin]->GetMinimum();
00098 if (binmax > maxval) { maxval = binmax; maxbin = ybin; }
00099 if (binmin < minval) { minval = binmin; minbin = ybin; }
00100 legend->AddEntry(temp[ybin],n0.Data(),"l");
00101 }
00102
00103
00104
00105 if (maxbin == minbin) {
00106 temp[maxbin]->Draw();
00107 } else {
00108 TH1F* tempb = new TH1F(*(temp[maxbin]));
00109 tempb->SetName(Form("%s_%d",GetName(),ybins+1));
00110 tempb->SetBinContent(1,maxval);
00111 tempb->SetBinContent(2,minval);
00112 tempb->SetMarkerStyle(1); tempb->SetMarkerColor(0);
00113 tempb->Draw("p");
00114 maxbin = -1;
00115 }
00116 for (ybin=0; ybin<ybins; ybin++) {
00117 if (ybin != maxbin) temp[ybin]->Draw("same");
00118 }
00119
00120
00121 if (!TestBit(kNoStats)) {
00122 temp[0] = XProjection(GetName());
00123 temp[0]->SetEntries(GetEntries());
00124 temp[0]->SetStats(kTRUE);
00125 temp[0]->Draw("boxsames");
00126 legend->SetX1(0.59);
00127 legend->SetX2(0.77);
00128 }
00129
00130 legend->Draw();
00131
00132 axis->SetRange(x0,x1);
00133 }
00134
00135 TH1F* StMultiH1F::XProjection(const char* name, Int_t ybin) {
00136 static char buf[256];
00137 if (ybin<0) sprintf(buf,"%s.",name);
00138 else sprintf(buf,"%s_%d_%s",GetName(),ybin,name);
00139
00140 TList* tgList = gDirectory->GetList();
00141 TH1F* temp = (TH1F*) tgList->FindObject(buf);
00142 if (temp) tgList->Remove(temp);
00143
00144 if (ybin<0) temp = (TH1F*) ProjectionX(buf);
00145 else temp = (TH1F*) ProjectionX(buf,ybin,ybin);
00146 TAttLine::Copy(*temp);
00147 TAttFill::Copy(*temp);
00148 TAttMarker::Copy(*temp);
00149 return temp;
00150 }
00151
00152 void StMultiH1F::SetBarOffset(Float_t offset) {
00153 if (offset == 0.25) {
00154 fMOffset = 1.2 * (GetMaximum() - GetMinimum());
00155 if (!fMOffset) fMOffset = 10.0;
00156 } else {
00157 fMOffset = offset;
00158 }
00159 }
00160
00161 Double_t StMultiH1F::GetNonZeroMinimum() const {
00162 Double_t value, minimum = GetMinimum();
00163 if (minimum) return minimum;
00164 minimum = GetMaximum();
00165 Int_t bin, binx, biny, binz;
00166 Int_t xfirst = fXaxis.GetFirst();
00167 Int_t xlast = fXaxis.GetLast();
00168 Int_t yfirst = fYaxis.GetFirst();
00169 Int_t ylast = fYaxis.GetLast();
00170 Int_t zfirst = fZaxis.GetFirst();
00171 Int_t zlast = fZaxis.GetLast();
00172 for (binz=zfirst;binz<=zlast;binz++) {
00173 for (biny=yfirst;biny<=ylast;biny++) {
00174 for (binx=xfirst;binx<=xlast;binx++) {
00175 bin = GetBin(binx,biny,binz);
00176 value = GetBinContent(bin);
00177 if (value && value < minimum) minimum = value;
00178 }
00179 }
00180 }
00181 if (!minimum) minimum = -1.0;
00182 return minimum;
00183 }
00184
00185 Double_t StMultiH1F::GetNonZeroMaximum() const {
00186 Double_t value, maximum = GetMaximum();
00187 if (maximum) return maximum;
00188 maximum = GetMinimum();
00189 Int_t bin, binx, biny, binz;
00190 Int_t xfirst = fXaxis.GetFirst();
00191 Int_t xlast = fXaxis.GetLast();
00192 Int_t yfirst = fYaxis.GetFirst();
00193 Int_t ylast = fYaxis.GetLast();
00194 Int_t zfirst = fZaxis.GetFirst();
00195 Int_t zlast = fZaxis.GetLast();
00196 for (binz=zfirst;binz<=zlast;binz++) {
00197 for (biny=yfirst;biny<=ylast;biny++) {
00198 for (binx=xfirst;binx<=xlast;binx++) {
00199 bin = GetBin(binx,biny,binz);
00200 value = GetBinContent(bin);
00201 if (value && value > maximum) maximum = value;
00202 }
00203 }
00204 }
00205 if (!maximum) maximum = -1.0;
00206 return maximum;
00207 }
00208
00209 void StMultiH1F::SavePrimitive(ostream& out, Option_t* option) {
00210
00211
00212 Bool_t nonEqiX = kFALSE;
00213 Int_t i;
00214
00215
00216
00217 if (GetXaxis()->GetXbins()->fN && GetXaxis()->GetXbins()->fArray) {
00218 nonEqiX = kTRUE;
00219 out << " Double_t xAxis[" << GetXaxis()->GetXbins()->fN
00220 << "] = {";
00221 for (i = 0; i < GetXaxis()->GetXbins()->fN; i++) {
00222 if (i != 0) out << ", ";
00223 out << GetXaxis()->GetXbins()->fArray[i];
00224 }
00225 out << "}; " << endl;
00226 }
00227
00228 char quote = '"';
00229 out <<" "<<endl;
00230 out <<" TH1 *" << GetName() << " = new " << ClassName() << "("
00231 << quote << GetName() << quote << "," << quote << GetTitle() << quote
00232 << "," << GetXaxis()->GetNbins();
00233 if (nonEqiX)
00234 out << ", xAxis";
00235 else
00236 out << "," << GetXaxis()->GetXmin()
00237 << "," << GetXaxis()->GetXmax();
00238 out << "," << GetYaxis()->GetNbins() << ");" << endl;
00239
00240
00241 Int_t bin;
00242 for (bin=0;bin<fNcells;bin++) {
00243 Double_t bc = GetBinContent(bin);
00244 if (bc) {
00245 out<<" "<<GetName()<<"->SetBinContent("<<bin<<","<<bc<<");"<<endl;
00246 }
00247 }
00248
00249
00250 if (fSumw2.fN) {
00251 for (bin=0;bin<fNcells;bin++) {
00252 Double_t be = GetBinError(bin);
00253 if (be) {
00254 out <<" "<<GetName()<<"->SetBinError("<<bin<<","<<be<<");"<<endl;
00255 }
00256 }
00257 }
00258
00259 for (bin=0;bin<GetYaxis()->GetNbins();bin++) {
00260 if (!(names[bin].IsNull()))
00261 out <<" "<<GetName()<< "->Rebin(" << bin << ","
00262 << quote << names[bin] << quote << ");" << endl;
00263 }
00264 if (fMOffset != 0)
00265 out <<" "<<GetName()<< "->SetBarOffset(" << fMOffset << ");" << endl;
00266
00267 TH1::SavePrimitiveHelp(out, option);
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314