Segmentation Error While filling Histograms in Loop

Dear Experts I am getting segmentation Error while running this code as I am trying to fill histograms in a Loop. The Error is as follows Please suggest solution for this.

Warning in <TH1::TH1>: nbins is <=0 - set to nbins = 1
Event: 1

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f55910ea45a in __GI___wait4 (pid=6972, stat_loc=stat_loc
entry=0x7ffe5e9049d8, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30
#1  0x00007f55910ea41b in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7ffe5e9049d8, options=options
entry=0) at ./posix/waitpid.c:38
#2  0x00007f5591050bcb in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:171
#3  0x00007f5591b168a4 in TUnixSystem::StackTrace() () from /home/yasir/Downloads/root/lib/libCore.so.6.26
#4  0x00007f5591b13bb5 in TUnixSystem::DispatchSignals(ESignals) () from /home/yasir/Downloads/root/lib/libCore.so.6.26
#5  <signal handler called>
#6  0x00007f559162c4df in ?? ()
#7  0x00007f558d0c0920 in ?? () from /home/yasir/Downloads/root/lib/libCling.so
#8  0x00005578de4f1f00 in ?? ()
#9  0x0000000000000001 in ?? ()
#10 0x00007f5591a42fa0 in ?? () from /home/yasir/Downloads/root/lib/libCore.so.6.26
#11 0x0000000000000000 in ?? ()
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum https://root.cern/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at https://root.cern/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x00007f559162c4df in ?? ()
#7  0x00007f558d0c0920 in ?? () from /home/yasir/Downloads/root/lib/libCling.so
#8  0x00005578de4f1f00 in ?? ()
#9  0x0000000000000001 in ?? ()
#10 0x00007f5591a42fa0 in ?? () from /home/yasir/Downloads/root/lib/libCore.so.6.26
#11 0x0000000000000000 in ?? ()
===========================================================
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <stdlib.h>

#include "TROOT.h"
#include "TTree.h"
#include "TChain.h"
#include "TFile.h"
#include "TString.h"
#include "TSystem.h"
#include "TCanvas.h"
#include "TLegend.h"
#include "TH1.h"
#include "TH2.h"
#include "TH3.h"
#include "TColor.h"
#include "Riostream.h"
#include "TDatime.h"
#include "TMath.h"
#include "TStyle.h"
#include "TLorentzVector.h"

#include <TTreeReader.h>
#include <TTreeReaderValue.h>
#include <TTreeReaderArray.h>


stringstream ss;

void study()
{
    // *************************************************************************
    // Rootfile and Tree
    TFile* f = new TFile("lumidet_FG.edm4hep.root");
    f->cd();

    //TTree* t = (TTree*)f->Get("events");
    TTreeReader myReader("events", f);                     // name of tree and file

    TTreeReaderArray<Float_t> energy(myReader, "LumiDirectPCALHits.energy");
    TTreeReaderArray<Float_t> x(myReader, "LumiDirectPCALHits.position.x");
    TTreeReaderArray<Float_t> y(myReader, "LumiDirectPCALHits.position.y");
    TTreeReaderArray<Float_t> z(myReader, "LumiDirectPCALHits.position.z");
    
    
     const int ngraph = 7; 
 	TCanvas * c[ngraph*8]; 
 	for (int i =0; i<ngraph; ++i){
 	c[i] = new TCanvas(Form("c%d",i),Form("c%d",i),1200,1000);
 	c[i]->SetMargin(0.09, 0.1 ,0.1,0.06);

	 }
 
 
    // Rootfile and Tree
    // *************************************************************************
    // *************************************************************************
    // Histograms
   
    float xmin = -50;
    float xmax = 50;
    int nx = 100;
    float ymin = -5;
    float ymax = 40;
    int ny = 100;
    float zmin = 66150;
    float zmax = 66350;
    int nz = 200;


 TH3D *h3[8];
   for(int i=0;i<8;i++){
         h3[i]=new TH3D(Form("h_%d",i),Form("h_%d",i), nx, xmin, xmax, ny, ymin, ymax, nz, 0, zmax - zmin);

   
    // Fill Histograms

    TRandom3 *rndm = new TRandom3();
    int nev = 0;
    vector<int> nphoton;
    float xxx = 0;
    float yyy = 0;
    float zzz = 0;
    
    TH1D *h_zmax= new TH1D("hzmax","hzmax",0,zmin,zmax);
    
    while (myReader.Next())
    {
        nev++;
        cout << "Event: " << nev << endl;
        nphoton.push_back(0);
        float tot_e = 0;

        for (int i = 0; i < energy.GetSize(); i++)
        {
            nphoton.back()++;
            tot_e += energy[i];
            //h3["energy"]->Fill(x[i],y[i],abs(z[i]) - zmin,energy[i]);
            h3[i]->Fill(x[i]+rndm->Gaus(0,1) ,y[i] + rndm->Gaus(0,1) , abs(z[i])+ rndm->Gaus(0,1) - zmin,energy[i] );
            xxx = max(abs(x[i]), xxx);
            yyy = max(abs(y[i]), yyy);
            zzz = max(abs(z[i]), zzz);
        }
            h_zmax->Fill(zzz);
    }

    cout << "Max-X: " << xxx << endl;
    cout << "Max-Y: " << yyy << endl;
    cout << "Max-Z: " << zzz << endl;
    
    
    
    // Fill Histograms
    // *************************************************************************

    // *************************************************************************
    // Draw Histograms
    for (int i =0;i<8;i++)
    {
        //gStyle->SetOptStat(0);


        TH3* h = (TH3*)h3[i]->Clone();
        h->Scale(1.0/float(h->Integral()));

        TH2* h2yx = (TH2*)h->Project3D("yx")->Clone();
        TH2* h2zx = (TH2*)h->Project3D("zx")->Clone();
        TH2* h2zy = (TH2*)h->Project3D("zy")->Clone();
        TH1* h1x = (TH2*)h->Project3D("x")->Clone();
        TH1* h1y = (TH2*)h->Project3D("y")->Clone();
        TH1* h1z = (TH2*)h->Project3D("z")->Clone();
        TH1* h1E = (TH2*)h->Project3D("energy")->Clone();
      
        
        c[8*i]->cd();
        h2yx->GetXaxis()->SetTitle("x (mm)");
        h2yx->GetYaxis()->SetTitle("y (mm)");
        gPad->SetLogx(0); gPad->SetLogy(0); gPad->SetLogz(0);
        if(i==0)
        h2yx->Draw("colz");
        
        //ss.str(""); ss << "test2D_yx_" << hname << ".png";
        //c->SaveAs(TString(ss.str()));
        
        c[1+8*i]->cd();
        h2zx->GetXaxis()->SetTitle("x (mm)");
        h2zx->GetYaxis()->SetTitle("z (mm)");
        gPad->SetLogx(0); gPad->SetLogy(0); gPad->SetLogz(0);
        h2zx->Draw("colz");
       // ss.str(""); ss << "test2D_zx_" << hname << ".png";
        //c->SaveAs(TString(ss.str()));
        
        c[2+8*i]->cd();
        h2zy->GetXaxis()->SetTitle("y (mm)");
        h2zy->GetYaxis()->SetTitle("z (mm)");
        gPad->SetLogx(0); gPad->SetLogy(0); gPad->SetLogz(0);
        h2zy->Draw("colz");
        //ss.str(""); ss << "test2D_zy_" << hname << ".png";
        //c->SaveAs(TString(ss.str()));
        
        c[3+8*i]->cd();
        h1x->GetXaxis()->SetTitle("x (mm)");
        gPad->SetLogx(0); gPad->SetLogy(1); gPad->SetLogz(0);
        h1x->Draw("colz");
        //ss.str(""); ss << "test1D_x_" << hname << ".png";
        //c->SaveAs(TString(ss.str()));

        c[4+8*i]->cd();
        h1y->GetXaxis()->SetTitle("y (mm)");
        gPad->SetLogx(0); gPad->SetLogy(1); gPad->SetLogz(0);
        h1y->Draw("colz");
        //ss.str(""); ss << "test1D_y_" << hname << ".png";
        //c->SaveAs(TString(ss.str()));

        c[5+8*i]->cd();
        h1z->Draw("colz");
        h1z->GetXaxis()->SetTitle("z (mm)");
        h1z->GetXaxis()->SetRangeUser(0., 200);
        gPad->SetLogx(0); gPad->SetLogy(1); gPad->SetLogz(0);
        
        
        c[6+8*i]->cd();
        h1z->Draw("L");
        h1z->SetTitle("Photon Distribution");
        h1z->GetXaxis()->SetTitle("z (mm)");
        h1z->GetYaxis()->SetTitle("E (GeV)");
        h1z->GetXaxis()->SetRangeUser(0., 200);
        gPad->SetLogx(0); gPad->SetLogy(0); gPad->SetLogz(0);
        
        
}
       // c->Close();
}
    }
    // Draw Histograms
    // *************************************************************************

Check your loop variables. Your outmost for loop uses i to create h3[i] and inside you have another for loop also with i and different limits, in which you try to fill h3[i].

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.