Calling TH1F function in a loop

Hi. I am having troubles with reading TH1F in a loop. In function calc_vthresh,

#define NCHMOD 1536


TH1F *readDataFile(char* fname, int nch) {
  int ch,counts;
  float fc;
  TH1F *h = new TH1F("h1","",nch,-0.5,nch-0.5);
  h->Sumw2();
  FILE* fp=fopen(fname,"r");
  if (fp==NULL) {
    printf("Could not open file %s\n",fname);
    return h;
  }
  for (int j=0; j<nch; j++) {  
      fscanf(fp,"%d %f",&ch,&fc);
      counts=fc;    
      h->SetBinContent(j+1,fc);
  }
  fclose(fp);
  return h;
}


TH2F* createScan(char* fn, int mi, int ma, int step, int nch) {
  char fname[100];
  TH1F *hdum;
  Int_t nstep=((ma-mi)/step)+1;
  TH2F *h2 = new TH2F("h2","",nch,-0.5,nch-0.5,nstep,mi-step/2., ma+step/2.);
  h2->SetStats(kFALSE);
  for (float i=mi; i<ma+1; i=i+step) {
    sprintf(fname,fn,i);
    hdum=readDataFile(fname, nch);
    for (int j=0; j<nch; j++)
      h2->Fill(j,i,hdum->GetBinContent(j+1));
    delete hdum;
  }
  return h2;
}

TH1F * getCh(TH2F* h2, int chan) {
  int nstep=h2->GetYaxis()->GetNbins();
  Double_t mi=h2->GetYaxis()->GetXmin();
  Double_t ma=h2->GetYaxis()->GetXmax();
  
  char channel[]= "Channel";
  char chanNum[100]; 
  sprintf(chanNum, "%s %d", channel, chan);
  
  TH1F *h1=new TH1F("h1",chanNum,nstep,mi,ma);
  for (int i=1; i<nstep+1; i++) { 
    h1->SetBinContent(i, h2->GetBinContent(chan+1,i));
    
  }
  return h1;
}


int calc_vthresh(TH2F *h2, int mod) {
  int Vthresh;
  TH1F *hdum;
	
  if(mod ==0) {
    minChan= 0;
    maxChan =NCHMOD;  
  }else if(mod==1) {  
     minChan=NCHMOD;
     maxChan = 2*NCHMOD-1;
  }else if(mod==2){  
      minChan= 2*NCHMOD;
      maxChan = 3*NCHMOD-1;
  }else if(mod==3) {
     minChan= 3*NCHMOD;
     maxChan = 4*NCHMOD-1;
  }else if(mod==4) {
    minChan= 4*NCHMOD;
    maxChan = 5*NCHMOD-1;
  }else(mod==5) {
    minChan= 5*NCHMOD;
    maxChan =6*NCHMOD-1;
  }
  
  for(int ichan = minChan; ichan<maxChan+1; ichan++) {
     hdum=getCh(h2,ichan);   //segmentation violation here
    //do the task here..........
     delete hdum;
  }
  return Vthresh;
}

message says…

*** Break *** segmentation violation
Attaching to program: /proc/11948/exe, process 11948

warning: .dynamic section for "/lib/libdl.so.2" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

warning: .dynamic section for "/usr/lib/libstdc++.so.6" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

warning: .dynamic section for "/lib/libm.so.6" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

warning: .dynamic section for "/lib/libgcc_s.so.1" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

warning: .dynamic section for "/lib/libpthread.so.0" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

warning: .dynamic section for "/usr/lib/libXrender.so.1" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

warning: .dynamic section for "/usr/lib/libXinerama.so.1" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

warning: .dynamic section for "/usr/lib/libXext.so.6" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

warning: .dynamic section for "/usr/lib/libXau.so.6" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

warning: .dynamic section for "/usr/lib/libXdmcp.so.6" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

warning: .dynamic section for "/usr/lib/libXcursor.so.1" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

warning: .dynamic section for "/usr/lib/libXfixes.so.3" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations
[Thread debugging using libthread_db enabled]
0x00ed8402 in __kernel_vsyscall ()
#1  0x00c72cb3 in __waitpid_nocancel () from /lib/libc.so.6
#2  0x00c1762b in do_system () from /lib/libc.so.6
#3  0x00386f3d in system () from /lib/libpthread.so.0
#4  0x00798477 in TUnixSystem::Exec(char const*) () from /users/root_v5.20/lib/libCore.so
#5  0x0079df01 in TUnixSystem::StackTrace() () from /users/root_v5.20/lib/libCore.so
#6  0x0079ab46 in TUnixSystem::DispatchSignals(ESignals) () from /users/root_v5.20/lib/libCore.so
#7  0x0079abd4 in SigHandler(ESignals) () from /users/root_v5.20/lib/libCore.so
#8  0x00799e19 in sighandler(int) () from /users/root_v5.20/lib/libCore.so
#9  <signal handler called>
#10 0x085395eb in G__G__Hist_107_0_79(G__value*, char const*, G__param*, int) ()
   from /users/root_v5.20/lib/libHist.so
#11 0x00f152e3 in Cint::G__ExceptionWrapper(int (*)(G__value*, char const*, G__param*, int), G__value*, char*, G__param*, int) () from /users/root_v5.20/lib/libCint.so
#12 0x00f277a2 in G__exec_asm () from /users/root_v5.20/lib/libCint.so
#13 0x00f2af26 in G__exec_bytecode () from /users/root_v5.20/lib/libCint.so
#14 0x00f8b228 in G__interpret_func () from /users/root_v5.20/lib/libCint.so
#15 0x00f79966 in G__getfunction () from /users/root_v5.20/lib/libCint.so
#16 0x00f5de70 in G__getitem () from /users/root_v5.20/lib/libCint.so
#17 0x00f60abb in G__getexpr () from /users/root_v5.20/lib/libCint.so
#18 0x00f6655d in G__getexpr () from /users/root_v5.20/lib/libCint.so
#19 0x00fd3b4f in G__exec_statement () from /users/root_v5.20/lib/libCint.so
#20 0x00fdf12a in G__exec_loop () from /users/root_v5.20/lib/libCint.so
#21 0x00fdea3b in G__exec_statement () from /users/root_v5.20/lib/libCint.so
#22 0x00f8ca8b in G__interpret_func () from /users/root_v5.20/lib/libCint.so
#23 0x00f79966 in G__getfunction () from /users/root_v5.20/lib/libCint.so
#24 0x00f5de70 in G__getitem () from /users/root_v5.20/lib/libCint.so
#25 0x00f60abb in G__getexpr () from /users/root_v5.20/lib/libCint.so
#26 0x00f502e4 in G__define_var () from /users/root_v5.20/lib/libCint.so
#27 0x00fd87ca in G__exec_statement () from /users/root_v5.20/lib/libCint.so
#28 0x00f4bc68 in G__exec_tempfile_core () from /users/root_v5.20/lib/libCint.so
#29 0x00f4cfa3 in G__exec_tempfile_fp () from /users/root_v5.20/lib/libCint.so
#30 0x00fe4b2c in G__process_cmd () from /users/root_v5.20/lib/libCint.so
#31 0x007878db in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) ()
   from /users/root_v5.20/lib/libCore.so
#32 0x006cca16 in TApplication::ProcessLine(char const*, bool, int*) ()
   from /users/root_v5.20/lib/libCore.so
#33 0x00238e9a in TRint::HandleTermInput() () from /users/root_v5.20/lib/libRint.so
#34 0x002374f0 in TTermInputHandler::Notify() () from /users/root_v5.20/lib/libRint.so
#35 0x0023978e in TTermInputHandler::ReadNotify() () from /users/root_v5.20/lib/libRint.so
#36 0x00796e02 in TUnixSystem::CheckDescriptors() () from /users/root_v5.20/lib/libCore.so
#37 0x0079b024 in TUnixSystem::DispatchOneEvent(bool) () from /users/root_v5.20/lib/libCore.so
#38 0x007248bc in TSystem::InnerLoop() () from /users/root_v5.20/lib/libCore.so
#39 0x00724683 in TSystem::Run() () from /users/root_v5.20/lib/libCore.so
#40 0x006ccb02 in TApplication::Run(bool) () from /users/root_v5.20/lib/libCore.so
#41 0x00237c6e in TRint::Run(bool) () from /users/root_v5.20/lib/libRint.so
#42 0x08048d83 in main ()
A debugging session is active.

        Inferior 1 [process 11948] will be detached.

Quit anyway? (y or n) [answered Y; input not from terminal]
Detaching from program: /proc/11948/exe, process 11948
Root > Function getCh() busy flag cleared
Function calc_vthresh() busy flag cleared

thanks for the help

F.

Hi,

I don’t see anything obvious. The best is to try when compiling with ACliC (i.e.: .L calc_vthresh.C+ instead of .L calc_vthresh.C) and if that does not solve the problem to run with valgrind (by starting root as:valgrind root.exe

Cheers,
Philippe.