Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

ROOT TH1::SetDirectory

  1. Jul 22, 2015 #1


    User Avatar
    Gold Member

    Can someone explain me what SetDirectory actually does?

    I found the problem with a macro like that:

    Code (C):

    #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");

    //add the histograms of A,B

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

    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)

    Code (C):



    //add the histograms of A,B
    the histogram appeared in the canvas...
    I only found this:
    https://root.cern.ch/drupal/content/histograms-and-current-directory [Broken]
    but it doesn't help me understand how it works.
    Last edited by a moderator: May 7, 2017
  2. jcsd
  3. Jul 22, 2015 #2


    Staff: Mentor

    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 [Broken]
    Last edited by a moderator: May 7, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - ROOT SetDirectory Date
How to plot generalized hypergeometric function in ROOT? May 4, 2017
Fortran Getting the cubic root Sep 23, 2016
C/++/# ROOT TLegend fill box Jul 31, 2016
Root histograms in multi-files Oct 16, 2015