Passing histograms between functions?

Hi!
I am creating a histogram in a function located in a .h include file of mine. I call that function from a macro, and would like to be able to pass the histogram, or a pointer to it, as the return value of the function that creates it. The reason is that the shape of the histogram depends on a parameter that is passed to the function creating it from the macro. How to do this? Can I define a function as being of datatype TH1F for example? Now I simply save the hsitogram on disc and opens that file again in the macro. Grateful for any advice.

kindest regards,
Axel

Axel,

Could you not define the function the following way?

 TH1F* blah_z_blah_function(<PARAMETERS>) {

TH1F *blah = new TH1F("My_Hist", "Histofgram", 260, 0,260);

return(blah);

 }

On the inside of the function you would just need to use the "new" operator to allocate the memory.  You will have to make sure to use the "delete" operator when you are finished with the histogram.

root:1> .x MyMACRO.root
root:2> TH1F *tmp = blah_z_blah();
root:3>
root:3> TH1F->Draw();

Now clean up the memory:

root:4> delete(tmp);

Be carefull not ot “delete” it more than once, that could cause a segfault.

Justace Clutter

P.S. I hope that is all correct :confused:

Opps…

root:3> TH1F->Draw();

Should be the following…

root:3> tmp->Draw();

Justace

Hi, thanks for helping!
I have tried similar things, and now I tried it exactly as you wrote it (except that the line “.x MyMACRO.root” should have been “.L MyMACRO.C” I guess the “.x” version doesn’t work since the input parameters are missing). The problem seems to be that since the script where “new TH1F(…” is located is a named script the histogram is erased when the script finishes. Thus, the histogram address IS passed to my calling script, but the histigram is then empty! I guess one could solve this by baking the script un-named, but I really prefer not to. Any ideas?

thanks in advance,
Axel

I have created and example and added it as an atachment.
Here is the output…

bash-2.05b$ root


  •                                     *
    
  •    W E L C O M E  to  R O O T       *
    
  •                                     *
    
  • Version 3.05/07 14 September 2003 *
  •                                     *
    
  • You are welcome to visit our Web site *
  •      [root.cern.ch](http://root.cern.ch)            *
    
  •                                     *
    

FreeType Engine v2.1.3 used to render TrueType fonts.
Compiled for linux with thread support.

CINT/ROOT C/C++ Interpreter version 5.15.94, June 30 2003
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] .L ./MyHist.C
root [1] TH1F *blah = MyMacro(200)
root [2] blah->Draw();
TCanvas::MakeDefCanvas: created default TCanvas with name c1
root [3]
MyHist.C (216 Bytes)

It works. I’m very grateful for your advice and sample code!

cheers,
Axel