What Does ROOT TH1::SetDirectory Do?

  • Thread starter ChrisVer
  • Start date
  • Tags
    Root
In summary, the SetDirectory function in this conversation allows for the manipulation and organization of histograms in a similar way to a filesystem. It can help with grouping and performing operations on multiple histograms at once. By setting the directory to 0, the histograms appear on the canvas for easier visualization.
  • #1
ChrisVer
Gold Member
3,378
464
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
  • #2
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:

FAQ: What Does ROOT TH1::SetDirectory Do?

1. What is the purpose of ROOT TH1::SetDirectory?

The ROOT TH1::SetDirectory function allows you to set the output directory for histograms created in ROOT. This means that any histograms created with this function will be saved in the specified directory instead of the default directory.

2. How do I use ROOT TH1::SetDirectory?

To use ROOT TH1::SetDirectory, you first need to create a ROOT TFile object that represents the output file. Then, you can call the SetDirectory function with the TFile object as the argument. This will set the output directory for any histograms created after this call.

3. Can I change the output directory multiple times in one script?

Yes, you can call the ROOT TH1::SetDirectory function multiple times in one script. Each call will change the output directory for any histograms created after that call. However, it is important to note that the last call to SetDirectory will be the one that is used for all histograms.

4. Will calling ROOT TH1::SetDirectory affect previously created histograms?

No, calling the ROOT TH1::SetDirectory function will only affect histograms created after the call. Any previously created histograms will remain in their original output directory.

5. What happens if I don't use ROOT TH1::SetDirectory?

If you don't use ROOT TH1::SetDirectory, histograms will be saved in the default output directory. This is usually the same directory as the script being executed. However, it is recommended to use SetDirectory to specify a different output directory for better organization and management of your histograms.

Similar threads

Replies
49
Views
10K
Replies
7
Views
8K
2
Replies
67
Views
11K
Replies
1
Views
3K
Back
Top