当您将对象作为参数传递给C ++中的函数时,是否可以从ROOT对象访问方法?

时间:2015-07-06 18:29:09

标签: c++ root-framework

请原谅我的无知,但我是C ++和ROOT的新手,我不知道自己到底做错了什么。

我要做的是编写一个函数,在直方图中返回n峰的bin位置。以下是我的代码:

#include <iostream>
#include <algorithm>
#include <iterator>

#include "TROOT.h"
#include "TCanvas.h"
#include "TH1.h"
#include "TF1.h"

using namespace std;

int *peak_counter1d(TH1F *histogram,int peak_num,int threshold = 5,int display = 0){
  if(display == 1){
    TCanvas *look = new TCanvas("look","look",500,400);
    histogram->Draw();
  }
  int total_bins = histogram->GetNBinsX();
  double peak_bins[peak_num];
  peak_bins[0] = histogram->GetMaximumBin();
  int counter = 1;
  int *check_array; // to put previously found peak bins
  while(counter < peak_num){
    double peak = threshold;
    double peak_loc = -500;
    check_array = new int[counter];
    for(int i=0; i<counter; i++){
      check_array[i] = peak_bins[i]; // fills the array with previously found peak bins
    }
    for(int i=0; i<total_bins; i++){
      if(peak < histogram->GetBinContent(i)){ 
        bool exists = find(begin(check_array),end(checkarray),i); // makes sure this is a peak we haven't already found
        if(!exists){
          peak = histogram->GetBinContent(i);
          peak_loc = i;
        }
      }
    }
    peak_bins[counter] = peak_loc;
    counter ++;
  }
  delete[] check_array;

  return peak_bins;
}

void testing(){
  gROOT->Reset();
  TH1F *histo = new TH1F("histo","try",100,0,10);
  TF1 *f1 = new TF1("f1","exp(-x/10)*sin(x)*sin(x)",0,10);
  double val;
  for(int i=0; i<100; i++){
    val = f1->Eval(i/10.0);
    //cout << i << "\t" << i/100.0 << "\t" << val << endl;
    histo->SetBinContent(i,val);
  }

  int *peak_bins;
  peak_bins = peak_counter1d(histo,3,5,1);
  for(int i=0; i<3; i++){
    cout << i << "\t" << *(peak_bins+i) << endl;
  }
}

当我在ROOT中执行此代码时,我得到以下内容:

root [] .x testing.cpp
Error: Can't call TH1F::GetNBinsX() in current scope testing.cpp:15:
Possible candidates are...
(in TH1F)
(in TH1)
*** Interpreter error recovered ***

我认为这是访问函数内部对象方法的问题,因为当我在testing()函数中调用histo-&gt; GetNBinsX()方法时,我没有遇到任何问题。但是我不知道。

谢谢,如果我正在做其他灾难性的编码行为,请告诉我。

2 个答案:

答案 0 :(得分:0)

已经指出,人们不能返回局部变量的地址,当函数结束时它将被销毁。

你的另一个问题是:

histo->GetNbinsX()

不工作。我尝试在主脚本和脚本的子程序中调用它:它对我来说非常适合当前的ROOT版本。在这个问题中,你错误拼写为GetNBinsX(是的,与骆驼案例政策更加一致)。也许...?

无论如何,我相信您会很高兴知道ROOT有一个非常智能的一维峰值搜索算法可供使用:寻找the TSpectrum class

答案 1 :(得分:-1)

您的代码存在各种问题。

最引人注目的是这个:

int *peak_counter1d(TH1F *histogram,int peak_num,int threshold = 5,int display = 0)
{
    //...
    double peak_bins[peak_num];
    //...
    return peak_bins;
}

您正在返回指向局部变量的指针。返回指向局部变量的指针是未定义的行为

下一个问题是:

  int *check_array; // to put previously found peak bins
  while(counter < peak_num)
  {
      //...
      check_array = new int[counter];
  }
  delete[] check_array;

由于您在没有取消分配check_array的情况下进行循环,因此可能存在内存泄漏。另一个问题是,如果该循环从未执行,则在未初始化的变量上调用delete []

下一个问题是:

int * peak_counter1d(...)
{
    double peak_bins[peak_num];
    //...
    return peak_bins;
}

即使您可以安全地返回指向局部变量的指针,您的函数也会返回一个int *,但是您返回的是double *

下一个问题是:

TCanvas *look = new TCanvas("look","look",500,400);

您正在分配look,但您从未取消分配,甚至不使用它。

你也在main中做同样的事情:

 TH1F *histo = new TH1F("histo","try",100,0,10);
  TF1 *f1 = new TF1("f1","exp(-x/10)*sin(x)*sin(x)",0,10);

C ++不是Java。您不必使用new创建对象。

TH1F histo("histo","try",100,0,10);
  TF1 f1("f1","exp(-x/10)*sin(x)*sin(x)",0,10);

除了上一期之外,如果您使用std::vector并且不使用new[]来创建动态数组,则可以解决这些问题。

应用这些更改,代码看起来应该是这样的(未编译):

#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>

#include "TROOT.h"
#include "TCanvas.h"
#include "TH1.h"
#include "TF1.h"

using namespace std;

vector<int> peak_counter1d(TH1F *histogram,int peak_num,int threshold = 5,int display = 0)
{
  if(display == 1)
  {
    // TCanvas *look = new TCanvas("look","look",500,400);
     histogram->Draw();
  }
  int total_bins = histogram->GetNBinsX();
  vector<int> peak_bins(peak_num);
  peak_bins[0] = histogram->GetMaximumBin();
  int counter = 1;
  vector<int> check_array; // to put previously found peak bins
  while(counter < peak_num){
    double peak = threshold;
    double peak_loc = -500;
    check_array.resize(counter);
    for(int i=0; i<counter; i++){
      check_array[i] = peak_bins[i]; // fills the array with previously found peak bins
    }
    for(int i=0; i<total_bins; i++){
      if(peak < histogram->GetBinContent(i)){ 
        bool exists = find(begin(check_array),end(checkarray),i); // makes sure this is a peak we haven't already found
        if(!exists){
          peak = histogram->GetBinContent(i);
          peak_loc = i;
        }
      }
    }
    peak_bins[counter] = peak_loc;
    counter ++;
  }
  return peak_bins;
}

void testing(){
  gROOT->Reset();
  TH1F histo("histo","try",100,0,10);
  TF1 f1("f1","exp(-x/10)*sin(x)*sin(x)",0,10);
  double val;
  for(int i=0; i<100; i++){
    val = f1.Eval(i/10.0);
    //cout << i << "\t" << i/100.0 << "\t" << val << endl;
    histo.SetBinContent(i,val);
  }

  vector<int> peak_bins = peak_counter1d(&histo,3,5,1);
  for(int i=0; i<3; i++){
    cout << i << "\t" << peak_bins[i] << endl;
  }
}