# SetBranchAddress data not loaded

**URL:** <https://root-forum.cern.ch/t/setbranchaddress-data-not-loaded/42535>\
**Category:** Newbie\
**Created:** [November 30, 2020, 9:45pm UTC](https://root-forum.cern.ch/t/setbranchaddress-data-not-loaded/42535 "2020-11-30T21:45:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chikin\_tam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/chikin_tam/32/22815_2.png) [@chikin\_tam](https://root-forum.cern.ch/u/chikin_tam)\
**Post date:** [November 30, 2020, 9:45pm UTC](https://root-forum.cern.ch/t/setbranchaddress-data-not-loaded/42535/1 "2020-11-30T21:45:37Z")

</div>

I have been trying to read and analyze the data from a tree by writing a class. I initialized the variables and array in the constructor and set the branch address in a function of the class. There is no error message compling the files and running the analysis but the data does not pass to these array and variables (in fact when I print them out they show 0). Here is the short version of my script.

```auto
Reader::Reader(const char* dirName, const char* fileName, int maxFiles):mMaxFiles(maxFiles), mDebug(0), mCurrentFile(0),mTTree(0)

{
          multi=0;
          Theta[20]={0};
          KinEnergy[20]={0};
          Phi[20]={0};
}

int Reader::Read()
{
  Chain = new Chain("E12345","Event");
  int nFiles=0;

  //call a function that fills the chain with root files listed in a text file
  if (file!="") { // if a filename was given
    if( strstr(file.c_str(),".lis") || strstr(file.c_str(),".list") ) { // if a file list is specified
      try {
	nFiles = fillChain(mTChain, (dir+file).c_str(), mMaxFiles);
      }
      catch(StException e) {
	throw e;
      }
    }
    else { // a single file was specified
      mTChain->Add((dir+file).c_str());
      nFiles++;
    }
  Chain->SetBranchAddress("event.multi",&multi);

  Chain->SetBranchAddress("event.Theta",Theta);
  Chain->SetBranchAddress("event.Phi",Phi);
  Chain->SetBranchAddress("event.KinEnergy",KinEnergy);

  return nFiles;
}

```

However, the branch address are set if I simply use a macro like

```auto
double KinEnergy[20];
double Phi[20];
...
...
...
Chain->SetBranchAddress(event.KinEnergy,KinEnergy);
...etc

```

I dont understand what the problem is as the logic seems ok. So I am hoping someone can tell me the correct approach to set branch address in a class. Thank you very much.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 30, 2020, 10:35pm UTC](https://root-forum.cern.ch/t/setbranchaddress-data-not-loaded/42535/2 "2020-11-30T22:35:50Z")

</div>

What is the “type” of the branch? (i.e. check Chain-\>Print()) Are the variable type the same in both your example?

---

<div class="post-metadata">

**Author:** ![chikin\_tam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/chikin_tam/32/22815_2.png) [@chikin\_tam](https://root-forum.cern.ch/u/chikin_tam)\
**Post date:** [December 1, 2020, 1:00am UTC](https://root-forum.cern.ch/t/setbranchaddress-data-not-loaded/42535/3 "2020-12-01T01:00:55Z")

</div>

Yes. I have checked the type of the data which are Int\_t and Double\_t for multi and KinEnergy respectively. I have defined Int\_t multi and Double\_t KinEnergy…etc in the header file. Just curious, does it allow me to define int even though the data type of the branch is Int\_t?

---

<div class="post-metadata">

**Author:** ![chikin\_tam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/chikin_tam/32/22815_2.png) [@chikin\_tam](https://root-forum.cern.ch/u/chikin_tam)\
**Post date:** [December 1, 2020, 5:53am UTC](https://root-forum.cern.ch/t/setbranchaddress-data-not-loaded/42535/4 "2020-12-01T05:53:06Z")

</div>

I found that the data type of some of the branches are Double\_t[7] but I have Double\_t[20]…I changed them and also setmakeclass(1) as the branches are subbranches… And now the data is loaded. Thanks!
