class THStack: public TNamed

The Histogram stack class

A THStack is a collection of TH1 or TH2 histograms. Using THStack::Draw() the histogram collection is drawn in one go according to the drawing option.

THStack::Add() allows to add a new histogram to the list. The THStack does not own the objects in the list.

By default (if no option drawing option is specified), histograms will be paint stacked on top of each other. TH2 If option "nostack" is specified the histograms are not drawn on top of each other but as they would if drawn using the option "same".

If option "nostackb" is specified the histograms are drawn next to each other as bar charts.

In all cases The axis range is computed automatically along the X and Y axis in order to show the complete histogram collection.


output of html604/MACRO_THStack_1.gif
   THStack *hs = new THStack("hs","");
   TH1F *h1 = new TH1F("h1","test hstack",10,-4,4);
   TH1F *h2 = new TH1F("h2","test hstack",10,-4,4);
   TH1F *h3 = new TH1F("h3","test hstack",10,-4,4);
   TCanvas *cs = new TCanvas("cs","cs",10,10,700,900);
   TText T; T.SetTextFont(42); T.SetTextAlign(21);
   cs->cd(1); hs->Draw(); T.DrawTextNDC(.5,.95,"Default drawing option");
   cs->cd(2); hs->Draw("nostack"); T.DrawTextNDC(.5,.95,"Option \"nostack\"");
   cs->cd(3); hs->Draw("nostackb"); T.DrawTextNDC(.5,.95,"Option \"nostackb\"");
   cs->cd(4); hs->Draw("lego1"); T.DrawTextNDC(.5,.95,"Option \"lego1\"");
   return cs;
A more complex example:
output of html604/MACRO_THStack_3.gif
TCanvas *hstack() {
// Example of stacked histograms: class THStack
//  Author: Rene Brun

   THStack *hs = new THStack("hs","Stacked 1D histograms");
   //create three 1-d histograms
   TH1F *h1st = new TH1F("h1st","test hstack",100,-4,4);
   TH1F *h2st = new TH1F("h2st","test hstack",100,-4,4);
   TH1F *h3st = new TH1F("h3st","test hstack",100,-4,4);

   TCanvas *cst = new TCanvas("cst","stacked hists",10,10,700,700);
   // in top left pad, draw the stack with defaults
   // in top right pad, draw the stack in non-stack mode
   // and errors option
   //in bottom left, draw in stack mode with "lego1" option

   //create two 2-D histograms and draw them in stack mode
   THStack *a = new THStack("a","Stacked 2D histograms");
   TF2 *f1 = new TF2("f1",
      "xygaus + xygaus(5) + xylandau(10)",-4,4,-4,4);
   Double_t params1[] = {130,-1.4,1.8,1.5,1, 150,2,0.5,-2,0.5,
   TH2F *h2sta = new TH2F("h2sta","h2sta",20,-4,4,20,-4,4);
   TF2 *f2 = new TF2("f2","xygaus + xygaus(5)",-4,4,-4,4);
   Double_t params2[] = {100,-1.4,1.9,1.1,2, 80,2,0.7,-2,0.5};
   TH2F *h2stb = new TH2F("h2stb","h2stb",20,-4,4,20,-4,4);
   return cst;
Note that picking is supported for all drawing modes.

Function Members (Methods)

voidTObject::AbstractMethod(const char* method) const
virtual voidAdd(TH1* h, Option_t* option = "")
virtual voidTObject::AppendPad(Option_t* option = "")
virtual voidBrowse(TBrowser* b)
static TClass*Class()
virtual const char*TObject::ClassName() const
virtual voidTNamed::Clear(Option_t* option = "")
virtual TObject*TNamed::Clone(const char* newname = "") const
virtual Int_tTNamed::Compare(const TObject* obj) const
virtual voidTNamed::Copy(TObject& named) const
virtual voidTObject::Delete(Option_t* option = "")MENU
virtual Int_tDistancetoPrimitive(Int_t px, Int_t py)
virtual voidDraw(Option_t* chopt = "")
virtual voidTObject::DrawClass() constMENU
virtual TObject*TObject::DrawClone(Option_t* option = "") constMENU
virtual voidTObject::Dump() constMENU
virtual voidTObject::Error(const char* method, const char* msgfmt) const
virtual voidTObject::Execute(const char* method, const char* params, Int_t* error = 0)
virtual voidTObject::Execute(TMethod* method, TObjArray* params, Int_t* error = 0)
virtual voidTObject::ExecuteEvent(Int_t event, Int_t px, Int_t py)
virtual voidTObject::Fatal(const char* method, const char* msgfmt) const
virtual voidTNamed::FillBuffer(char*& buffer)
virtual TObject*TObject::FindObject(const char* name) const
virtual TObject*TObject::FindObject(const TObject* obj) const
virtual Option_t*TObject::GetDrawOption() const
static Long_tTObject::GetDtorOnly()
TH1*GetHistogram() const
TList*GetHists() const
virtual const char*TObject::GetIconName() const
virtual Double_tGetMaximum(Option_t* option = "")
virtual Double_tGetMinimum(Option_t* option = "")
virtual const char*TNamed::GetName() const
Int_tGetNhists() const
virtual char*TObject::GetObjectInfo(Int_t px, Int_t py) const
static Bool_tTObject::GetObjectStat()
virtual Option_t*TObject::GetOption() const
virtual const char*TNamed::GetTitle() const
virtual UInt_tTObject::GetUniqueID() const
TAxis*GetXaxis() const
TAxis*GetYaxis() const
virtual Bool_tTObject::HandleTimer(TTimer* timer)
virtual ULong_tTNamed::Hash() const
virtual voidTObject::Info(const char* method, const char* msgfmt) const
virtual Bool_tTObject::InheritsFrom(const char* classname) const
virtual Bool_tTObject::InheritsFrom(const TClass* cl) const
virtual voidTObject::Inspect() constMENU
voidTObject::InvertBit(UInt_t f)
virtual TClass*IsA() const
virtual Bool_tTObject::IsEqual(const TObject* obj) const
virtual Bool_tTObject::IsFolder() const
Bool_tTObject::IsOnHeap() const
virtual Bool_tTNamed::IsSortable() const
Bool_tTObject::IsZombie() const
virtual voidls(Option_t* option = "") const
voidTObject::MayNotUse(const char* method) const
virtual Long64_tMerge(TCollection* li, TFileMergeInfo* info)
virtual voidModified()
virtual Bool_tTObject::Notify()
voidTObject::Obsolete(const char* method, const char* asOfVers, const char* removedFromVers) const
voidTObject::operator delete(void* ptr)
voidTObject::operator delete(void* ptr, void* vp)
voidTObject::operator delete[](void* ptr)
voidTObject::operator delete[](void* ptr, void* vp)
void*TObject::operator new(size_t sz)
void*TObject::operator new(size_t sz, void* vp)
void*TObject::operator new[](size_t sz)
void*TObject::operator new[](size_t sz, void* vp)
virtual voidPaint(Option_t* chopt = "")
virtual voidTObject::Pop()
virtual voidPrint(Option_t* chopt = "") const
virtual Int_tTObject::Read(const char* name)
virtual voidRecursiveRemove(TObject* obj)
voidTObject::ResetBit(UInt_t f)
virtual voidTObject::SaveAs(const char* filename = "", Option_t* option = "") constMENU
virtual voidSavePrimitive(ostream& out, Option_t* option = "")
voidTObject::SetBit(UInt_t f)
voidTObject::SetBit(UInt_t f, Bool_t set)
virtual voidTObject::SetDrawOption(Option_t* option = "")MENU
static voidTObject::SetDtorOnly(void* obj)
virtual voidSetHistogram(TH1* h)
virtual voidSetMaximum(Double_t maximum = -1111)MENU
virtual voidSetMinimum(Double_t minimum = -1111)MENU
virtual voidTNamed::SetName(const char* name)MENU
virtual voidTNamed::SetNameTitle(const char* name, const char* title)
static voidTObject::SetObjectStat(Bool_t stat)
virtual voidTNamed::SetTitle(const char* title = "")MENU
virtual voidTObject::SetUniqueID(UInt_t uid)
virtual voidShowMembers(TMemberInspector& insp) const
virtual Int_tTNamed::Sizeof() const
virtual voidStreamer(TBuffer&)
voidStreamerNVirtual(TBuffer& ClassDef_StreamerNVirtual_b)
virtual voidTObject::SysError(const char* method, const char* msgfmt) const
Bool_tTObject::TestBit(UInt_t f) const
Int_tTObject::TestBits(UInt_t f) const
THStack(const THStack& hstack)
THStack(const char* name, const char* title)
THStack(TH1* hist, Option_t* axis = "x", const char* name = 0, const char* title = 0, Int_t firstbin = 1, Int_t lastbin = -1, Int_t firstbin2 = 1, Int_t lastbin2 = -1, Option_t* proj_option = "", Option_t* draw_option = "")
virtual voidTObject::UseCurrentStyle()
virtual voidTObject::Warning(const char* method, const char* msgfmt) const
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0)
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0) const
virtual voidTObject::DoError(int level, const char* location, const char* fmt, va_list va) const
THStack&operator=(const THStack&)

Data Members

static TObject::(anonymous)TObject::kBitMask
static TObject::EStatusBitsTObject::kCanDelete
static TObject::EStatusBitsTObject::kCannotPick
static TObject::EStatusBitsTObject::kHasUUID
static TObject::EStatusBitsTObject::kInvalidObject
static TObject::(anonymous)TObject::kIsOnHeap
static TObject::EStatusBitsTObject::kIsReferenced
static TObject::EStatusBitsTObject::kMustCleanup
static TObject::EStatusBitsTObject::kNoContextMenu
static TObject::(anonymous)TObject::kNotDeleted
static TObject::EStatusBitsTObject::kObjInCanvas
static TObject::(anonymous)TObject::kOverwrite
static TObject::(anonymous)TObject::kSingleKey
static TObject::(anonymous)TObject::kWriteDelete
static TObject::(anonymous)TObject::kZombie
TH1*fHistogramPointer to histogram used for drawing axis
TList*fHistsPointer to array of TH1
Double_tfMaximumMaximum value for plotting along y
Double_tfMinimumMinimum value for plotting along y
TStringTNamed::fNameobject identifier
TObjArray*fStack!Pointer to array of sums of TH1
TStringTNamed::fTitleobject title

Class Charts

Inheritance Inherited Members Includes Libraries
Class Charts

Function documentation

 THStack default constructor
THStack(const char* name, const char* title)
 constructor with name and title
THStack(TH1* hist, Option_t* axis = "x", const char* name = 0, const char* title = 0, Int_t firstbin = 1, Int_t lastbin = -1, Int_t firstbin2 = 1, Int_t lastbin2 = -1, Option_t* proj_option = "", Option_t* draw_option = "")
 Creates a new THStack from a TH2 or TH3
 It is filled with the 1D histograms from GetProjectionX or GetProjectionY
 for each bin of the histogram. It illustrates the differences and total
 sum along an axis.

 - hist:  the histogram used for the projections. Can be an object deriving
          from TH2 or TH3.
 - axis:  for TH2: "x" for ProjectionX, "y" for ProjectionY.
          for TH3: see TH3::Project3D.
 - name:  fName is set to name if given, otherwise to histo's name with
          "_stack_<axis>" appended, where <axis> is the value of the
          parameter axis.
 - title: fTitle is set to title if given, otherwise to histo's title
          with ", stack of <axis> projections" appended.
 - firstbin, lastbin:
          for each bin within [firstbin,lastbin] a stack entry is created.
          See TH2::ProjectionX/Y for use overflow bins.
          Defaults to "all bins but under- / overflow"
 - firstbin2, lastbin2:
          Other axis range for TH3::Project3D, defaults to "all bins but
          under- / overflow". Ignored for TH2s
 - proj_option:
          option passed to TH2::ProjectionX/Y and TH3::Project3D (along
          with axis)
 - draw_option:
          option passed to THStack::Add.
 THStack destructor
THStack(const THStack& hstack)
 THStack copy constructor
void Add(TH1* h, Option_t* option = "")
 add a new histogram to the list
 Only 1-d and 2-d histograms currently supported.
 A drawing option may be specified
void Browse(TBrowser* b)
void BuildStack()
  build sum of all histograms
  Build a separate list fStack containing the running sum of all histograms
Int_t DistancetoPrimitive(Int_t px, Int_t py)
 Compute distance from point px,py to each graph

void Draw(Option_t* chopt = "")
 Draw this multihist with its current attributes.

   Options to draw histograms  are described in THistPainter::Paint
 By default (if option "nostack" is not specified), histograms will be paint
 stacked on top of each other.
TH1 * GetHistogram() const
    Returns a pointer to the histogram used to draw the axis
    Takes into account the two following cases.
       1- option 'A' was specified in THStack::Draw. Return fHistogram
       2- user had called TPad::DrawFrame. return pointer to hframe histogram

 - You must call Draw before calling this function. The returned histogram
   depends on the selected Draw options.
 - This function returns a pointer to an intermediate fixed bin size
   histogram used to set the range and for picking.
   You cannot use this histogram to return the bin information.
   You must get a pointer to one of the histograms in the stack,
   the first one, for example.
Double_t GetMaximum(Option_t* option = "")
  returns the maximum of all added histograms
  returns the maximum of all histograms if option "nostack".
Double_t GetMinimum(Option_t* option = "")
  returns the minimum of all added histograms
  returns the minimum of all histograms if option "nostack".
Int_t GetNhists() const
 Return the number of histograms in the stack
TObjArray * GetStack()
 Return pointer to Stack. Build it if not yet done
TAxis * GetXaxis() const
 Get x axis of the histogram used to draw the stack.

  You must call Draw before calling this function. The returned histogram
  depends on the selected Draw options.
TAxis * GetYaxis() const
 Get x axis of the histogram used to draw the stack.

  You must call Draw before calling this function. The returned histogram
  depends on the selected Draw options.
void ls(Option_t* option = "") const
 List histograms in the stack
Long64_t Merge(TCollection* li, TFileMergeInfo* info)
 Merge the THStack in the TList into this stack.
 Returns the total number of histograms in the result or -1 in case of an error.
void Modified()
 invalidate sum of histograms
void Paint(Option_t* chopt = "")
 Paint the list of histograms.
 By default, histograms are shown stacked.
    - the first histogram is paint
    - then the sum of the first and second, etc

 If option "nostack" is specified, histograms are all paint in the same pad
 as if the option "same" had been specified.

 If the option nostackb is specified histograms are all paint in the same pad
 next to each other as bar plots.

 if option "pads" is specified, the current pad/canvas is subdivided into
 a number of pads equal to the number of histograms and each histogram
 is paint into a separate pad.

 By default the background of the histograms is erased before drawing the
 histograms. The option "noclear" avoid this behaviour. This is useful
 when drawing a THStack on top of an other plot. If the patterns used to
 draw the histograms in the stack are transparents, then the plot behind
 will be visible.

 See THistPainter::Paint for a list of valid options.
void Print(Option_t* chopt = "") const
 Print the list of histograms
void RecursiveRemove(TObject* obj)
 Recursively remove object from the list of histograms
void SavePrimitive(ostream& out, Option_t* option = "")
 Save primitive as a C++ statement(s) on output stream out
void SetMaximum(Double_t maximum = -1111)
 Set maximum.
void SetMinimum(Double_t minimum = -1111)
 Set minimum.
THStack& operator=(const THStack& )
TList * GetHists() const
{ return fHists; }
void SetHistogram(TH1* h)
{fHistogram = h;}