Re: Sorting and IsEqual: AN EXAMPLE

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Dec 10 1999 - 08:26:26 MET


Hi Anthony,
If the only thing you want to do is to sort histograms, you do not need
to create a derived class from TH1F. The following macro is a short
example.

Rene Brun

//example of macro illustrating how to sort histograms by their bin
contents
void hsort()
{
   //create 100 histograms and fill them with a gaus distribution
   const Int_t nh = 100;
   TH1F *h[nh];
   Int_t i,j;
   char name[8];
   for (i=0;i<nh;i++) {
      sprintf(name,"h%d",i);
      h[i] = new TH1F(name,"",100,-4,4);
      for (j=0;j<10000;j++) h[i]->Fill(gRandom->Gaus(0,1));
   }

   //get maximum of each histogram into a local array
   Float_t hmax[nh];
   for (i=0;i<nh;i++) hmax[i] = h[i]->GetMaximum();
   
   //sort histograms. Store in a TObjArray
   Int_t index[nh];
   TMath::Sort(nh,hmax,index);
   TObjArray *hlist = new TObjArray(nh); //will contain sorted
histograms
   for (i=0;i<nh;i++) {
      printf("i=%d, index=%d, max=%f\n",i,index[i],hmax[index[i]]);
      hlist->Add(h[i]); 
   }
}      
            
Anthony Faust wrote:
> 
> Hello
> 
>         I have defined a sortable histogram class, derived from TH1F, which will
> sort based on the maximum bin content. My first try at writing the IsEqual and
> Compare functions are as follows
> 
> Bool_t TH1Fs::IsEqual(TObject *obj)
> {
>   Float_t a = GetMaximum();
>   Float_t b = ((TH1F*)obj)->GetMaximum();
>   return ( a == b ) ;
> }
> 
> Int_t TH1Fs::Compare(TObject* obj)
> {
>   Float_t a = TH1F::GetMaximum();
>   Float_t b = ((TH1F*)obj)->GetMaximum();
>   return ( (a == b) ? 0 : ( (a > b) ? 1 : -1 ) ) ;
> }
> 
> I then load up a TObjArray with TH1Fs objects and call the Sort function, but
> it crashes in IsEqual when it tries to call
> 
> Float_t b = ((TH1F*)obj)->GetMaximum();
> 
> The cast seems to match the one in define in TObjNum of
> http://root.cern.ch/root/html/examples/tcollex.C.html, except the example
> referenced a data member and did not call a function.
> 
>         What am I missing?      Thanks for any help.
> 
> _____________
> Dr. Anthony A. Faust                            | Anthony.Faust@dres.dnd.ca
> Defence Research Establishment Suffield         | PH:  (403) 544-5362
> Box 4000, Medicine Hat, Alberta, CANADA T1A 8K6 | FAX: (403) 544-4704



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:44 MET