Write an array of histograms to root file

Hi all,

I want to store an array TH2F hist[N] to a root file.
I tried with single one (TH2F hist), it worked.
But when I tried with TH2F hist[N], it was not successful.

Can anyone help?

you should loop on them… can you show what you are doing ? some C++ code …

Dear couet,

Detail of my code is below.
Let me summarize it:

  • I try to separate events of root file to histograms named hPedEvent (TH2F *hPedEvent[160])
  • start&end events are listed in a table
  • when I tried hPedEvent->Write(), there was error message:
**Error: non class, strcut, union object hPedEvent used with . or -> ......**
  • when I tried to make a loop, for(;; ) hPedEvent[iBin]->Write(); it can run, but I cannot call the histogram or draw it when opening root file.
**root [1] .ls**
**TFile**         etchisto010-036_Extracted.root**
** TFile*         etchisto010-036_Extracted.root**
**  KEY: TH2F     hPedEvent[0];1**
**root [2] hPedEvent[0]->Draw()**
**input_line_43:2:3: error: use of undeclared identifier 'hPedEvent'**
** (hPedEvent[0]->Draw())**
**  ^**

Actually, what I expect is I can output the whole array of histograms to root file. Say, hPedEvent[160], not hPedEvent0, hPedEvent1, …
Because I have a lot of histograms to analyze, more than 1000.

Thanks a lot !

 //##Initialize histograms
  TH2F *hPedEvent[nCycle*nDAC];
  nEntries = tree->GetEntries();
  for(iBin=0; iBin<nBin; iBin++){
    hPedEvent[iBin] = new TH2F(Form("hPedEvent[%d]",iBin),"",nEntries,0,nEntries,30,243,246);//1event/bin vs 0.1ADC/bin
    //std::cout << "initialized hPedEvent[" << iCycle << "][" << iDac << "]" << std::endl; 
  }
  
  //##Extract Data to histograms
  tree->SetBranchAddress("Pedestal",Pedestal);
  tree->SetBranchAddress("EventNumber",&EventNumber);
  for(iBin=0; iBin<nBin; iBin++){
    startEvent = StartEvent[iBin];
    if(iBin<nCycle*nDAC-1) endEvent = StartEvent[iBin+1];
    else endEvent = nEntries;
    if(startEvent>endEvent){
      std::cout << "found startevent>endevent @" << iBin << std::endl;
      //exit(-1);
    }
    std::cout << "separating events from " << startEvent << " to " << endEvent << std::endl;
    for(iEvent=startEvent;iEvent<endEvent;iEvent++){
      tree->GetEntry(iEvent);
      //std::cout << EventNumber << " " << Pedestal[iPMT] << std::endl;
      hPedEvent[iBin]->Fill(EventNumber,Pedestal[iPMT]);
    }
    std::cout << "hPedEvent[" << iBin << "]: " << hPedEvent[iBin]->GetEntries() << "events" << std::endl;    
  }

  TFile *outputFile;
  outputFile = new TFile(outFileName,"RECREATE");
  //hPedEvent->Write(); //***** I tried this one but error
  for(iBin=0; iBin<nBin; iBin++){
    hPedEvent[iBin]->Write(); //***** I tried this one, the software can run, but I cannot call the histogram when opening root file
    std::cout << "stored hPedEvent[" << iBin << "] in " << outFileName << std::endl;
  }
  outputFile->Close();

I would say that you should put your histograms into a TLIst and then write the list in the file using the option TObject::kSingleKey as described here

Dear couet,

could you please give me an example ?

Thank you very much.

void writehistlist()
{
   TList *l = new TList();
   TH1F *h1 = new TH1F("h1","h1",100.,0.,1.);
   TH1F *h2 = new TH1F("h2","h2",100.,0.,1.);
   TH1F *h3 = new TH1F("h3","h3",100.,0.,1.);
   l->Add(h1);
   l->Add(h2);
   l->Add(h3);
   TFile *f = new TFile("histlist.root","RECREATE");
   l->Write("histlist", TObject::kSingleKey);
   f->ls();
}
1 Like

Dear couet,

Thanks for the comment.
uhm … my problem now is somehow because of the memory leak/limitation.
I want to fill data -> write() to root file -> reset the content of histogram.

Is it possible to do something like:

hist = new TH1F(…)
for (iEvent=…; iEvent<…; iEvent++) hist->Fill()
hist->Write()
hist->Reset()

Thanks !

if you do that you are back to te normal write … it does not use TLIst

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.