Drawing waveform from TArray

I have a desktop digitizer system whose software (CoMPASS) produces ROOT files containing a TTree with a TArrayS branch for the waveform. I would like to draw and perform basic operations (e.g. integrals, smoothing) on these waveforms which, as far as I can tell, is best done using a histogram (TH1S).

So far I am filling the histogram using a loop over all elements of the TArray, code snippet below.

TArrayS *samples = new TArrayS();
Data_R->SetBranchAddress("Samples", &samples);
Int_t nsamples = samples->GetSize();  // all waveforms in file have the same length
TH1S *waveform = new TH1S("waveform", "waveform", nsamples, 0, nsamples);
for (int i==0; i<nsamples; i++){
   waveform->SetBinContent(i, samples->GetAt(i));

My questions are:

  1. Can I visualize the waveforms directly from the TArray using a TTree->Draw() command?
  2. Is there a more straightforward or efficient way of filling the histograms from the TArrays?
  3. In general, does it make sense to store waveforms in a TArray instead of e.g. a TH1? I could see that for an acquisition software performance is more important than making the subsequent analysis easier.

A very similar question was posed previously (see TTree::Draw with TArrayS) but the workaround mentioned was never provided.

I’ve attached a short run file in case it is useful.

DataR_run_28.root (33.8 KB)



Hi, I’m sorry I don’t remember what the workaround was - and apparently it was temporary enough that I can’t find the code when searching through my files.

Your loop to fill the histogram looks fine to me for visualization, but I would suggest using a TGraph unless your data is actually counts in bins. The TGraph is a better fit for waveforms in my opinion.

Hi @AHoward ,
I think we need @couet 's help regarding the plotting (question 1.).

I think a loop with SetBinContent is an appropriate way of converting the array into a TH1.

I think it makes sense to store waveforms as arrays, you could even use a std::vector instead of a TArray (@pcanal can correct me if I’m wrong, but I think it should be just as fast or faster) unless you need some specific features from TArray.


TArray does not have any Draw() method.To visualise it I guess you will need to make a TGraph from it.

Thanks for the input everyone, I’ll look into using a TGraph instead of TH1.



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