How to parse a TH1*D hist[l][m][n] to a function

Dear experts,

I have a 3D TH1D* and I want to parse it to a function. I did [1] but I had the error message[2]. Do you know what is wrong?

Regards

[2]
error: no matching function for call to 'setaxis’
setaxis( hist[ifile][countdir][nvar]);
^~~~~~~
note: candidate function not viable: no known conversion
from 'TH1D ’ to ‘TH1D ()[1][8]’ for 1st argument
void setaxis(TH1D
h[4][1][8],
^

[1]

void setaxis(TH1D* h[4][1][8] ){

}

void test(){
TH1D *hist[4][1][8];

setaxis(hist);
}

If it’s ROOT 5, then see: Multidimensional Array
and try something like this (note: you may need to precompile your source code using ACLiC): [code]#include “TH1.h”

typedef TH1D* (*My3DTH1DArray)[4][1][8];

void setaxis(My3DTH1DArray h) {
// …
}

void test() {
TH1D* hist[4][1][8];
// …
setaxis(&hist);
}[/code]
I guess a better idea would be to move to ROOT 6 (you should not need any tricks then).

Dear Pepe,

thank you for your answer. When I’m using [1] with"->" to access method have the error message[2], when I use [3] with “.” I have error message [4]. Do you know what is wrong?

Regards

[1]
#include “TH1.h”

typedef TH1D* (*My3DTH1DArray)[4][1][8];

void setaxis(My3DTH1DArray h,
const std::vectorstd::string &vfile,
const std::vectorstd::string &vdir,
const std::vectorstd::string &vvar
) {

for(int file=0; file<vfile.size(); file++){
for(int dir=0; dir<vdir.size(); dir++){
for(int var=0; var<vvar.size(); var++) {

  int temp = h[file][dir][var] -> FindLastBinAbove(0.0005);

 }

}
}
}

void test() {
TH1D* hist[4][1][8];
vector vfile;
vector vdir;
vector vvar;

setaxis(&hist, vfile, vdir, vvar);
}

[2]
error: member reference
base type ‘TH1D *’ is not a structure or union
temp = h[file][dir][var] -> FindLastBinAbove(0.0005); //get last bin on x axis with data
~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~
In file included from input_line_9:1:
/lstore/cms/calpas/h2taus/CMSSW_8_0_20/src/miniAOD/miniAnalyzer/makeHist/plot_h2taus.cc:123:5: error: no matching function
for call to 'setaxis’
setaxis( &hist, vfile, vdir, vvar);
^~~~~~~
/lstore/cms/calpas/h2taus/CMSSW_8_0_20/src/miniAOD/miniAnalyzer/makeHist/…/func/myfunc.cc:75:6: note: candidate function not
viable: no known conversion from ‘TH1D ()[nfile][ndir][maxvar]’ to ‘My3DTH1DArray’ (aka ‘TH1D ()[4][1][8]’) for 1st
argument
void setaxis(My3DTH1DArray h,

[3]
#include “TH1.h”

typedef TH1D* (*My3DTH1DArray)[4][1][8];

void setaxis(My3DTH1DArray h,
const std::vectorstd::string &vfile,
const std::vectorstd::string &vdir,
const std::vectorstd::string &vvar
) {

for(int file=0; file<vfile.size(); file++){
for(int dir=0; dir<vdir.size(); dir++){
for(int var=0; var<vvar.size(); var++) {

  int temp = h[file][dir][var] . FindLastBinAbove(0.0005);

 }

}
}
}

void test() {
TH1D* hist[4][1][8];
vector vfile;
vector vdir;
vector vvar;

setaxis(&hist, vfile, vdir, vvar);
}

[4]
In function ‘void setaxis(My3DTH1DArray, const std::vector<std::basic_string >&, const std::vector<std::basic_string >&, const std::vector<std::basic_string >&)’:
…/func/myfunc.cc:91:29: error: request for member ‘FindLastBinAbove’ in '((h + ((sizetype)(((long unsigned int)file) * 256ul))))[dir][var]’, which is of non-class type 'TH1D [8]'
temp = h[file][dir][var] . FindLastBinAbove(0.0005);

Hi,

The typedef is wrong - it’s an array of function ptrs :slight_smile: For ROOT 6, just do the following:

using My3DTH1DArray = std::array<std::array<std::array<TH1D*,4>,1>,8>;
void setaxis(My3DTH1DArray& h,
const std::vector<std::string> &vfile,
const std::vector<std::string> &vdir,
const std::vector<std::string> &vvar 
)

That will allow the compiler to complain in a much more obvious way than with C-style arrays and pointers.

Cheers, Axel.

int temp = ((*h)[file][dir][var]) -> FindLastBinAbove(0.0005);
Note: you need to precompile this source code in ROOT 5 (CINT will choke on it when “interpreted”) … something like … "root [0] .L setaxis.cxx++"
P.S. In ROOT 6, you do not need to play any “My3DTH1DArray” tricks.

Dear Axel,

your code works fine. Thank you for your help.

Regards

Dear Pepe,

some how Alex’s code worked. Thank you too for your help.

Regards