Accessing a dynamically allocated ROOT object after reference lost

I have a

int script()
TH1F *h = new TH1F("h", "h", 11, -5, 5);
return 0;

I run the script as .x

How can I locate the object pointed to by h in Root memory after the script has ended and the reference h destroyed?

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

like so:

int script(h **TH1F) {
   *h = new TH1F(...);
   return 0;

or, return it directly:

TH1F *script() {
    return new TH1F(...);

Ok, but imagine in my real life script there are 20 TH1 objects and their list gets modified as I work on my code.

root [0] .x
(int) 0
root [1] .ls
 OBJ: TH1F	h	h : 0 at: 0x7fb13cc60ce0
root [2] h->Draw()
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [3] 

1 Like

globals are a pain in the neck to work with or retrofit afterwards in a “real” program.

I would suggest to either pass a std::vector<TH1F*>& or a std::map<std::string, TH1F*>& instead:

int script(std::vector<TH1F*>& histos) {
    histos.push_back(new TH1F(...));
    return 0;

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.