0

请原谅我的无知,但我是 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->GetNBinsX() 方法时,我没有遇到任何问题。不过我不知道。

谢谢,如果我正在做其他灾难性的可怕编码实践,请告诉我。

4

2 回答 2

0

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

你的另一个问题是:

histo->GetNbinsX()

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

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

于 2018-02-15T14:22:28.090 回答
-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除了最后一个问题,总体而言,如果您使用 using而不是 usenew[]来创建动态数组,则可以解决这些问题。

应用这些更改,代码应如下所示(未编译):

#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;
  }
}
于 2015-07-06T19:09:08.453 回答