![](/img/trans.png)
[英]Can you pass a subclass as an argument to a function with a superclass parameter? C++
[英]Can you access methods from a ROOT object when you pass the object as an argument to a function in C++?
请原谅我的无知,但是我是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()方法时,我没有遇到任何问题。 但是我不知道。
谢谢,如果我要执行其他灾难性的可怕编码操作,请告诉我。
已经指出,不能返回局部变量的地址,该局部变量在函数结束时将被销毁。
您的另一个问题是关于:
histo->GetNbinsX()
不工作。 我试图在脚本的主程序和子程序中都调用它:对于当前的ROOT版本,它对我来说非常合适。 在该问题中,您拼写错误为GetNBinsX
(是的,这与骆驼案政策更加一致)。 也许...?
无论如何,我相信您一定会很高兴地知道ROOT可以使用一种非常智能的一维峰搜索算法:寻找TSpectrum类 。
您的代码存在各种问题。
最明显的是:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.