请原谅我的无知,但我是C++和根目录的新手,我不知道我到底做错了什么。
我要做的是编写一个函数,返回直方图中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()方法时,我没有得到任何问题。然而,我不知道。
谢谢,如果我正在做其他灾难性的糟糕的编码实践,请让我知道。
发布于 2018-02-15 22:22:28
已经指出,不能返回局部变量的地址,该地址将在函数结束时被销毁。
你的另一个问题是:
histo->GetNbinsX()不起作用。我尝试在main和脚本的一个子例程中调用它:它在当前的根版本中非常适合我。在问题中,您将拼写错误为GetNBinsX (是的,这将与驼峰策略更一致)。也许...?
无论如何,我相信您会很高兴知道ROOT有一个非常智能的1D峰值搜索算法可以使用:查找the TSpectrum class。
发布于 2015-07-07 03:09:08
您的代码存在各种问题。
最引人注目的是这一条:
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;
}
}https://stackoverflow.com/questions/31253060
复制相似问题