# Variable Size allocation

**URL:** <https://root-forum.cern.ch/t/variable-size-allocation/20471>\
**Category:** ROOT\
**Created:** [January 21, 2016, 7:03am UTC](https://root-forum.cern.ch/t/variable-size-allocation/20471 "2016-01-21T07:03:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Debangana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/47e85d/32.png) [@Debangana](https://root-forum.cern.ch/u/Debangana)\
**Post date:** [January 21, 2016, 7:03am UTC](https://root-forum.cern.ch/t/variable-size-allocation/20471/1 "2016-01-21T07:03:27Z")

</div>

Hi everyone,

I have a class derived from TObject.One of my data members of the class is variable in its size based on some parameter.How do I implement it?

class event\_header : public TObject{  
public:  
unsigned short hit\_pattern; ----\> size determinig parameter  
unsigned short data\_event\_no;  
public:  
ClassDef(event\_header,1);  
};  
class charge\_profile : public TObject{  
public:  
short profile\_data\_hg[31]; -----\>size will vary based on hitpattern  
short charge\_data\_hg;  
short profile\_data\_lg[31]; -----\>size will vary based on hitpattern  
short charge\_data\_lg;  
public :  
ClassDef(charge\_profile,1);  
};

I am unable to declare in this way::::error message no pointer to vtable

class charge\_profile : public TObject{  
public:  
short \*profile\_data\_hg;  
short charge\_data\_hg;  
short \*profile\_data\_lg;  
short charge\_data\_lg;  
public :  
ClassDef(charge\_profile,1);  
};

Somebody please guide

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [January 21, 2016, 9:51am UTC](https://root-forum.cern.ch/t/variable-size-allocation/20471/2 "2016-01-21T09:51:23Z")

</div>

Hi,

a good read could be this one: [root.cern.ch/root/htmldoc/guide … #streamers](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#streamers)

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [January 21, 2016, 1:07pm UTC](https://root-forum.cern.ch/t/variable-size-allocation/20471/3 "2016-01-21T13:07:42Z")

</div>

[${ROOTSYS}/tutorials/tree/tree2.C](http://root.cern.ch/root/html534/tutorials/tree/tree2.C.html)  
[${ROOTSYS}/tutorials/tree/tree3.C](http://root.cern.ch/root/html534/tutorials/tree/tree3.C.html)  
[root-forum -\> Data Structure Question - Storing Data per Event](https://root-forum.cern.ch/t/data-structure-question-storing-data-per-event/18376)  
[ROOT User’s Guide -\> Input/Output -\> Streamers -\> Variable Length Array](https://root.cern.ch/root/html534/guides/users-guide/InputOutput.html#variable-length-array)

---

<div class="post-metadata">

**Author:** ![Debangana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/47e85d/32.png) [@Debangana](https://root-forum.cern.ch/u/Debangana)\
**Post date:** [May 30, 2016, 12:18pm UTC](https://root-forum.cern.ch/t/variable-size-allocation/20471/4 "2016-05-30T12:18:53Z")

</div>

I am able to store variable size field. My class looks like:

class Channel: public TObject  
{  
int value;  
int length;  
short \*profile; //[length]  
}

class Event:public TObject  
{  
int eventNo;  
Channel ch[8];  
}

I am doing a new when length is not zero and allocating space for profile and assigning it else making profile=0. But while reading I am getting StreamerInfo error during GetEntry call… Please Someone Help!!

---

<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:** [May 30, 2016, 7:39pm UTC](https://root-forum.cern.ch/t/variable-size-allocation/20471/5 "2016-05-30T19:39:16Z")

</div>

what kind of error are you getting?

---

<div class="post-metadata">

**Author:** ![Debangana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/47e85d/32.png) [@Debangana](https://root-forum.cern.ch/u/Debangana)\
**Post date:** [June 3, 2016, 12:34pm UTC](https://root-forum.cern.ch/t/variable-size-allocation/20471/6 "2016-06-03T12:34:04Z")

</div>

I was getting Streamer error… I made a very silly mistake… I didn’t use ROOT Data types I used C++ data types… that is just int X and not Int\_t X !!

---

<div class="post-metadata">

**Author:** ![Debangana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/47e85d/32.png) [@Debangana](https://root-forum.cern.ch/u/Debangana)\
**Post date:** [June 7, 2016, 4:14am UTC](https://root-forum.cern.ch/t/variable-size-allocation/20471/7 "2016-06-07T04:14:48Z")

</div>

Hi everyone…  
I am facing this problem for a long time.I have a class derived from TObject class in which I have a pointer variable.I am storing them and while reading I am unable to read the values which I am storing via a pointer variable…I am getting garbage values stored.  
Can someone give me some program example of storing values via pointer variable and retrieving them  
My classes are:

[code]class ChannelInformation : public TObject{  
public:  
Int\_t ChannelNo;  
Bool\_t HitInfo;  
Short\_t LGainCharge;  
Short\_t HGainCharge;  
Int\_t channelLength;  
Short\_t \*LGainProfile; //[channelLength]  
Short\_t \*HGainProfile; //[channelLength]  
};

class L2event : public TObject  
{  
public:  
Int\_t EventNo;  
Double\_t time;  
Int\_t eventType;  
Bool\_t HitArray[1088];  
ChannelInformation channelMap[1088];  
};  
[/code]  
My code of writing root file:

[code] FILE \*fp =fopen(“LOSimulated2\_6.6.2016.bin”,“r+b”);  
TStopwatch timer;  
TFile rootFile(“Simulated\_uc.root”,“recreate”,“ROOT Writing”,-1);  
TTree tree(“t2”,“a Tree with mace data”);  
L2event \*event = new L2event;  
tree.Branch(“L2event”,&event,8000,1);  
if(fp == NULL)  
{  
printf(“unable to open file handler”);  
}  
else  
{  
int eventcount=0;

```
  long eventSize;

  timer.Start();

  while(!feof(fp) && eventcount <EVENTCOUNT)

  {
     event->_ReadFromFile(fp);

     tree.Fill();

     ++eventcount;

  }

  tree.Write("",TObject::kOverwrite);

```

}

timer.Stop();

printf(“ROOT File with CX=CX= %g, real time:%7.3f s, Cpu=%7.3f s, File size= %9d bytes \n”,rootFile.GetCompressionFactor(),timer.RealTime(),timer.CpuTime(),  
(Int\_t)rootFile.GetBytesWritten());  
[/code]  
Reading method:: Here except the profile values rest of the data is ok.But for profile data it is giving garbage values.

[code]  
void ReadEvent(Long64\_t eventNo)  
{  
TStopwatch timer;  
timer.Start();

```
  TFile *f = new TFile("Simulated_uc.root");
  TTree *t2 = (TTree*)f->Get("t2");

  L2event *event = 0;
  t2->SetBranchAddress("L2event",&event);
  Long64_t nentries = t2->GetEntries();

  FILE *NewfpText=fopen("SpecificEvent.txt","w");

  for (Long64_t i=0;i<nentries;i++)
  {
     if(i==eventNo)
     {
        t2->GetEntry(i);

        fprintf(NewfpText,"EventNumber %d\t\t",event->EventNo);
        fprintf(NewfpText,"Time %f\t\t",event->time);
        fprintf(NewfpText,"Data Type %d\t\t",event->eventType);

        int TotalHit=0;
        for(int i=0;i<1088;i++)
        {
           //printf("channel: %d, hitvalue: %d\n",i,Event->Hitarray[i]);
           if(event->channelMap[i].HitInfo==true)
              TotalHit++;
        }
        //printf("Total Hit: %d",TotalHit);

        float hitpercent=((float)TotalHit/(float)1088)*((float)(100));

        //printf("Hit percentage for Event %d: %f",Event->EventNumber,hitpercent);
        fprintf(NewfpText,"\tHit Percentage\t %f",hitpercent);
        fprintf(NewfpText,"\n\n\tHitArray\n\n");

        for(int i=0;i<1088;i++)
        {
           fprintf(NewfpText,"%d\t",event->HitArray[i]);
        }

        fprintf(NewfpText,"\n\n\tChannel Information\n\n");

        for(int i=0;i<1088;i++)
        {
           fprintf(NewfpText,"Channel Number\t\t\t\t%d\n",event->channelMap[i].ChannelNo);
           fprintf(NewfpText,"Channel Hit Value:\t\t\t%d\n",event->channelMap[i].HitInfo);
           fprintf(NewfpText,"Channel High Charge:\t\t\t%d\n",event->channelMap[i].HGainCharge);

           if(event->channelMap[i].HitInfo ==true)
           {
              fprintf(NewfpText,"Channel High Profile Value\n");

              event->channelMap[i].HGainProfile = new Short_t[31];
              event->channelMap[i].LGainProfile = new Short_t[31];
              for(int j=0;j<31;j++)
              {
                 fprintf(NewfpText,"%d\t",*(event->channelMap[i].HGainProfile + j));
              }
              fprintf(NewfpText,"\n");
           }
           fprintf(NewfpText,"Channel Low Charge:\t\t\t %d\n",event->channelMap[i].LGainCharge);
          
           if(event->channelMap[i].HitInfo ==true)
           {
              fprintf(NewfpText,"Channel Low Profile Value\n");
              
              for(int j=0;j<31;j++)
              {
                 fprintf(NewfpText,"%d\t",*(event->channelMap[i].HGainProfile + j));
              }
              fprintf(NewfpText,"\n");
           }
           fprintf(NewfpText,"\n\n");
        }
     }
     
  }[/code]

```

Please someone help!!

---

<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:** [June 7, 2016, 3:51pm UTC](https://root-forum.cern.ch/t/variable-size-allocation/20471/8 "2016-06-07T15:51:02Z")

</div>

Hi,

Why is the reading code replacing the read-in information by new allocation:`
   event->channelMap[i].HGainProfile = new Short_t[31];
   event->channelMap[i].LGainProfile = new Short_t[31];
`?

Philippe.

---

<div class="post-metadata">

**Author:** ![Debangana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/47e85d/32.png) [@Debangana](https://root-forum.cern.ch/u/Debangana)\
**Post date:** [June 9, 2016, 10:29am UTC](https://root-forum.cern.ch/t/variable-size-allocation/20471/9 "2016-06-09T10:29:01Z")

</div>

I made a mistake there… It is working fine…Thank you 🙂

Cheers…
