Hi Ben,
You can simplify your main program and still get what you want
Rene Brun
#include "TCanvas.h"
#include "TNoisePlot.h"
#include "TSystem.h"
void loopnoise() {
TCanvas c1("c1","My Canvas",400,400);
TNoisePlot np;
while(!gSystem->ProcessEvents()) {
np.PlotHistos(&c1);
gSystem->Sleep(100);
}
}
On Thu, 15 Aug 2002, Ben Morgan wrote:
> Hi ROOTers,
> I've written a very simple object TNoisePlot which holds an
> array of thirty randomly filled histograms plus a dummy histogram to
> ensure all are plotted correctly (this object is eventually for use with
> data...). In a separate named script loopnoise(), I create a TNoisePlot
> object plus a TCanvas to display the histograms. As I want to eventually
> enter data that'll be used to generate the histograms, the script contains
> a while loop. At each pass through the loop, the PlotHistos method of
> TNoisePlot is called, and this regenerates and plots the histograms.
>
> As I want the canvas to be editable between each pass through the while
> loop, I also put a TTimer object in the script to use the
> gSystem->ProcessEvents() command. However, after 3-4 passes through the
> while loop, ROOT locks up completely (and with no error message). Taking
> the TTimer related lines out stops this happening, and the loop runs fine,
> but the canvas is no longer editable! I guess I'm probably doing something
> wrong with the TTimer. I'm using ROOT v3.02/07 on a PC running RH7.1, and
> have attached the code below. Any
> solutions will be most gratefully received!
> Thanks,
>
> Ben Morgan.
>
> #include <TROOT.h>
> #include <TChain.h>
> #include <TFile.h>
> #include <TH1.h>
> #include <iostream.h>
>
>
> class TNoisePlot {
>
> private:
> TH1S *hNoise[30];
> TH1S *hNoiseAxis;
>
> public:
> TNoisePlot();
> ~TNoisePlot();
> void FillHistos();
> void PlotHistos(TCanvas *c1);
> };
>
> TNoisePlot::TNoisePlot()
> {
> char hname[20];
> for(Int_t i=0; i<30; i++)
> {
> sprintf(hname,"noise%d",i);
> hNoise[i] = new TH1S(hname," ",100,0,100);
> }
>
> hNoiseAxis = new TH1S("hNoiseAxis"," ",100,0,100);
> }
>
> TNoisePlot::~TNoisePlot()
> {
> delete hNoiseAxis;
> for(Int_t i=0; i<30; i++)
> {
> delete hNoise[i];
> }
> }
>
> void TNoisePlot::FillHistos()
> {
> for(Int_t i=0; i<30; i++)
> {
> hNoise[i]->Reset();
> }
> hNoiseAxis->Reset();
>
>
> for(Int_t i=0; i<30; i++)
> {
> for(Int_t j=0; j<100; j++)
> {
> hNoise[i]->Fill(j,gRandom->Gaus(i*20,10));
> }
> }
>
> Short_t max=0;
> Short_t min=0;
> for(Int_t k=0; k<30; k++)
> {
> if((hNoise[k]->GetMaximum())>max) max=hNoise[k]->GetMaximum();
> if((hNoise[k]->GetMinimum())<min) min=hNoise[k]->GetMinimum();
> }
>
> hNoiseAxis->SetMaximum(max);
> hNoiseAxis->SetMinimum(min);
> }
>
> void TNoisePlot::PlotHistos(TCanvas *c1)
> {
> c1->cd();
> FillHistos();
>
> hNoiseAxis->Draw();
>
> for(Int_t i=0; i<30; i++)
> {
> hNoise[i]->Draw("same");
> }
> c1->Modified();
> c1->Update();
> }
>
>
> #include <iostream.h>
> #include <stdio.h>
>
> int loopnoise()
> {
> int ptest=0;
>
> TCanvas *c1 = new TCanvas("c1","My Canvas",400,400);
> TNoisePlot *np = new TNoisePlot();
> TTimer *ti = new TTimer("gSystem->ProcessEvents();",100,kFALSE);
>
> while(ptest==0)
> {
> cout<<"Timer turned on"<<endl;
> ti->TurnOn();
> cout<<"Timer reset"<<endl;
> ti->Reset();
>
> //At present, enter 0 to keep loop going
> cout<<"Enter ptest: ";
> cin>>ptest;
>
> np->PlotHistos(c1);
> cout<<"Plotted histos"<<endl;
> ti->TurnOff();
> cout<<"Timer turned off"<<endl;
> }
>
> delete np;
> delete c1;
> delete ti;
>
>
> return 0;
> }
>
>
>
> --
> -------------------------------------------------------------------------------
> Mr. Ben Morgan Tel: 44 (0)114 22 23553 (Work)
> Postgraduate Student
> University of Sheffield
> Department of Physics & Astronomy
> Hicks Building
> Hounsfield Road
> Sheffield S3 7RH
> -------------------------------------------------------------------------------
>
This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:04 MET