RE: [ROOT] How to initialize TH1F* aPtr[]?

From: Valeri Fine (fine@bnl.gov)
Date: Tue Feb 10 2004 - 15:56:13 MET


----
Best regards
                   Valeri


> -----Original Message-----
> From: owner-roottalk@pcroot.cern.ch
[mailto:owner-roottalk@pcroot.cern.ch] On
> Behalf Of Exaos Lee
> Sent: Tuesday, February 10, 2004 6:18 AM
> To: roottalk@pcroot.cern.ch
> Subject: [ROOT] How to initialize TH1F* aPtr[]?
> 
> Dear ROOTer,
> Please help me to check the attachments. I want to use an array of
TH1F*
> in my code. But I am confused by how to initialize it. And it is not
> good to use something like "malloc", "calloc" in a C++ code.


There are several ways to do what you want in C++. 

You can use either STL container classes or ROOT container classes.

int ERootAnalysis::InitHist(int n)
{
  int errNum = 0;
  
  pH1FArr = calloc(n,sizeof(TH1F*));
  for(int i=0;i<n;i++) {
    TString hName = "h1f_"; hName += i;
    //TString hSpec = "";
    pH1FArr[i]=new TH1F(hName.Data(),"",1000,0,1000);
    if(!pH1FArr[i]) errNum+=-1;
  }

  For example you may define your 

      stl::vector<TH1F *> pH1Farr;

  and then use it in your code:

int ERootAnalysis::InitHist(int n)
{  
  for(int i=0;i<n;i++) {
    TString hName = "h1f_"; hName += i;
    pH1Farr.push_back(new TH1F(hName.Data(),"",1000,0,1000));
  }


I would like to mention your
    if(!pH1FArr[i]) errNum+=-1;

is useless because the operator "new" ALWAYS return non-zero pointer.

> 
> Regards.
> 
> Exaos



This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:05 MET