0

ねえ、私は現在 ROOT を使用しており、2 つのヒストグラムを取り、一方を他方から減算し、各ビンをループしてゼロ以外のビンがあるかどうかを確認するマクロを作成しました。これは、ヒストグラムが同等。

現在、関数をテストするためだけにマクロ内に 2 つのヒストグラムを作成しています。3 つ目のヒストグラムは Hist 1 - Hist 2 ですが、任意の 2 つのヒストグラムをパラメーターとしてマクロに入力してテストを実行できるようにしたいと考えています。 .

これどうやってするの?

マクロは現在これであり、内部の 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 に答える