i wrote a macro as in below
void identify_hist_peaks2(TH1D* hist,int maxpositions=3, double resolution=1.0, bool draw=false, bool addToHist=false,int decimal_places=2,string unit="MHz"){
cout << unit << endl;
TSpectrum * peak_finder = new TSpectrum(maxpositions, resolution); // (int maxpositions, double resolution = 1)
int npeaks = peak_finder->Search(hist,2.0,"",0.1); // int TSpectrum::Search(const TH1* hist, double sigma = 2, const char* option = "", double threshold = 0.050000000000000003)
cout << "found " << npeaks << " peaks." << endl;
TPolyMarker* pm = (TPolyMarker*) hist->FindObject("TPolyMarker");
if(addToHist){hist->GetListOfFunctions()->Add(pm->Clone()); };
if(draw){pm->Draw(); };
// caveat: if not draw, the TPoly will resides in the ListOfFunction of that histo.
// if draw, the TPoly need to be attached to the ListOfFunction, in order to be drawn
for (int pk=0; pk<npeaks;pk++){
TLatex* text = new TLatex(pm->GetX()[pk]*1.05, pm->GetY()[pk]*0.9, Form("%.*f %s", decimal_places, pm->GetX()[pk],unit.c_str()));
text->SetTextColor(2);
text->SetTextSize(0.04);
if(draw){text->DrawClone(); };
if(addToHist){hist->GetListOfFunctions()->Add(text); };
};
}
then in pyroot, i run ROOT.gInterpreter.Declare('# include "test.h"')
(also tried ROOT.gROOT.ProcessLine()
). I have some fft histograms that i use the âimportedâ function to find and label the peaks, below is the code that does the job.
c = r.TCanvas("cc","c",800,400)
hist_fft.Draw()
ROOT.identify_hist_peaks2(hist_fft,maxpositions=3, resolution = 1.0,draw=True,unit="zzz")
c.Draw()
and wierd things happens:
if i do exactly the same as in the above code, i got error saying could not convert argument 4 (tuple assignment index out of range)
.
on the other hand, if i keep only the pass only the hist
and draw=True
, then i got (jupyter) kernel restart.
is my code buggy or i just missed out something ?
ROOT Version: 6.24 (conda-forge)
Platform: ubuntu 20.04(wsl)
Compiler: gcc9