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);
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
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"
ClassImp(AliTOFChSim)
//________________________________________________________________AliTOFChSim::AliTOFChSim():
//________________________________________________________________AliTOFChSim::AliTOFChSim(const AliTOFChSim& channel) : TObject(channel)
//________________________________________________________________AliTOFChSim &AliTOFChSim::operator =(const AliTOFChSim& channel) {
//____________________________________________________________________________
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;
Float_t minToT= 1345.; Float_t maxToT= 15.55; Double_t sigmaL = 0.1;
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;}
min = minch + (j-1)*Delta; minbin = j; break;}
//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();
Int_t nrandom = 1000;
for (Int_t i = 0;i<nrandom; i++){
Float_t trix = 0; Float_t triy = 0; Float_t land = 0;
iter++; trix = gRandom->Rndm(i); triy = gRandom->Rndm(i); trix = (max-min)*trix + min; triy = maximum*triy; land = TMath::Landau(trix, mpv, sigma);}
//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(); /*
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();
//____________________________________________________________________________
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;}
for (Int_t i=0; i<nbins; i++){ Double_t y = histo->GetBinContent(i); Double_t x = min + i*Delta;
//_____________________________________________________________________________
#ifndef ALITOFCHSIM_H
#define ALITOFCHSIM_H
/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
//////////////////////////////////////////////// // class for TOF calibration // //////////////////////////////////////////////// #include "TNamed.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;
#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);
This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:13 MET