Problems with TCanvas and TSpectrum

Hi all,

I do have a small problem with the Peakfinder in root, when I use the peakfinder of TSpectrum, TApplication does not work properly. Maybe it is easier to understand if you take a look at the following code:



#include <iostream>
#include <vector>
#include <string>
#include "TROOT.h"
#include "TH1F.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TRandom.h"
#include "TLine.h"
#include "TGraphErrors.h"
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include "TGraph.h"
#include "TGraphErrors.h"
#include <TSpectrum.h>
#include <TF1.h>




    class test{
    public:
       test();
       ~test();
       void DrawLine(Int_t);
       void CreateEntries(Int_t);
       void GetHistogram();
       void GetIntegration();




    private:
       TApplication* tApp;
       TH1F* energyHistogram;
       std::vector <TH1F*> energyHistogramV;
       Double_t pulsercounts;
       Double_t peakcounts;
       TCanvas* canvas;

    };




    test::test()
    {
    	tApp=new TApplication("Data",0,0);
    	canvas=new TCanvas("canvas","Data",10,10,1100,600);
    	energyHistogram=new TH1F("c1","c1",100,-4,4);
    }

    test::~test()
    {
    	tApp->Run();
    }


    void test::GetIntegration()
    {
    	TSpectrum* peakFinder= new TSpectrum();
    	Int_t peaks=peakFinder->Search(energyHistogram,3); //deactivate this line and it works!!!
    	delete peakFinder;

    }

    void test::DrawLine(Int_t counter)
    {
    	if(counter==0){delete canvas;}
    	std::stringstream stream;
    	stream << counter;
    	std::string canvasname="canvas";
    	canvasname+=stream.str();
    	canvas=new TCanvas(canvasname.c_str(),canvasname.c_str(),10,10,1200,600);

    	for(UInt_t i=0;i<energyHistogramV.size();++i)
    	{
    		if(i==0){energyHistogramV.at(i)->Draw();}
    		else{energyHistogramV.at(i)->Draw("SAME");}
    	}
    	energyHistogramV.clear();
    	canvas->Update();
    }


    void test::GetHistogram()
    {

    	energyHistogram->FillRandom("gaus",100);
    	energyHistogramV.push_back(energyHistogram);

    }

    int main()
    {

    test testclass;

   	for(Int_t i=0;i<3;++i)
   	{
   		testclass.GetHistogram();
   		testclass.GetIntegration();
   		testclass.DrawLine(i);
   		sleep(2);



    }
    return 0;
    }

So using “Int_t peaks=peakFinder->Search(energyHistogram,3);” destroys my previous Canvas(es) when you call tApp->Run() and write the latest Histogram into the older canvases. The same happens with canvas->WaitPrimitive();. The strange thing is that it works fine before calling tApp->Run();
So either this is a bug or I am doing something wrong that I simply do not get :slight_smile:.
P.S. in function DrawLine you do not need the for loop, but my vector is usually bigger than one entry :slight_smile:

Try with:
Int_t peaks = peakFinder->Search(energyHistogram, 3, “nodraw”); // or “goff nodraw”

Yes, that works fine, thank you, but it still looks very weird to me. Why do you have to deactivate that option?

By the way is there a possibility to prevent root from creating a default TCanvas?

Thanks

http://root.cern.ch/root/html/TSpectrum.html#TSpectrum:Search

Don’t draw anything (i.e. before you deliberately create your own canvas) or run “root -b” (i.e. if you don’t want to have any graphics displayed on your screen at all).