Histograms appear and disappear instantly

Hi,

I’m using ROOT 6.14/04 on Ubuntu 18.04 with gcc 7.4.0 for analyzing signals collected by DRS4. I’m trying to display a Graph and three Histograms on a single Canvas divided into four pieces. The graph is displayed but histograms appears for a moment and then disappears while the titles still remained.

#include <string.h>
#include <stdio.h>
#include <float.h>
#include "TFile.h"
#include "TTree.h"

#include "TMath.h"
#include "TH1F.h"
#include "TF1.h"
#include "TString.h"
#include "TGraph.h"
#include "TCanvas.h"

// METHODS ARE USED FROM DRS4 Measurement.cpp

double ss_nan();
int ss_isnan();
void convert2Volt(double (&w)[1024]);
int *LocMaxArray(double *x, double *y, int n, double RMS);
double *MRiseArray(double *x, double *y, int n, int *locs);
double *MFallArray(double *x, double *y, int n, int *locs);

using namespace std;

void analyzePulseL(int fEvent=1){
  
  TFile *theFile = new TFile("20mVThr_Cs137_50000_17042019.root");https://drive.google.com/file/d/1dwPSiGUYx0wo5M51HytWw1f1uQ4sT14t/view?usp=sharing
  TTree *theTree = (TTree*)theFile->Get("rec");
  int EventNo = 0;
	int iEvent = 0;
  const int npts = 1024;
  double t1[npts] = {0};
  double w1[npts] = {0};
	int *locArray;
	double *riseTime, *fallTime;
	double meanVolt, RMSVolt;
	int nEntries;
	int iLoc, iDigit;

	TCanvas *c1 = new TCanvas("c1", "c1", 800, 600);
	c1->Divide(2,2);

  theTree->SetBranchAddress("t1", &t1);
  theTree->SetBranchAddress("w1", &w1);  
  
	TGraph *g1 = new TGraph(npts, (double *)t1, (double *)w1);
	TH1F *hPV = new TH1F("hPV","Peak Voltage",400,0,200);  
	TH1F *hRT = new TH1F("hRT","Rise Time",80,0,40);  
	TH1F *hFT = new TH1F("hFT","Fall Time",80,0,40);  

  nEntries = (int)theTree->GetEntries();
  cout << "n_events: " << nEntries << endl << endl;

	for(iEvent = 0; iEvent < nEntries; iEvent++){
		theTree->GetEntry(iEvent);
		convert2Volt(w1);

		for (iDigit=0 ; iDigit<npts ; iDigit++){
			g1->SetPoint(iDigit, t1[iDigit], w1[iDigit]);
		}  

		meanVolt = g1->GetMean(2);
		RMSVolt  = g1->GetRMS(2);

		if (RMSVolt<10.) RMSVolt=10.;

		locArray=LocMaxArray(t1, w1, npts,RMSVolt);

		if (locArray != 0){

			riseTime = MRiseArray(t1, w1, npts,locArray);
			fallTime = MFallArray(t1, w1, npts,locArray);

			for (iLoc=1; iLoc<=locArray[0]; iLoc++){
				hPV->Fill((float)w1[locArray[iLoc]]);
				hRT->Fill((float)riseTime[iLoc]);
				hFT->Fill((float)fallTime[iLoc]);
			}
		}else{
				cout << "====== " << iEvent << " rejected ======" << endl<< endl;
		}
	}

	c1->cd(1);
		g1->SetTitle("Single Event");
		g1->SetMarkerStyle(kDot);
		g1->Draw("AP");

	c1->cd(2);
		hPV->SetTitle("Peak Voltage");
		hPV->GetXaxis()->SetTitle("Voltage (mV)");
		hPV->GetYaxis()->SetTitle("Nb. of Events/0.5 mV");
		hPV->SetLineColor(kBlue);
		hPV->SetMarkerColor(kBlue);
		hPV->SetMarkerStyle(21);
		hPV->Draw();

	c1->cd(3);
		hRT->SetTitle("Rise Time");
		hRT->GetXaxis()->SetTitle("Rise Time (ns)");
		hRT->GetYaxis()->SetTitle("Nb. of Events/0.5 ns");
		hRT->SetLineColor(kBlue);
		hRT->SetMarkerStyle(22);
		hRT->Draw();

	c1->cd(4);
		hFT->SetTitle("Fall Time");
		hFT->GetXaxis()->SetTitle("Fall Time (ns)");
		hFT->GetYaxis()->SetTitle("Nb. of Events/0.5 ns");
		hFT->SetLineColor(kBlue);
		hFT->SetMarkerStyle(23);
		hFT->Draw();

	cout << hPV->GetEntries() << endl;
	cout << hPV->GetBinContent(hPV->GetMaximumBin()) << endl;

	c1->Modified();  
	c1->Update();

  theFile->Close();

}

double ss_nan()
{
  cout << "returning nan value" << endl;
  return -99999;
}

int ss_isnan(double x)
{
  return isnan(x);
}

void convert2Volt(double (&w)[1024])
{
  for (int i=0 ; i<1024 ; i++){
    w[i] = w[i] * 1000;
  }
}

int *LocMaxArray(double *x, double *y, int n, double RMS)
{
	Bool_t cntChanged=kFALSE;
  double max = y[0];
	double nbincnt=0;
  if (n <= 0)
    return 0;
  static int locs[10] = {0};
	int loc =0;
	int peakcounter=0;
  for(int i=0 ; i < (n-4) ; i++){
    if(max < y[i] && y[i] > 1.96*RMS){
			// cout << "max: " << max << " #bins: " << nbincnt << endl;
			nbincnt++;
      max = y[i];
      loc = i;
			if (y[loc+1] < y[loc] && y[loc-1] < y[loc] && nbincnt > 1){
				peakcounter++;
				locs[peakcounter]=loc;
				cntChanged=kTRUE;
				nbincnt=0;
				//cout << "peak: " << peakcounter << " @ " << loc << endl;
			}
    }
		if (y[i]<RMS && cntChanged){cntChanged=kFALSE; max = y[i]; }
		if (peakcounter==10){
			cout << "RMS too low, peak count exceeds 10..." << endl;
			locs[0]=-99999;
			return 0;
		}
  }
	locs[0]=peakcounter;
  return locs;
}

double *MRiseArray(double *x, double *y, int n, int *locs)
{

	if(n <= 0)
		return 0;

	int i,j,posmin;
	double miny, maxy, t1, t2, y10, y90;
	static double rt[10];

	rt[0]=locs[0];

	for(j=1; j<=locs[0]; j++){

		miny=maxy=y[locs[j]];

		for (i=locs[j]-1; i>0; i--){
			if(y[i] < miny)
			 miny = y[i];
			else
				break;
		}

		if (i == 0){
			//cout << "RISE TIME ERROR @ peak #" << j << " no real minimum." << endl;
			rt[j]=-99999;
			continue;
		}
		
		if (maxy - miny < 10){
			//cout << "RISE TIME ERROR @ peak #" << j << " not enough amplitude." << endl;
			rt[j]=-99999;
			continue;
		}

		posmin = i+1;
    y10 = miny+0.1*(maxy-miny);
    y90 = miny+0.9*(maxy-miny);

    /* search for 10% level crossing */
		for (i=posmin; i<locs[j]; i++)
			if (y[i] > y10 && y[i-1] <= y10)
				break;		
		t1 = (y10*(x[i]-x[i-1])+x[i-1]*y[i]-x[i]*y[i-1])/(y[i]-y[i-1]);	

		/* search for 90% level crossing */
		for (i=posmin; i<locs[j]; i++)
			if (y[i] > y90 && y[i-1] <= y90)
				break;
   	t2 = (y90*(x[i]-x[i-1])+x[i-1]*y[i]-x[i]*y[i-1])/(y[i]-y[i-1]);

		rt[j] = t2-t1;
	}
	return rt;
}


double *MFallArray(double *x, double *y, int n, int *locs)
{

	if(n <= 0)
		return 0;

	int i,j,posmin;
	double miny, maxy, t1, t2, y10, y90;
	static double rt[10];

	rt[0]=locs[0];

	for(j=1; j<=locs[0]; j++){

		miny=maxy=y[locs[j]];

		for (i=locs[j]+1; i<n; i++){
			if(y[i] < miny)
			 miny = y[i];
			else
				break;
		}

		if (i == n){
			//cout << "FALL TIME ERROR @ peak #" << j << " no real minimum." << endl;
			rt[j]=-99999;
			continue;
		}
		
		if (maxy - miny < 10){
			//cout << "FALL TIME ERROR @ peak #" << j << " not enough amplitude." << endl;
			rt[j]=-99999;
			continue;
		}

		posmin = i;
    y10 = miny+0.1*(maxy-miny);
    y90 = miny+0.9*(maxy-miny);

    /* search for 90% level crossing */
		for (i=locs[j]+1; i>posmin; i++)
			if (y[i] > y90 && y[i-1] <= y90)
				break;		
    t1 = (y90*(x[i]-x[i-1])+x[i-1]*y[i]-x[i]*y[i-1])/(y[i]-y[i-1]);

		/* search for 90% level crossing */
		for (; i>posmin; i++)
			if (y[i] > y10 && y[i-1] <= y10)
				break;
    t2 = (y10*(x[i]-x[i-1])+x[i-1]*y[i]-x[i]*y[i-1])/(y[i]-y[i-1]);

		rt[j] = t2-t1;
	}
	return rt;
}

Try to add:

// ...
#include "TROOT.h"
// ...
	// ...
	hPV->SetDirectory(gROOT); // (gROOT) ... or ... (0)
	hRT->SetDirectory(gROOT); // (gROOT) ... or ... (0)
	hFT->SetDirectory(gROOT); // (gROOT) ... or ... (0)
	// ...

BTW. Note that ‘’’ is not equal to ``` (see how your post has been edited above) :wink:

Thanks it worked.:pray::smiley: