0

嘿,我现在正在使用 ROOT,我创建了一个宏,它将采用两个直方图并从另一个中减去一个,并循环遍历每个 bin 以检查是否有任何非零 bin,这将测试直方图是否是平等的。

目前我在宏内创建两个直方图只是为了测试函数,第三个直方图是 Hist 1 - Hist 2 但我想做它,这样我就可以将任意两个直方图作为参数输入到宏中并执行测试.

我怎样才能做到这一点?

宏目前是这样的,提醒你里面的两个直方图只是为了测试它:

#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";
}
4

1 回答 1

1

首先,您正在编写一个函数而不是宏(请参见此处)。

然后,即使我对ROOT一无所知,提供函数参数也很简单。对于您的示例:

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;
}

看起来你也有一个画布,你可以添加一个参数,以同样的方式为函数提供画布。要了解有关函数和参数如何工作的更多信息,只需搜索互联网(也许是一个好的开始)。

请记住,当您使用 来创建指针时new,不要忘记在delete不再需要它们时使用它们(以避免内存泄漏)。

于 2014-11-04T04:53:43.593 回答