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)