Segmentation fault in TCanvas array

Hello Rooters,

I am having a segmentation fault in my TCanvas array. This was working fine with the 2D array. When I add a third set of TCanvas gave me a segmentation fault.

int i_theta_bins = 4;
    int n_p = 6;
    int sectors=6;
TH1F *h_pimMX2array_3fold_bgnd[i_theta_bins][n_p][sectors];
    char histName_3fold_bgnd[64];
     for (int isec=0; isec<sectors; isec++) {
    for (int ith=0; ith<i_theta_bins; ith++) {
        for (int ippim=0; ippim<n_p; ippim++) {
          
            sprintf(histName_3fold_bgnd,"h_pimMX23foldbgndarray_p%03d_th%03d_sec%03d",ippim,ith,isec);
            h_pimMX2array_3fold_bgnd[ith][ippim][isec] = new TH1F(histName,"3fold and 4fold Missing pimMassSq",300,-0.5,0.5);
            h_pimMX2array_3fold_bgnd[ith][ippim][isec]->SetTitle(Form("P bin %d / th bin %d / sec %d ",ippim,ith,isec));
            }
        }
    }


//Filling histograms

//TCanvas array
double n3bgnd[i_theta_bins][n_p][sectors];
    double n4bgnd[i_theta_bins][n_p][sectors];
    double bgnd_norm[i_theta_bins][n_p][sectors];
   TCanvas *canEfficiency[6];
 
    char canName[32], canLabel[48];
    for (int isec=0; isec< 6; isec++){
      //  sprintf(canName,"CanEfficiency%03d",isec);
      //  sprintf(canLabel,"Sector %03d Efficiency",isec);
       // canEfficiency[isec]= new TCanvas(canName,canLabel,12000,6000);
          canEfficiency[isec]= new TCanvas(Form("canEfficiency%d",isec));
        canEfficiency[isec]->Divide(6,4);
    for (int ith=0; ith< i_theta_bins ; ith++) {
        for (int ippim=0; ippim<n_p; ippim++){
            
            
            n3bgnd[i_theta_bins][n_p][sectors] =h_pimMX2array_3fold_bgnd[ith][ippim][isec]->GetEntries();
            // remove overflows and underflows (should be 0)
            n3bgnd[i_theta_bins][n_p][sectors] += -h_pimMX2array_3fold_bgnd[ith][ippim][isec]->GetBinContent(0,0);
            n3bgnd[i_theta_bins][n_p][sectors] += -h_pimMX2array_3fold_bgnd[ith][ippim][isec]->GetBinContent(h_pimMX2array_3fold_bgnd[ith][ippim][isec]->GetNbinsX()+1);
           
            
            
            n4bgnd[i_theta_bins][n_p] [sectors]=h_pimMX2array_4fold_bgnd_notexc[ith][ippim][isec]->GetEntries();
            
            n4bgnd[i_theta_bins][n_p][sectors] += -h_pimMX2array_4fold_bgnd_notexc[ith][ippim][isec]->GetBinContent(0,0);
            n4bgnd[i_theta_bins][n_p][sectors] += -h_pimMX2array_4fold_bgnd_notexc[ith][ippim][isec]->GetBinContent(h_pimMX2array_4fold_bgnd_notexc[ith][ippim][isec]->GetNbinsX()+1);
            //    printf("4fold_bngd_notExc events = %f \n", n4bgnd[i_theta_bins][n_p]);
            
            
            bgnd_norm[i_theta_bins][n_p][sectors] = n3bgnd[i_theta_bins][n_p][sectors]/n4bgnd[i_theta_bins][n_p][sectors];
            
            
            TH1F  *h_pimMX2array_3fold_norm =(TH1F*)h_pimMX2array_4fold_exc_notexc[ith][ippim][isec]->Clone("h_pimMX2array_3fold_norm");
            h_pimMX2array_3fold_norm->Add(h_pimMX2array_4fold_exc_notexc[ith][ippim][isec],bgnd_norm[i_theta_bins][n_p][sectors]-1.0);
            
            TH1F* Neg_Normalized_bgnd=( TH1F* )h_pimMX2array_3fold_norm->Clone("Neg_Normalized_bgnd");
            Neg_Normalized_bgnd->Scale(-1);
            
            TH1F* h_pimMX2array_1=(TH1F*) h_pimMX2array[ith][ippim][isec]->Clone("h_pimMX2array_1");
            h_pimMX2array_1->Add(Neg_Normalized_bgnd);
            
            TH1F*h_pimMX2array_4fold_exc_notexc_1=(TH1F*)   h_pimMX2array_4fold_exc_notexc[ith][ippim][isec]->Clone("h_pimMX2array_4fold_exc_notexc_1");
            h_pimMX2array_4fold_exc_notexc_1->Scale(-1);
            h_pimMX2array_4fold_exc_notexc_1->Add(h_pimMX2array_4fold[ith][ippim][isec]);
            
            TH1F* Efficiency=(TH1F*)h_pimMX2array_4fold_exc_notexc_1->Clone("Efficiency");
            
            Efficiency->Divide(h_pimMX2array_1);
            
            
            
            
            
            k++;
            
            canEfficiency[isec]->cd(k);h_pimMX2array[ith][ippim][isec]->SetLineColor(kGreen);h_pimMX2array[ith][ippim][isec]->Draw();
            canEfficiency[isec]->cd(k); h_pimMX2array_4fold[ith][ippim][isec]->SetLineColor(kYellow);h_pimMX2array_4fold[ith][ippim][isec]->Draw("sames");
            canEfficiency[isec]->cd(k); h_pimMX2array_4fold_exc_notexc[ith][ippim][isec]->SetLineColor(kCyan);h_pimMX2array_4fold_exc_notexc[ith][ippim][isec]->Draw("sames");
            
            canEfficiency[isec] ->cd(k); h_pimMX2array_4fold_bgnd_notexc[ith][ippim][isec]->SetLineColor(6);h_pimMX2array_4fold_bgnd_notexc[ith][ippim][isec]->Draw("sames");
            
            canEfficiency[isec]->cd(k); h_pimMX2array_3fold_bgnd[ith][ippim][isec]->SetLineColor(8);h_pimMX2array_3fold_bgnd[ith][ippim][isec]->Draw("sames");
            canEfficiency[isec]->cd(k); h_pimMX2array_3fold_norm->SetLineColor(9);h_pimMX2array_3fold_norm->Draw("sames");
           canEfficiency[isec]->cd(k); h_pimMX2array_1->SetLineColor(1);h_pimMX2array_1->Draw("sames");
            canEfficiency[isec]->cd(k); h_pimMX2array_4fold_exc_notexc_1->SetLineColor(2);h_pimMX2array_4fold_exc_notexc_1->Draw("sames");
            
            
            
            
            
           
                
           }
            
            
        }
   }
    

As I realized , the segmentation fault happed after I added following part.

TCanvas *canEfficiency[6];
 
    char canName[32], canLabel[48];
    for (int isec=0; isec< 6; isec++){
      //  sprintf(canName,"CanEfficiency%03d",isec);
      //  sprintf(canLabel,"Sector %03d Efficiency",isec);
       // canEfficiency[isec]= new TCanvas(canName,canLabel,12000,6000);
          canEfficiency[isec]= new TCanvas(Form("canEfficiency%d",isec));
        canEfficiency[isec]->Divide(6,4);

it’s not even creating my 6 canvases.
Just see only one plot.

thanks
Dil

What’s k? since you do Divide(6,4), ‘cd’ only takes from 1 to 24.

Hello,

Defined as int k;
I was trying to Draw the plots with a set of nested
loops preceded by a canvas division to match the array dimienions.

followed following link
https://root.cern.ch/root/roottalk/roottalk02/1796.html

Do you think is that a problem?

Thanks
Dil

At least, inside of the “for” loops, use:

n3bgnd[ith][ippm][isec]
n4bgnd[ith][ippm][isec]
bgnd_norm[ith][ippm][isec]

Always try to precompile your ROOT macro so that the compiler “checks” your source code and reports problems:

root [0] .L MyMacro.cxx++

Quite often, trying to compile the source code outside of a ROOT interactive session provides more thorough “checks”, e.g.:

[...]$ `root-config --cxx --cflags` -O2 -Wall -Wextra -c MyMacro.cxx

After you fix all errors and warnings reported by the compiler and your ROOT macro still “misbehaves”, ask questions here.

Hello,

Thank you for reply.

I don’t understand what’s this statement
"At least, inside of the “for” loops, use:

n3bgnd[ith][ippm][isec]
n4bgnd[ith][ippm][isec]
bgnd_norm[ith][ippm][isec]"

When I start introducing line

 n4bgnd[i_theta_bins][n_p] [sectors]=h_pimMX2array_4fold_bgnd_notexc[ith][ippim][isec]->GetEntries();

The code start crashing at that line.I checked my histogram and everything but they look okay.

thanks
Dil

And what does the compiler (with use of “-O2 -Wall -Wextra”) say about your source code?

Sorry I am not familiar with this.(I am using exactly root but added few extra libraries.)
So when I ran it as you said it gave me a following

*** Break *** illegal instruction

===========================================================

There was a crash.

This is the entire stack trace of all threads:

===========================================================

#0 0x00007f34161ab41c in waitpid () from /lib64/libc.so.6

#1 0x00007f3416128f12 in do_system () from /lib64/libc.so.6

#2 0x00007f341ac9bf95 in TUnixSystem::StackTrace() () from /site/12gev_phys/2.4/Linux_CentOS7.7.1908-gcc9.2.0/root/6.20.04/lib/libCore.so.6.20

#3 0x00007f341ac9900c in TUnixSystem::DispatchSignals(ESignals) () from /site/12gev_phys/2.4/Linux_CentOS7.7.1908-gcc9.2.0/root/6.20.04/lib/libCore.so.6.20

#4

#5 0x00007f33ff158089 in ?? ()

#6 0x00007f3419a8e77c in TF1::EvalPar(double const*, double const*) [clone .localalias] () from /site/12gev_phys/2.4/Linux_CentOS7.7.1908-gcc9.2.0/root/6.20.04/lib/libHist.so.6.20

#7 0x00007f3419a8f286 in TF1::Eval(double, double, double, double) const [clone .localalias] () from /site/12gev_phys/2.4/Linux_CentOS7.7.1908-gcc9.2.0/root/6.20.04/lib/libHist.so.6.20

#8 0x00007f33ff15168d in ?? ()

#9 0x000000000d2e1e30 in ?? ()

#10 0x000000000d2eeeb0 in ?? ()

#11 0x000000000d2fbf30 in ?? ()

#12 0x000000000d308fb0 in ?? ()

#13 0x000000000d316030 in ?? ()

#14 0x000000000d3230b0 in ?? ()

#15 0x000000000d2e26e0 in ?? ()

#16 0x000000000d2ef760 in ?? ()

#17 0x000000000d2fc7e0 in ?? ()

#18 0x000000000d309860 in ?? ()

#19 0x000000000d3168e0 in ?? ()

#20 0x000000000d323960 in ?? ()

#21 0x000000000d2e2f90 in ?? ()

#22 0x000000000d2f0010 in ?? ()

#23 0x000000000d2fd090 in ?? ()

#24 0x000000000d30a110 in ?? ()

Hello,

Herewith attached my .root file and code for creating canvas.
When I run this as “.L Canvas_draw.cpp”. It gave me a crash.Canvas_draw.cpp (5.4 KB)
efficiency_datatest_pim.root (985.3 KB)

Any help would be greatly appreciated.

h_pimMX2array_3fold_bgnd is undefined in your macro.

No.I defined it as follows,

TH1F *h_pimMX2array_3fold_bgnd[i_theta_bins][n_p][sectors];
    char histName_3fold_bgnd[64];
    for (int isec=0; isec<sectors; isec++) {
        for (int ith=0; ith<i_theta_bins; ith++) {
            for (int ippim=0; ippim<n_p; ippim++) {
                
                sprintf(histName_3fold_bgnd,"h_pimMX23foldbgndarray_p%03d_th%03d_sec%03d",ippim,ith,isec);
                h_pimMX2array_3fold_bgnd[ith][ippim][isec] = new TH1F(histName,"3fold and 4fold Missing pimMassSq",300,-0.5,0.5);
                h_pimMX2array_3fold_bgnd[ith][ippim][isec]->SetTitle(Form("P bin %d / th bin %d / sec %d ",ippim,ith,isec));
            }
        }
    }

root [0] 
Processing Canvas_draw.cpp...
In file included from input_line_9:1:
/Users/couet/Downloads/Canvas_draw.cpp:23:53: error: use of undeclared identifier 'h_pimMX2array_3fold_bgnd'
                n3bgnd[i_theta_bins][n_p][sectors] =h_pimMX2array_3fold_bgnd[ith][ippim][isec]->GetEntries();

Thank you for reply.
I solved this. Look like my histogram names are the issue.

-Dil

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