What Does ROOT TH1::SetDirectory Do?

  • Thread starter Thread starter ChrisVer
  • Start date Start date
  • Tags Tags
    Root
Click For Summary
SUMMARY

The ROOT framework's TH1::SetDirectory function controls the association of histograms with a directory in memory. When histograms are created, they are linked to the current directory, which can lead to issues when the directory is deleted, causing the histograms to be lost. By using SetDirectory(0), as demonstrated in the provided macro, histograms can be detached from the directory, ensuring they remain accessible even if the directory is deleted. This approach resolves the issue of empty canvases when drawing histograms.

PREREQUISITES
  • Familiarity with ROOT framework and its histogram classes (e.g., TH1F)
  • Understanding of C++ programming and basic syntax
  • Knowledge of TCanvas and TFile classes in ROOT
  • Concept of memory management in programming, particularly regarding object ownership
NEXT STEPS
  • Explore ROOT's documentation on TH1::SetDirectory for deeper insights
  • Learn about memory management practices in C++ to optimize histogram usage
  • Investigate the implications of using TFile and TCanvas in ROOT applications
  • Study advanced histogram manipulation techniques in ROOT, such as merging and grouping
USEFUL FOR

Data analysts, physicists, and software developers working with the ROOT framework who need to manage histograms effectively in their data visualization and analysis tasks.

ChrisVer
Science Advisor
Messages
3,372
Reaction score
465
Can someone explain me what SetDirectory actually does?

I found the problem with a macro like that:

C:
#include<iostream>
#include <TH1F.h>
#include <TCanvas.h>
#include <TFile.h>

int histoSum(){

//create histograms to use
TH1F* histo1 = new TH1F("histo1",";A", 100, 0,10);
TH1F* histo2 = new TH1F("histo2",";B", 100, 0,10);
TH1F* histoSUM = new TH1F("histoSUM",";C", 100, 0,10);

//call histograms from a root file
TFile f ("histograms.root");
f.GetObject("h_A_events",histo1);
f.GetObject("h_B_events",histo2);

//add the histograms of A,B
histo1->sumw2();
histo2->sumw2();
histoSUM->Add(histo1,histo2);

//print the histogram of SUM on canvas
TCanvas * c= new TCanvas("sum_histos",900,700);
histoSUM->Draw();

return 0;
}

The problem when I ran the program like this, is that the Canvas appeared empty...
On the other hand if I set for my histos histo1,histo2,histoSUM the ->SetDirectory(0)

C:
[...]
f.GetObject("h_B_events",histo2);

histo1->SetDirectory(0);
histo2->SetDirectory(0);
histoSUM->SetDirectory(0);

//add the histograms of A,B
histo1->sumw2();
[...]

the histogram appeared in the canvas...
I only found this:
https://root.cern.ch/drupal/content/histograms-and-current-directory
but it doesn't help me understand how it works.
 
Last edited by a moderator:
Technology news on Phys.org
My reading of it is that the Directory is a means to group histograms together for easier organized operations. Specifically, it says that if you delete the Directory all associated histograms are deleted as well.

It seems to work like a filesystem with histograms representing files and Directories representing filesystem subdirectories. In the filesystem paradigm, one can work with files as a group with a common subdirectory name ie one can copy files or on can delete the subdirectory to delete its files (some cautions may apply ie might have to force the operation).

This page talks about changing Directory to get a different set of histograms:

https://root.cern.ch/drupal/content/subdirectories-and-navigation
 
Last edited by a moderator:

Similar threads

Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
4
Views
11K
  • · Replies 49 ·
2
Replies
49
Views
12K
  • · Replies 7 ·
Replies
7
Views
8K
  • · Replies 67 ·
3
Replies
67
Views
17K
Replies
2
Views
6K
  • · Replies 32 ·
2
Replies
32
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K