Invalid memory pointer passed to a callee when running root in a bash script


ROOT Version: 6.18/04
Platform: f30
Compiler: 9.2.1 20190827


Dear co-rooters,

I am trying to manipulate some data files, create histogram and store them in root files. To do so I created a bash script that does all that. Inside this script I run two root macros.

The issue is that when I run the last root macro I get the following error which I don’t really get why.

plot_histos.C:17:6: warning: invalid memory pointer passed to a callee:
h->SetLineColor(color);

Any idea on what might be the issue?
Thanks in advance!

My codes are the following:

-sum.sh

#!/bin/bash
# Run it using the following command
# ./sum.sh tp2014-0010-10_adc 2

# The first argument will be the input file name
# The second argument will be the number of files to be proccessed

for i in $(seq -f "%03g" 1 $2); do
    # First I need to get rid of the spaces in the file names because awk gets confused'
    cp *$i.his $1_$i.hist
    # Then just create a two column file using awk, 1st column = channels, 2nd column = counts
    awk '{print NR, $1}' $1_$i.hist >$1_$i.dat
    # Run the root macro histo_from_ascii to produce a root file with all the histograms
    root -l -q "histo_from_ascii.C(\"$1_$i.dat\", \"$1_$i\", \"$1.root\")"
    root -l -q "plot_histos.C(\"$1.root\", \"$1\")"
done

-histo_from_ascii.C

#include "header_c.h"
#include "header_root.h"

void histo_from_ascii(string filename_in, TString histo_name, TString filename_out){
	
	std::vector<float> X, Y;
	float              x, y;
	
	ifstream f_in;
		
	f_in.open(filename_in);
	
	while(1){
		f_in >> x >> y;
		x = x-0.5;
		if ( !f_in.good() ) break;
		X.push_back(x);
		Y.push_back(y);
	}
	
	f_in.close();
	
	
	int nbins = X.size();
	
	
	TH1F *h = new TH1F( histo_name, histo_name, nbins, X[0], X[nbins-1]);
	
	for ( int i=0; i< h->GetNbinsX(); ++i ){
		h->SetBinContent(i+1, Y[i]);
	}
	
	TFile *fout = new TFile(filename_out, "UPDATE");
		h->Write();
	fout->Close();
	

}

-plot_histos.C

#include "header_c.h"
#include "header_root.h"

void plot_histos(TString filename_in, TString histo_name){

	TFile   *fin = new TFile(filename_in, "OPEN");
	TRandom *r   = new TRandom();
	TCanvas *c   = new TCanvas();
	
	//string h_name = filename_in;
	for (int i=1; i<11; ++i){
	    
        //TH1F *h = (TH1F*) fin->Get(histo_name);
        TH1F *h = (TH1F*) fin->Get(TString::Format("%s_%03i", histo_name.Data(), i) );	
	
	    int color = (int) ((113-51)*r->Rndm(i)+51);
	    h->SetLineColor(color);
	    h->SetLineWidth(4);
	    h->Draw("histo same");
	
	}
	
	c->SaveAs(TString::Format("Canvas_%s", filename_in.Data()));

}

I am also attaching a zip file with a couple of sample filessum_script.zip (5.3 KB)

TString h_name = histo_name + TString::Format("_%03i", i);
TH1F *h; fin->GetObject(h_name, h);
if (!h) { std::cout << h_name << " not found." << std::endl; continue; }

Thanks a lot for your reply!
I changed the plot.C macro according to your recommendation but I still get the same…

#include "header_c.h"
#include "header_root.h"

void plot_histos(TString filename_in, TString histo_name){

	TFile   *fin = new TFile(filename_in, "OPEN");
	TRandom *r   = new TRandom();
	TCanvas *c   = new TCanvas();
	
	//string h_name = filename_in;
	for (int i=1; i<11; ++i){
	    
        //TH1F *h = (TH1F*) fin->Get(histo_name);
        //TH1F *h = (TH1F*) fin->Get(TString::Format("%s_%03i", histo_name.Data(), i) );
        TString h_name = histo_name + TString::Format("_%03i", i);
        TH1F *h; fin->GetObject(h_name, h);
        if (!h) { std::cout << h_name << " not found." << std::endl; continue; }
	
	    int color = (int) ((113-51)*r->Rndm(i)+51);
	    h->SetLineColor(color);
	    h->SetLineWidth(4);
	    h->Draw("histo same");
	
	}
	
	c->SaveAs(TString::Format("Canvas_%s", filename_in.Data()));

}
[astamato@localhost test]$ ./sum.sh tp2014-0010-10_adc 2

Processing /home/astamato/Documents/n_ToF/237Np/TargetCharacterisation_JRC/Zina/Thanos/scripts/histo_from_ascii.C("tp2014-0010-10_adc_001.dat", "tp2014-0010-10_adc_001", "tp2014-0010-10_adc.root")...

Processing /home/astamato/Documents/n_ToF/237Np/TargetCharacterisation_JRC/Zina/Thanos/scripts/plot_histos.C("tp2014-0010-10_adc.root", "tp2014-0010-10_adc")...
#0  0x00007f976ed1e4fb in waitpid () from /lib64/libc.so.6
#1  0x00007f976ec9ae37 in do_system () from /lib64/libc.so.6
#2  0x00007f976f39ad3e in TUnixSystem::StackTrace() () from /usr/lib64/root/libCore.so.6.18
#3  0x00007f976aed536d in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /usr/lib64/root/libCling.so
#4  0x00007f976aed4dc4 in cling_runtime_internal_throwIfInvalidPointer () from /usr/lib64/root/libCling.so
#5  0x00007f976f4fa49b in ?? ()
#6  0x0000556f7cef8090 in ?? ()
#7  0x0000556f7d85cbf0 in ?? ()
#8  0x0000556f7d785e90 in ?? ()
#9  0x00007f976af4818a in std::_Function_handler<llvm::Expected<unsigned long> (), llvm::orc::RTDyldObjectLinkingLayer::ConcreteLinkedObject<std::shared_ptr<llvm::RuntimeDyld::MemoryManager>, std::shared_ptr<llvm::JITSymbolResolver>, llvm::orc::RTDyldObjectLinkingLayer::addObject(std::shared_ptr<llvm::object::OwningBinary<llvm::object::ObjectFile> >, std::shared_ptr<llvm::JITSymbolResolver>)::{lambda(std::_List_iterator<std::unique_ptr<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject, std::default_delete<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject> > >, llvm::RuntimeDyld&, std::shared_ptr<llvm::object::OwningBinary<llvm::object::ObjectFile> > const&, std::function<void ()>)#1}>::getSymbolMaterializer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)::{lambda()#1}>::_M_invoke(std::_Any_data const&) () from /usr/lib64/root/libCling.so
#10 0x00007fff18d09518 in ?? ()
#11 0x00007fff18d09530 in ?? ()
#12 0x000000647c67fa40 in ?? ()
#13 0x0000000000000000 in ?? ()
Error in <HandleInterpreterException>: Trying to access a pointer that points to an invalid memory address..
Execution of your code was aborted.
In file included from input_line_8:1:
/home/astamato/Documents/n_ToF/237Np/TargetCharacterisation_JRC/Zina/Thanos/scripts/plot_histos.C:17:6: warning: invalid memory pointer passed to a callee:
            h->SetLineColor(color);
            ^

The crash happens in line 17 which does not correspond to your new “plot.C” file.

You are right.
I was running a different version.
It works, so thanks a lot!

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