0
votes

Hey I'm using ROOT at the moment and I created a macro that will take two histograms and subtract one from the other, and loop through each bin to check if there are any non zero bins, this will test whether or not the histograms are equal.

Currently I'm creating two histograms inside the macro just to test the function, and the third histogram is Hist 1 - Hist 2 but I'd like to make it so I can input any two histograms as parameters into the macro and execute the test.

How can I do this?

The macro is currently this, and to remind you the two histograms inside are just there to test it:

#include "TCanvas.h"
#include "TROOT.h"
#include "TPad.h"
#include "TH1F.h"
#include "math.h"
#include "TRandom.h"
#include "TH1.h"

string subtracthist() {
  TCanvas *c1 = new TCanvas();
  ////////First histogram                                      
  TH1F *h1 = new TH1F("h1","Histogram 1",100,-3,3);
  h1->FillRandom("gaus",10000);
  ////////Second histogram                                     
  TH1F *h2 = new TH1F("h2","Histogram 2",100,-3,3);
  h2->FillRandom("gaus",10000);
  ////////First Histogram minus Second Histogram                        
  TH1F *h3 = new TH1F("h3","Subtracted Histograms",100,-3,3);
  h3->Add(h1,h2,1,-1);
  // h3->Draw();                                                        
  //TH1F *h4 = new TH1F("h4","Test", 100,-3,3);                         
  //h4->Draw();                                                         
  //c1->Update();                                                       

  ////////Caluclate Total number of bins in histogram including underflow and overflow bins                                                    

Int_t numberofbins = h3->GetSize();
////////This loop will run through each bin and check its content, if there is a nonzero bin the loop will break and output "The Histograms are not the same" If all bins are zero, it will output "The Histograms are the same".                                                          
    for(int i=0; i<=(numberofbins - 1); i++) {
    Int_t x = h3->GetBinContent(i);
      if (x != 0)
        {return "The Histograms are not the same";
          break;}

 }
  return "The Histograms are the same";
}
1
How do you want to use the macro exactly? You can add arguments to the subtracthist function as shown by DoubleYou and then use it in cint or cling with: .L subtracthist.C and then string res = subtracthist(h1,h2).pseyfert

1 Answers

1
votes

First, you're writing a function instead of a macro (see here).

Then, even though I don't know anything about ROOT, providing function parameters is pretty simple. For your example:

string subtracthist(TH1F *h1, TH1F *h2) {
    TH1F *h3 = new TH1F("h3","Subtracted Histograms",100,-3,3);
    h3->Add(h1,h2,1,-1);

    // Caluclate Total number of bins in histogram including underflow and overflow bins                                                    
    Int_t numberofbins = h3->GetSize();
    // This loop will run through each bin and check its content, if there is a nonzero bin the loop will break and output "The Histograms are not the same" If all bins are zero, it will output "The Histograms are the same".                                                          
    for(int i=0; i<=(numberofbins - 1); i++) {
        Int_t x = h3->GetBinContent(i);
        if (x != 0) {
            delete h3;
            return "The Histograms are not the same";
        }
    }
    delete h3;  //because you've created a h3, you need to also delete it otherwise you have memory leaks.
    return "The Histograms are the same";
}

int main() {
    //this is just to show how it might work
    TH1F *h1 = new TH1F("h1","Histogram 1",100,-3,3);   //first hist
    h1->FillRandom("gaus",10000);
    TH1F *h2 = new TH1F("h2","Histogram 2",100,-3,3);   //second hist
    h2->FillRandom("gaus",10000);
    string res=substracthist(h1,h2);
    delete h1;
    delete h2;
}

Looking that you have a canvas as well, you can add a parameter to provide the canvas in the same way to the function. To learn more about functions and how parameters work, just search the internet (maybe this can be a good start).

And remember, when you create pointers with new, don't forget to use delete when you don't need them anymore (to avoid memory leaks).