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