Search code examples
c++data-analysisroot-framework

TTree objects; using Draw options to histogram the difference


This macro works with ROOT (cern) TTree objects. Its aim is to display one histogram with another subtracted from it. The trees are friends. I am attempting to use the Draw() options to subtract one histogram from another;

tree1->Draw("hit_PMTid - plain.hit_PMTid");

However its making the wrong axis negative. The result looks like;

oddly symmetrical graph

As I know the graphs have almost the same shape this is clearly a display of them back to back. How can I get it to change the axis on which it subtracts, from x to y?

Its probably not needed but here is the full macro;

void testReader3(){
  TFile * file1 = TFile::Open("alt4aMaskOutput.root");
  TTree * tree1 = (TTree*)file1->Get("HitsTree");
  tree1->AddFriend("plain = HitsTree", "plainMaskOutput.root");

  tree1->Draw("hit_PMTid - plain.hit_PMTid");
}

Solution

  • The command you gave will make a histogram of the difference between the hit_PMTid and plain.hit_PMTid variables for every event of the tree. If instead you want to look at the bin by bin differences in the distributions, you need to fill two histograms, then subtract them (used TH1::Add). As you say, you have to force the same binning. An example of this would be:

    void testReader3(){
      TFile * file1 = TFile::Open("alt4aMaskOutput.root");
      TTree * tree1 = (TTree*)file1->Get("HitsTree");
      tree1->AddFriend("plain = HitsTree", "plainMaskOutput.root");
    
      // Draw the two histograms from the tree, specifying the
      // output histogram name and binning
      tree1->Draw("hit_PMTid>>hist1(100,0,6000)");
      tree1->Draw("plain.hit_PMTid>>hist2(100,0,6000)");
    
      // Retrieve the two histograms we just created
      TH1* hist1 = gDirectory->Get("hist1");
      TH1* hist2 = gDirectory->Get("hist2");
    
      // Subtract
      TH1* hist_diff = (TH1*) hist1->Clone("hist_diff");
      hist_diff->Add(hist2, -1);
    
    }