2dim array of THF1 with variable dimensions

How can I allocate a 2 dim array [of THF1] with its dims being defined at a later point in the code. The following did not work:
class DrawIt{
//…
void GetHistosTest;
TH1F* PhHistos_10;
DrawIt::DrawIt(): PhHistos_10=NULL
{}
void DrawIt::GetHistosTest(){
int const dima=DimA; int const dimb=DimB;
PhHistos_10= new TH1F[dima][dimb];
for(int a=0;a<dima;a++){
for(int b=0;b<dimb;b++){
GetHistosFile(FirstLoopSet[a],SecondLoopSet[b], FirstNoLoopSet[0], SecondNoLoopSet[0]);
TH1F* hist=(TH1F*) gDirectory->Get(“H_Alpha_UpLeft”);
hist->SetDirectory(0);
HistosFile->Close(“R”);
PhHistos_10[a][b]=hist;
}}
}

Hi,

I think I understand from your example

void DrawIt::GetHistosTest()
{
   int const dima = DimA;
   int const dimb = DimB;
   PhHistos_10 = new TH1F[dima][dimb];
   for (int a = 0; a < dima; a++) {
      for (int b = 0; b < dimb; b++) {
         GetHistosFile(FirstLoopSet[a], SecondLoopSet[b], FirstNoLoopSet[0], SecondNoLoopSet[0]);
         TH1F *hist = (TH1F *) gDirectory->Get("H_Alpha_UpLeft");
         hist->SetDirectory(0);
         HistosFile->Close("R");
         PhHistos_10[a][b] = hist;
      }
   }
}

that the quantities dima and dimb are known at the moment of allocating the array: is this accurate?
If this is the case you just need to allocate a an array of pointers (TH1F* PhHistos_10[dima][dimb] ) and fill it with the histograms in the files you open.

Danilo

Hallo Danilo,
actually dima and dimb are not known at the moment of allocating the array.
I run many cases. in each the dimensions are different. the array should be allocated at initializing time before knowing the dimensions.

tariq

[quote=“tariq”]Hallo Danilo,
actually dima and dimb are not known at the moment of allocating the array.
I run many cases. in each the dimensions are different. the array should be allocated at initializing time before knowing the dimensions. The array will be used later in other functions as the one where it is filled.

tariq[/quote]

rosettacode.org -> Create a two-dimensional array at runtime
cplusplus.com -> Multi-Dimensional Arrays
codeproject.com -> Introduction to dynamic two dimensional arrays in C++

I try to display 2-dim array as a histogram, but it does not work.

I have created a tree which consist of some branches. One of these branches is branch of 2dim arrays.
If I try to display it by using Draw() method or TBrowser class It gives me absolutely wrong picture.
But If I use , for instance, mytree-> Draw(“mybranch[1][3]”) it works.

What is a problem?

Hi,

That seems to be a new topic - it would have been better had you created a new one instead of adding to this one…

That said, how is the picture wrong? What do you expect mytree->Draw(“mybranch”) to do versus what you get?

Cheers, Axel.

i agree with you but I do not know how to start a new topic.

That one is easy to answer :slight_smile: Go to the relevant forum (e.g. viewforum.php?f=3) and hit the big button on top, the one labeled “New Topic” :slight_smile:

Cheers, Axel.