Variable Size allocation

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

Hi,

a good read could be this one: root.cern.ch/root/htmldoc/guide … #streamers

Cheers,
Danilo

${ROOTSYS}/tutorials/tree/tree2.C
${ROOTSYS}/tutorials/tree/tree3.C
root-forum -> Data Structure Question - Storing Data per Event
ROOT User’s Guide -> Input/Output -> Streamers -> Variable Length Array

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!!

what kind of error are you getting?

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 !!

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!!

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.

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

Cheers…