Hi all,
I have several functions that do return a pointer to the histogram that draw the axis in the current canvas, in order to modify the axis properties.
What I want to do works successfully when I return THStack::GetHistogram() or TGraph::GetHistogram() but not in the example below:
#include "TH1.h"
#include "TCanvas.h"
#include "TROOT.h"
#include "TStyle.h"
#include <vector>
bool HistScaledGT ( TH1* h1, TH1* h2 )
{
return (h1->GetMaximum()/h1->Integral()) < (h2->GetMaximum()/h2->Integral());
}
template <class T>
T* DrawNormHists( const std::vector<T*> &h_vec )
{
typename std::vector<T*>::const_iterator it, it2;
it = max_element( h_vec.begin(), h_vec.end(), HistScaledGT );
if( it != h_vec.end() )
{
(*it)->DrawNormalized("hist"); // Drawn with axis
for( it2 = h_vec.begin() ; it2 != h_vec.end() ; it2++ )
if( (*it2)->Integral() > 0 ) (*it2)->DrawNormalized("hist same A");
}
return *it; // Return histogram used to build axis
}
int main()
{
gROOT->SetStyle("Plain");
// gStyle->SetOptTitle(0);
gStyle->SetOptStat(0);
std::vector<TH1F*> h_vec;
h_vec.push_back( new TH1F("h1", "h1", 50, -10, 10) );
h_vec.back()->FillRandom("gaus");
h_vec.push_back( new TH1F("h2", "h2", 50, -10, 10) );
h_vec.back()->FillRandom("landau");
h_vec.push_back( new TH1F("h3", "h3", 50, -10, 10) );
h_vec.back()->FillRandom("expo"); // This will be the maximum
h_vec.back()->SetTitle("InitTitle");
TCanvas *c = new TCanvas("tmp","", 600, 600);
TH1* f = DrawNormHists( h_vec );
// These don't work. Why ?
f->SetTitle("A title");
f->GetXaxis()->SetTitle("Hello");
c->Modified();
c->Update();
c->ForceUpdate(); // Even after this
c->Print(".png");
return 0;
}
What am I doing wrong ?
Is there another option to draw a bunch of normalized histograms so that they plot nicely. I could fill a THStack with scaled histograms but I’d rather avoid duplicating the histograms.
Is there any reason why THStack has no DrawNormalized() member function?
I am using ROOT 5.18/00 trunk@21744 on MacOS X. Is it time to upgrade ?
Thanks in advance for your help.
Karolos