Compiled macro can't be executed


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;

  return 1;

    string token;
    istringstream stream(str);

      ii = kk/3;
      jj = kk%3;

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

  for (size_t hh = 0; hh < 22; hh++) {

  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++) {
    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/] binchangeinrun(int) (no debug info)

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

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

[/usr/local/root-6.14.04/cmake_build/lib/] 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/] 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/] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&amp;, cling::Value*, bool) (no debug info)

[/usr/local/root-6.14.04/cmake_build/lib/] 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/] 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/] 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/] TRint::HandleTermInput() /usr/local/root-6.14.04/core/rint/src/TRint.cxx:603

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

[/usr/local/root-6.14.04/cmake_build/lib/] TMacOSXSystem::DispatchOneEvent(bool) /usr/local/root-6.14.04/core/macosx/src/

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

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

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

[/usr/local/root-6.14.04/cmake_build/lib/] 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)


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.


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/./
root [1] binchangeinrun(18)

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

This is simple code. (379 Bytes)


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.

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?

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.