Compiled macro can't be executed

Hello,

I can’t execute this macro.

int binchangeinrun(int runnum){
  char *calibpath;
  sprintf(calibpath, "./%04d/calib.txt",runnum);

  ifstream ifs(calibpath);
  cout<<calibpath << " is read."<<endl;
  double a[22];
  double channum[22];

  float A[22][3] = {};

  string str;
  int ii = 0;
  int jj = 0;
  int kk = 0;
  float temp = 0;

  if(!ifs){
      std::cout<<"Error!";
  return 1;
  }

  while(getline(ifs,str)){
    string token;
    istringstream stream(str);

    while(getline(stream,token,'\t')){
      ii = kk/3;
      jj = kk%3;

      temp = stod(token); 
      A[ii][jj] = temp; 
      kk++;
    }
  }

  for (size_t hh = 0; hh < 22; hh++) {
    channum[hh]=A[hh][0];
    a[hh]=A[hh][2];
  }

  TFile *specout = new TFile("./outroot/Spectrumbintest.root","recreate");
  TH1D *newhist;
  TH1D *hist[22];
  newhist = new TH1D(Form("GammaEnergy_Run%d",runnum),Form("Run %d,GammaEnergy",runnum),10000,0,10000);

  char *histname;

  TFile *file = TFile::Open(Form("./outroot/Spectrum_run%04d.root",runnum));

  cout<<"run"<< runnum << " is loaded."<<endl;

  int chnum[22]={1,2,3,4,5,6,7,8, 17,18,19,20,21,22,23, 29,30,31,32,33,34,35};

  for (size_t i = 0; i < 22; i++) {
    sprintf(histname,"GammaEnergy_s%d_Run%d",chnum[i],runnum);
    hist[i] = (TH1D*)file->Get(histname);
  }

  ///////////////////////////////////////////////////////////////
  int binnum = 10000;

  int n;
  double xx;
  int x;
  double yy;
  int y;

  int event_x;
  int event_y;
  int event_z;
  int c_event;

  for(int d = 0; d < binnum; d ++){
     for (size_t i = 0; i < 22; i++) {
     }
}
  return 0;
}

It is compiled with no error.
But when I execute the macro, root called

*** Break *** segmentation violation

[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)

[&lt;unknown binary&gt;] (no debug info)

[/usr/lib/system/libsystem_c.dylib] __sfvwrite (no debug info)

[/usr/lib/system/libsystem_c.dylib] __vfprintf (no debug info)

[/usr/lib/system/libsystem_c.dylib] __v2printf (no debug info)

[/usr/lib/system/libsystem_c.dylib] vsprintf_l (no debug info)

[/usr/lib/system/libsystem_c.dylib] sprintf (no debug info)

[/Volumes/BUFFALO/binchange_cpp.so] binchangeinrun(int) (no debug info)

[&lt;unknown binary&gt;] (no debug info)

[/usr/local/root-6.14.04/cmake_build/lib/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)

[/usr/local/root-6.14.04/cmake_build/lib/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)

[/usr/local/root-6.14.04/cmake_build/lib/libCling.so] cling::Interpreter::process(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, cling::Value*, cling::Transaction**, bool) (no debug info)

[/usr/local/root-6.14.04/cmake_build/lib/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&amp;, cling::Value*, bool) (no debug info)

[/usr/local/root-6.14.04/cmake_build/lib/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&amp;, cling::Value*) /usr/local/root-6.14.04/core/metacling/src/TCling.cxx:2069

[/usr/local/root-6.14.04/cmake_build/lib/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) /usr/local/root-6.14.04/core/metacling/src/TCling.cxx:2218

[/usr/local/root-6.14.04/cmake_build/lib/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) /usr/local/root-6.14.04/core/rint/src/TRint.cxx:741

[/usr/local/root-6.14.04/cmake_build/lib/libRint.so] TRint::HandleTermInput() /usr/local/root-6.14.04/core/rint/src/TRint.cxx:603

[/usr/local/root-6.14.04/cmake_build/lib/libCore.so] TUnixSystem::CheckDescriptors() /usr/local/root-6.14.04/core/unix/src/TUnixSystem.cxx:1324

[/usr/local/root-6.14.04/cmake_build/lib/libCore.so] TMacOSXSystem::DispatchOneEvent(bool) /usr/local/root-6.14.04/core/macosx/src/TMacOSXSystem.mm:378

[/usr/local/root-6.14.04/cmake_build/lib/libCore.so] TSystem::InnerLoop() /usr/local/root-6.14.04/core/base/src/TSystem.cxx:412

[/usr/local/root-6.14.04/cmake_build/lib/libCore.so] TSystem::Run() /usr/local/root-6.14.04/core/base/src/TSystem.cxx:362

[/usr/local/root-6.14.04/cmake_build/lib/libCore.so] TApplication::Run(bool) /usr/local/root-6.14.04/core/base/src/TApplication.cxx:1174

[/usr/local/root-6.14.04/cmake_build/lib/libRint.so] TRint::Run(bool) /usr/local/root-6.14.04/core/rint/src/TRint.cxx:458

[/usr/local/root-6.14.04/cmake_build/bin/root.exe] main /usr/local/root-6.14.04/main/src/rmain.cxx:32

[/usr/lib/system/libdyld.dylib] start (no debug info)

[&lt;unknown binary&gt;] (no debug info)

What is the problem…?

What is debug info?

Simple macro (it is only cout<<OK<<endl) can excecute.


_ROOT Version:6.14.04(cmake)
_Platform:macOS High Sierra 10.13.6
_Compiler:Xcode Version 10.0 (10A255)


Hi,

I’ll check, but in the meanwhile, please take a look at this:

And see how I edited your code :wink:

Compiling in debug mode (-g compiler flag or adding a g when using ACLiC)

BTW, how do you compile your code?
And can you also provide the necessary files to run the code?

Cheers, Bertrand.

bellenot

Thank you for your response.

I’m sorry to send code which is hard to see.

I attached the code.

I conpile like below,

Sato:BUFFALO sato$ root
root [0] .L binchange.cpp++
Info in <TMacOSXSystem::ACLiC>: creating shared library /Volumes/BUFFALO/./binchange_cpp.so
root [1] binchangeinrun(18)

calib.txt (931 Bytes)
binchange.cpp (15.3 KB)

This is simple code.
cout.cc (379 Bytes)

Hi,

I have those warnings when compiling:

binchange.cpp(87) : warning C4700: uninitialized local variable 'calibpath' used
binchange.cpp(143) : warning C4700: uninitialized local variable 'histname' used

So changing char *calibpath; to char calibpath[1024]; and char *histname; to char histname[1024]; solves already one problem…

Cheers, Bertrand.

Sorry.
Please put [calib.txt] in ./0025/ and put Spectrum_run0025.root (sorry. The root file is too big to upload.)

Well, first of all, did you try to modify your code? Did it run?

Yes.
I modified it many time, but sometime , I can’t understand the behavior.

The code I send was not work. So I deleted some line and add [cout<<“OK”<<endl;] for debug.
It didn’t work.
However I deleted [cout<<“OK”<<endl;] , there are no error.

I can’t understand the pattern. It’s not reproductibe.

I just explained how to fix your code. Could you try this version: binchange.cpp (15.3 KB)

It worked.

The Problem is size of char ?

No, as I said, to fix those warnings:

binchange.cpp(87) : warning C4700: uninitialized local variable 'calibpath' used
binchange.cpp(143) : warning C4700: uninitialized local variable 'histname' used

this declaration:

char *calibpath;

must be something like:

char calibpath[1024];

and this one too:

char *histname;

must be something like:

char histname[1024];

otherwise you access invalid memory via uninitialized pointers (this is very basic c++)

1 Like

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