Extracting data from Tree, performing analysis

Greetings,
I am having a difficult time trying to do analysis in ROOT. My data is a CSV file of x and y pairs with a one line header, x column is energy and the y is counts. I have imported my data into a TTree using ReadFile as I show below.

I would like to be able to create a new file or output for future analysis based on the energy. For example, I would like to collect/sum the y (counts) values around 25 keV (x axis) that fall within 5 keV. The data is shown below. This collected data saved in an output file.

Any help would be greatly appreciated.

Regards,
Richard

Import data from CSV file

void readfile_ex() {
  
  TFile *f = new TFile("basic2.root","RECREATE");
  TH1F *h1 = new TH1F("h1","x distribution",100,-4,4);
  TTree *T = new TTree("ntuple","data from ascii file");
  Long64_t nlines = T->ReadFile(Form("filename_0001.CSV"),"x:y");
  printf(" found %lld points\n",nlines);
  T->Draw("x","y");
  T->Write();
}

Example data

Energy, Data  
1.464844, 12
4.394531, 4
7.324219, 8
10.253906, 54
13.183594, 52
16.113281, 72
19.042969, 107
21.972656, 110
24.902344, 133
27.832031, 164
30.761719, 297
33.691406, 829
36.621094, 1321
39.550781, 922
42.480469, 463
45.410156, 243
48.339844, 208
51.269531, 214
54.199219, 230
57.128906, 227
60.058594, 260
62.988281, 295

I have a second method of importing the data.

#include "Riostream.h"
#include <iostream>

void Spectrum_data_extractor(){
  ifstream in;
  in.open(Form("./filename(0001).CSV"));
  TString header1, header2, header3, blank;
  in>>header1>>header2;
  
  cout<<header1<<endl;
  cout<<header2<<endl;
  
  Double_t x;
  Int_t y;
  Int_t nlines=0;
  TGraph* g0 = new TGraph();
  g0->SetMarkerStyle(15);
  while (1){
    in>>x>>header1>>y; //header1 gets rid of the troublesome comma.
    cout<<x<<endl;
    cout<<y<<endl;
      
    if(!in.good())break;
   
    nlines++;
    g0->SetPoint(g0->GetN(), x, y);
    
   }
  printf("found %d point\n",nlines);

You have not provided your attempt at creating a subset tree.
Perhaps this example is close to what you want to accomplish: https://root.cern.ch/doc/master/copytree3_8C.html1

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