Create mutliple histogram objects

Hello co-rooters!

I am facing the following issue :

In my code I have to define a lot of histograms, that share almost the same name (i.e. ha1, ha2, ha3, …)
This is the case for many other histograms wihtin my code (hb1, hb2, hb3, …, hc1, hc2, hc3, …)

I thought about making a function that will construct those histograms. Although I tried to do it, I am not sure if it’s possible and if I am on the right track.

My code so far is

TH1F test_root(int number, char *name, double nbins, double bin_low, double bin_high){ TString hname(name); TH1F *myhist[number]; char *histname = new char[10]; for (int i=1; i<=number; i++){ sprintf(histname,"h%s%d",hname,i); //myhist[i] = new TH1F(TString::Format("%s%d",hname.Data(),i),TString::Format("%s%d",hname.Data(),i),nbins,bin_low,bin_high); myhist[i] = new TH1F (histname,"",nbins,bin_low,bin_high); return myhist[i]; } }

When I run the code using .x test_root.c(2,“amp”,100,0,100) I get the error

[quote]Error: Non-static-const variable in array dimension test_root.c:3: (cint allows this only in interactive command and special form macro which is special extension. It is not allowed in source code. Please ignore subsequent errors.) *** Interpreter error recovered ***[/quote]

I am sure that my code is not correct, but at least I have a starting point… Any help would be more than welcome!

Hi,

Yes it’s possible. In fact there are quite a lot of ways to do this; you could have a look through previous postings. For instance:

In your example the error quoted is because variable length arrays aren’t allowed (although there are some other problems in the example).

One possibility could be to use a vector of pointers to TH1F objects:

#include <vector>

std::vector<TH1F*> test_root(Int_t number, const char *name, Int_t nbins, Double_t bin_low, Double_t bin_high){
  std::vector<TH1F*> v;
  for(Int_t i=1;i<=number;i++) {
    TString hname = TString::Format("h%s%d",name,i);
    TH1F *h = new TH1F(hname.Data(),"",nbins,bin_low,bin_high);
    v.push_back(h);
  }
  return v;
}

Thank you very much for your answer!
Althought this code works standalone, when I include it in my main code I get the error

[quote]./fimg_v6.C:114: error: request for member ‘c’ in ‘test_root’, which is of non-class type ‘std::vector<TH1F*, std::allocator<TH1F*> >(Int_t, const char*, Int_t, Double_t, Double_t)’ ./fimg_v6.C:124: error: ‘hamp1’ was not declared in this scope ./fimg_v6.C:124: error: ‘hamp2’ was not declared in this scope ./fimg_v6.C:124: error: ‘hamp3’ was not declared in this scope ./fimg_v6.C:125: error: ‘hamp4’ was not declared in this scope ./fimg_v6.C:125: error: ‘hamp5’ was not declared in this scope ./fimg_v6.C:125: error: ‘hamp6’ was not declared in this scope[/quote]

I am compilling my code using this make file

[code]INC=’-I/usr/include/shift -I/cern/pro/include/cfortran -I/usr/local/include -I/usr/include’
LIB=’-L/usr/lib64/cernlib/2006/lib -lmathlib -lpacklib -lkernlib -L/usr/lib64 -lshift -lnsl -lcrypt -ldl -lm -L./lx6libs’
OPT=’-O0 -fPIC -Wall’

g++ -Wall ${OPT} -fPIC root-config --cflags -c ./fimg_v6.C $INC
g++ -Wall ${OPT} fimg_v6.o $INC root-config --glibs $LIB -o fimg_v6.exe

rm fimg_v6.o[/code]

Hi,

I don’t think I can say what might be wrong without seeing fimg_v6.C. Do you want to post it?

Sure!

Please find a link to it

dropbox.com/s/64xgy1kdlp6us … _v6.C?dl=0

Cheers

I’m afraid we need all your source code files, i.e. including “c_header.h”, “root_header.h”, “global_variables.h”, “functions.h” (try maybe to create a “.tar.gz” file with all of them and attach it here).

That makes sense!

Thank you!
root_forum.zip (7.48 KB)

I get no errors when I try:
root-config --cxx --cflags fimg_v6.C root-config --glibs -o fimg_v6.exe

Try to rename “functions.h” into something like “my_functions.h” (another file “functions.h” may exist somewhere in your "${INC}).

The test_root() function is part of the root_header.h, however I renamed functions.h to my_functions.h

The thing is that I still get an error in compilling

[astamato@lxplus0105 THANOS]$ `root-config --cxx --cflags` fimg_v6.C `root-config --glibs` -o fimg_v6.exe fimg_v6.C: In function ‘int main(int, char* const*)’: fimg_v6.C:124: error: ‘hamp1’ was not declared in this scope fimg_v6.C:124: error: ‘hamp2’ was not declared in this scope fimg_v6.C:124: error: ‘hamp3’ was not declared in this scope fimg_v6.C:125: error: ‘hamp4’ was not declared in this scope fimg_v6.C:125: error: ‘hamp5’ was not declared in this scope fimg_v6.C:125: error: ‘hamp6’ was not declared in this scope

Post the output of:
which g++
g++ --version
root-config --cxx --cflags
root-config --glibs

[astamato@lxplus0001 THANOS]$ which g++ /usr/bin/g++

[astamato@lxplus0001 THANOS]$ g++ --version g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16) Copyright (C) 2010 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[astamato@lxplus0001 THANOS]$ root-config --cxx --cflags g++ -pthread -m64 -I/usr/include/root

[astamato@lxplus0001 THANOS]$ root-config --glibs -L/usr/lib64/root -lGui -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic

It is possible that you meet a limitation of your (a bit old) compiler.
Try the attached “fimg_v6.C” file.
If I am right, you will get errors pointing to lines 220, 221, …
fimg_v6.C (26.3 KB)