writing/reading arrays to/from a file

From: chiara zampolli <Chiara.Zampolli_at_bo.infn.it>
Date: Sat, 26 Nov 2005 15:59:43 +0000


Dear rooters,

    I have a problem similar to the one I had some days ago. As before, I am trying to write/read an array of objects to/from a file, but in this case the array is not the data member of a class (so that when writing the object of the class, I write the array), but it is exactly what I have to write. Here you find some code lines which explain what I would like to do (see also the attached testchsim.C):

void testchsim()
{

  AliTOFChSim *ch;
  ch = new AliTOFChSim[1000];

  ch[0].SetMaxToT(3);
  ch[0].SetMinToT(4);
  ch[4].SetMaxToT(2);
  ch[4].SetMaxToT(5);

  Float_t max = ch[0].GetMaxToT();
  Float_t min = ch[0].GetMinToT();
  cout << " max 0 = " << max << endl;
  cout << " min 0 = " << min << endl;
  max = ch[4].GetMaxToT();
  min = ch[4].GetMinToT();
  cout << " max 4 = " << max << endl;
  cout << " min 4 = " << min << endl;
  TFile *file = new TFile("ciccio.root","recreate");   ch->Write("ciccio",TObject::kSingleKey);   file->Close();
  delete file;
  TFile *file = new TFile("ciccio.root","read");   AliTOFChSim *ch1; //[1000]
  file->GetObject("ciccio",ch1);
  Float_t max = ch1[0].GetMaxToT();
  Float_t min = ch1[0].GetMinToT();
  cout << " max 0 = " << max << endl;
  cout << " min 0 = " << min << endl;
  max = ch1[4].GetMaxToT();
  min = ch1[4].GetMinToT();
  cout << " max 4 = " << max << endl;
  cout << " min 4 = " << min << endl;
}

where AliTOFChSim inherits from TObject (it is a class I have defined, where the MaxToT and MinToT are two data members - you can find it in the attached files). As you can see from the output of the macro:

root [15] .x testchsim.C
 ch[1] = 0x9f9afe8

 max 0  = 3
 min 0  = 4
 max 4  = 5
 min 4  = 0
 max 0  = 3
 min 0  = 4
 max 4  = -0.0618096
 min 4  = 1.1992e-11
 max 4  = -0.0618096
 min 4  = 1.1992e-11

root [16] .q

when trying to read back my array, its elements seem to be empty (apart from the first one, element zero). What am I doing wrong?

Thank you for your help.
Cheers,

   Chiara

/**************************************************************************

* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
* *
* Author: The ALICE Off-line Project. *
* Contributors are mentioned in the code where appropriate. *
* *
* Permission to use, copy, modify and distribute this software and its *
* documentation strictly for non-commercial purposes is hereby granted *
* without fee, provided that the above copyright notice appears in all *
* copies and that both the copyright notice and this permission notice *
* appear in the supporting documentation. The authors make no claims *
* about the suitability of this software for any purpose. It is *
* provided "as is" without express or implied warranty. *
**************************************************************************/

/* $Id: AliTOFChSim.cxx,v 1.5 2005/04/11 07:13:27 cz Exp $ */

///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// class for TOF calibration                                                 //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

#include "AliTOFChSim.h"

#include "TF1.h"
#include "TH1F.h"

ClassImp(AliTOFChSim)

//________________________________________________________________
AliTOFChSim::AliTOFChSim():
fSlewedStatus(kFALSE),
fmaxToT(0),
fminToT(0),
fmaximum(0),
fmpv(0),
fsigma(0)
{}
//________________________________________________________________
AliTOFChSim::AliTOFChSim(const AliTOFChSim& channel) :   TObject(channel)
{
// copy constructor
  fSlewedStatus = channel.IsSlewed();
  fmaxToT= channel.GetMaxToT();
  fminToT=channel.GetMinToT();
  fmaximum=channel.GetMaximum();
  fmpv=channel.GetMPV();
  fsigma=channel.GetSigma();
}
//________________________________________________________________
AliTOFChSim &AliTOFChSim::operator =(const AliTOFChSim& channel) {
// assignment operator
  fSlewedStatus = channel.IsSlewed();
  fmaxToT= channel.GetMaxToT();
  fminToT=channel.GetMinToT();
  fmaximum=channel.GetMaximum();
  fmpv=channel.GetMPV();
  fsigma=channel.GetSigma();
  return *this;
}
//____________________________________________________________________________

void AliTOFChSim::SimulateToT(Option_t *option){   Float_t tsize = 0.06;
  Float_t lsize = 0.05;
  //Float_t texsize = 0.07;

  Float_t toff = 1.2; 
  Float_t minch = 13.5;
  Float_t maxch = 15.5;

  Int_t nbins = 20;
  Float_t minToT= 1345.;
  Float_t maxToT= 15.55;
  Double_t sigmaL = 0.1;

  Double_t mpvL = 0.;
  Float_t Delta = (maxch - minch)/nbins;   const char * name = "histoname";
  const char * title = "histotitle";

  TH1F *hcharge = new TH1F ("hcharge", "charge spectrum" , nbins, minToT, maxToT);   //generation of a ToT distr according to TB   TH1F *hchargenorm;
  Float_t charge;
  mpvL = 14.9-0.9;
  for (Int_t i=0; i<nbins; i++){
    Double_t x = minch + i*Delta;
    charge = Float_t(TMath::Landau(x,mpvL,sigmaL));     hcharge->Fill(29-(x+Delta*0.5), charge);   }
  Double_t maximum = hcharge->GetMaximum();   Int_t nentries = (Int_t)hcharge->GetEntries();   //cout << " entries = " << nentries << endl;   //cout << " maximum = " << maximum << endl;

  Float_t max=0,min=0; //maximum and minimum value of the distribution   Int_t maxbin=0,minbin=0; //maximum and minimum bin of the distribution   for (Int_t i=nbins; i>0; i--){
    if (hcharge->GetBinContent(i)!= 0) {

      max = maxch - (nbins-i-1)*Delta;
      maxbin = i; 
      break;}

  }
  for (Int_t j=1; j<nbins; j++){
    if (hcharge->GetBinContent(j)!= 0) {
      min = minch + (j-1)*Delta;
      minbin = j; 
      break;}

  }
  AliTOFChSim::SetMaxToT(max);
  AliTOFChSim::SetMinToT(min);
  hchargenorm=new TH1F(*hcharge);

  //normalization of the ToT distribution from TB   Float_t scaleFact = hchargenorm->Integral(1,nbins);   hchargenorm->Scale(1./scaleFact);

  //reverse the distribution in order to fit it

  //when looking for the landau fit of the distr, since the distribution is   //reversed wrt a usual Landau, the histo has first to be reversed...   name = "hchargeRev";
  title = "Reversed charge spectrum";
  TH1F * hchargeRev = (TH1F*) ReverseHisto(hchargenorm, name, title);

  //...and then Fit

  hchargeRev->Fit("landau", "Q" ," ", min, max);   TF1 *func = hchargeRev->GetFunction("landau");   Float_t mpv = func->GetParameter(1);
  Float_t sigma = func->GetParameter(2);   maximum = hchargeRev->GetMaximum();

  AliTOFChSim::SetMaximum(maximum);
  AliTOFChSim::SetMPV(mpv);
  AliTOFChSim::SetSigma(sigma);

  //end of the simulation of the ToT distribution (with its own normalization)

 //random seed generation for the simulation of a ToT distribution; 
  //current time used
  //  TDatime *time= new TDatime();
  //Int_t seed = time->GetTime();

  TH1F *hchargeSim = new TH1F ("hchargeSim", "Simulated charge spectrum" , nbins, minch-Delta*0.5, maxch+Delta*0.5);

  Int_t nrandom = 1000;
  for (Int_t i = 0;i<nrandom; i++){

    Float_t trix = 0;
    Float_t triy = 0;
    Float_t land = 0;

    Int_t iter = 0;
    while (land <= triy){
      iter++;
      trix = gRandom->Rndm(i);
      triy = gRandom->Rndm(i);
      trix = (max-min)*trix + min; 
      triy = maximum*triy;
      land = TMath::Landau(trix, mpv, sigma);
    }
    hchargeSim->Fill(trix,land);
  }

  //now the simulated ToT distribution is reversed in order to   //reproduced the original one
  name = "hchargeSimRev";
  title = "Reversed Simulated charge spectrum";   TH1F * hchargeSimRev = (TH1F*) ReverseHisto(hchargeSim, name, title);   //renormalization
  scaleFact = hchargeSimRev->Integral(1,nbins);   hchargeSimRev->Scale(1./scaleFact);

  //  TFile *file = new TFile("ToT.root","recreate");
  //hchargeSimRev->Write();
  //file->Close();
  /*

  if(strstr(option,"print")){     

    cout << "gStyle 0 = " << gStyle << endl;     gStyle->SetOptStat(0);
    cout << "gStyle 1 = " << gStyle << endl;     gStyle->SetOptTitle(0);
    cout << "gStyle 2 = " << gStyle << endl;     gStyle->SetPalette(1);
    cout << "gStyle 3 = " << gStyle << endl;     gStyle->SetCanvasColor(0);
    cout << " sono qui 4" << endl;     

    TCanvas *c = new TCanvas("c", "c",-2,30,500,500);

    c->SetBorderMode(0);
    c->SetBottomMargin(0.15);
    c->SetLeftMargin(0.17);
    c->SetRightMargin(0.03);
    c->cd();
    hchargeSimRev->SetLineWidth(3);
    hchargeSimRev->GetXaxis()->SetTitleSize(tsize);
    hchargeSimRev->GetXaxis()->SetTitle("Time Over Threshold (ns)");
    hchargeSimRev->GetXaxis()->SetLabelSize(lsize);
    hchargeSimRev->GetYaxis()->SetTitleSize(tsize);
    hchargeSimRev->GetYaxis()->SetTitleOffset(toff);
    hchargeSimRev->GetYaxis()->SetLabelSize(lsize);     hchargeSimRev->Draw();
  }
  */
  //cout << " Simulate ToT ok 0 " << endl;   delete hcharge;
  //cout << " Simulate ToT ok 1 " << endl;   delete hchargenorm;
  //cout << " Simulate ToT ok 2 " << endl;   delete hchargeSim;
  //cout << " Simulate ToT ok 3 " << endl;   delete hchargeSimRev;
  //cout << " Simulate ToT ok " << endl; }
//____________________________________________________________________________

TH1F* AliTOFChSim::ReverseHisto (TH1F* histo, const char * name, const char * title) {

  //the histo is supposed to be with bins of equal widths   Int_t nbins = histo->GetNbinsX();
  // cout << " nbins = " << nbins << endl;   Float_t max=0, min=0;
  //cout << " histo->GetXaxis()->SetBinUpEdge(nbins) = " << histo->GetXaxis()->GetBinUpEdge(nbins) << endl;

  Axis_t maxch = histo->GetXaxis()->GetBinUpEdge(nbins);
  Axis_t minch = histo->GetXaxis()->GetBinLowEdge(1);
  Axis_t Delta = histo->GetXaxis()->GetBinWidth(1);
  TH1F * hRev = new TH1F (name, title, nbins, minch, maxch);
  for (Int_t i=nbins; i>0; i--){
    if (histo->GetBinContent(i)!= 0) {
      max = maxch - (nbins-i-1)*Delta;
      break;}

  }

  for (Int_t j=1; j<nbins; j++){
    if (histo->GetBinContent(j)!= 0) {

      min = minch + (j-1)*Delta;
      break;}

  }
  Float_t dist = (max + min)*0.5;
  for (Int_t i=0; i<nbins; i++){
    Double_t y = histo->GetBinContent(i); 
    Double_t x = min + i*Delta;

    //cout << " x = " << x << endl;
    hRev->Fill(dist*2-(x+Delta*0.5), y);   }
  return hRev;
}
//_____________________________________________________________________________




#ifndef ALITOFCHSIM_H
#define ALITOFCHSIM_H

/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *

////////////////////////////////////////////////
//  class for TOF calibration                 //
////////////////////////////////////////////////

#include "TNamed.h"

#include "TH1F.h"
#include "AliTOF.h"

class AliTOFChSim: public TObject {

public:
  AliTOFChSim();
  AliTOFChSim(const AliTOFChSim &chan);
  AliTOFChSim& operator= (const AliTOFChSim &chan);   virtual ~AliTOFChSim(){};
  //
  Bool_t IsSlewed() const {return fSlewedStatus;}   void SetSlewedStatus(Bool_t status) { fSlewedStatus = status;}   void SetMaxToT(Float_t max){fmaxToT=max;}   void SetMinToT(Float_t min){fminToT=min;}   void SetMaximum(Float_t max){fmaximum=max;}   void SetMPV(Float_t mpv){fmpv=mpv;}   void SetSigma(Float_t sigma){fsigma=sigma;}

  Float_t GetMaxToT() const {return fmaxToT;}
  Float_t GetMinToT() const{return fminToT;}
  Float_t GetMaximum() const {return fmaximum;}
  Float_t GetMPV() const {return fmpv;}
  Float_t GetSigma() const {return fsigma;}
  void SimulateToT(Option_t* option);   TH1F* ReverseHisto(TH1F* histo, const char * name, const char * title);

private:
  Bool_t fSlewedStatus;
  //members for simulation of Time Over Threshold

  Float_t  fmaxToT;
  Float_t  fminToT;
  Float_t  fmaximum;
  Float_t  fmpv;
  Float_t  fsigma;

  //
  ClassDef(AliTOFChSim,1) // TOF Sensor Calibration object };

#endif

void testchsim()
{

  AliTOFChSim *ch;
  ch = new AliTOFChSim[1000];
  cout << " ch[1] = " << &ch[1] << endl;   for (Int_t i = 0 ; i<1000; i++){
    ch[i] = new AliTOFChSim();
  }

  ch[0].SetMaxToT(3);
  ch[0].SetMinToT(4);
  ch[4].SetMaxToT(2);
  ch[4].SetMaxToT(5);

  Float_t max = ch[0].GetMaxToT();
  Float_t min = ch[0].GetMinToT();
  cout << " max 0 = " << max << endl;
  cout << " min 0 = " << min << endl;
  max = ch[4].GetMaxToT();
  min = ch[4].GetMinToT();
  cout << " max 4 = " << max << endl;
  cout << " min 4 = " << min << endl;
  TFile *file = new TFile("ciccio.root","recreate");   ch->Write("ciccio",TObject::kSingleKey);   file->Close();
  delete file;
  TFile *file = new TFile("ciccio.root","read");   AliTOFChSim *ch1; //[1000]
  file->GetObject("ciccio",ch1);
  Float_t max = ch1[0].GetMaxToT();
  Float_t min = ch1[0].GetMinToT();
  AliTOFChSim ch2 = ch1[4];
  cout << " max 0 = " << max << endl;
  cout << " min 0 = " << min << endl;
  max = ch1[4].GetMaxToT();
  min = ch1[4].GetMinToT();
  cout << " max 4 = " << max << endl;
  cout << " min 4 = " << min << endl;
  max = ch2.GetMaxToT();
  min = ch2.GetMinToT();
  cout << " max 4 = " << max << endl;
  cout << " min 4 = " << min << endl;
} Received on Sat Nov 26 2005 - 15:57:20 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:13 MET