Adding Key :: Trees

Hi all,
I have a root file . Inside there is 30 tree keys. I can open one tree and read them. But now I need to add all the 30 tree keys to get all the entries . How do i can do that ?

Thank you

I think @pcanal can help you.

Can you copy/paste the result of

myfile->ls();

What is the relationship between the 30 TTrees? Why do you need collate them?

I have a root file sensor_0.root. Inside that there are trees CAM_1, CAM_2, CAM_3, …CAM_38.

I can open CAM_1 by command
TTree *tree= (TTree *)m_f->Get(“CAM_1”);
and read all the datas and make a histogram.

Now I need to open all the CAM files from 1 to 38 and add them to get all the entries and make the histogram for that .

How do i can open them all and add them ? I mean add CAM_1 + CAM_2 +… like that

A part of my code is following.

TTree *tree= (TTree *)m_f->Get("CAM_1");


        if (!tree) {
                std::cout << "ERROR: missing the event trees!" << std::endl;
        }

        Int_t idEvent;
        std::vector<double> *energy=0 ;
        std::vector<double> *time=0 ;



        tree->SetBranchAddress("time", &time);
        tree->SetBranchAddress("idEvent",&idEvent);
        tree->SetBranchAddress("energy",&energy);

        TH1F *peakno = new TH1F("peakno","peakno",20000,0,20000);

        int N = tree->GetEntries();

        std::cout << "N= " << N << std::endl;



        for (int i=0; i<N; i++)

        {
                tree->GetEntry(i);


                ULong_t nsig = time->size();
                double *sig = time->data();


                double *esig = energy->data();

                if (idEvent==1){

                for (ULong_t j=0; j<nsig; j++){

//                      std::cout << "sig[j] " << sig[j] << "j " << j <<"idEvent = " <<idEvent  <<"esig = " << esig[j] <<  std::endl;
                        peakno->Fill(sig[j]);

Here this is done only for CAM_1 . I have to do it for all CAM ( by adding them).

Thank you.

Thank you

Instead of a TFile and TTree, you can use just one TChain, and Add all the trees to it. See the example in the note about “Tagging the name of the tree…” in the Add method I linked.
Create the chain, and in a loop just make a char or string variable (or use Form) to join “CAM_” with the number and add it to the chain; e.g.

TChain *tree = new TChain();
for (int i=1; i<39; i++) {
 tree->Add(Form("%s%d","sensor_0.root?#CAM_",i));
}
if (!tree) ...  // the rest of your code will be the same

Hi,
I have total 100 root files. So, i can not open one using this command inside the code

 tree->Add(Form("sensor_0.root?#CAM_",i));

I am compiling the code by command
./Read_spill -i sensor_x.root [ when x goes from 0 to 100].

I have to give the input file from outside of the code.

Can I use

tree->Add(Form("#CAM_",i)); only ?

And in addtion following errors are coming.
1. error: ‘TChain’ was not declared in this scope
  106 |        TChain *tree = new TChanin();

2. error: ‘tree’ was not declared in this scope; did you mean ‘free’?
  106 |        TChain *tree = new TChanin();

3.  error: expected type-specifier before ‘TChanin’
  106 |        TChain *tree = new TChanin();

4. warning: too many arguments for format [-Wformat-extra-args]
  108 |         tree->Add(Form("sensor_0.root?#CAM_",i));



You said you had 38 trees in one file, so I did the code for that. If you have multiple files, each with multiple trees, and you want to Add all, you need to modify accordingly and figure out the appropriate nested loops (supposing the names only differ by numbers; otherwise, you have to explicitly write each one).
For the first 3 errors:

TChain *tree = new TChanin();

Chanin is wrong.
For the last one, I corrected my post (the “%s%d” was missing in Form…).

Thank you for your response. I modified as you said . But the error is same.

  1. error: ‘TChain’ was not declared in this scope
    106 | TChain *tree = new TChain();
  2. error: ‘tree’ was not declared in this scope; did you mean ‘free’?
    106 | TChain *tree = new TChain();
  3. error: expected type-specifier before ‘TChain’
    106 | TChain *tree = new TChain();

Do I have declare anything ?

Thank you.

‘’’ TChain *tree = new TChain();
for (int i=1;i<39;i++){
tree->Add(Form(“%s%d”,“sensor_0.root?#CAM_”,i));
}
‘’’

If you are compiling, yes, include TChain.h

Thank you there is no error now.
I have to give the input file from outside of the code. Can not open the input inside and add the trees.
I am trying to write it down like this.

// open file

        TFile *m_f = new TFile(inFilename.c_str(), "READ");
        if (!m_f) {

                std::cout << "ERROR: missing the file!" << std::endl;

        }
       TChain *tree = new TChain();
       for (int i=1;i<39;i++){
               tree->Add(Form("%s%d","*m_f?#CAM_",i));
       }
        if (!tree) {
                std::cout << "ERROR: missing the event trees!" << std::endl;
        }

But this is not working. Could you help?

Thank you.

Do you get some error messages?

There is no error message while compiling but the result i am getting this. I am sorry it very big in length.

Reading data
N= 0
Output file written to interaction.root

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007ff5f8561c3a in __GI___wait4 (pid=3744, stat_loc=stat_loc
entry=0x7ffd1dc75c68, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:27
#1  0x00007ff5f8561bfb in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7ffd1dc75c68, options=options
entry=0) at waitpid.c:38
#2  0x00007ff5f84d0f67 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
#3  0x00007ff5fa1a2e2e in TUnixSystem::StackTrace() () from /home/saradindu/root/lib/libCore.so.6.22
#4  0x00007ff5fa19fcb5 in TUnixSystem::DispatchSignals(ESignals) () from /home/saradindu/root/lib/libCore.so.6.22
#5  <signal handler called>
#6  __GI___libc_free (mem=0x545f63696d6f7461) at malloc.c:3102
#7  0x00007ff5f48fa962 in ROOT::TMetaUtils::GetFullyQualifiedTypeName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, clang::QualType const&, clang::ASTContext const&) () from /home/saradindu/root/lib/libCling.so
#8  0x00007ff5f48faaad in ROOT::TMetaUtils::GetFullyQualifiedTypeName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, clang::QualType const&, cling::Interpreter const&) () from /home/saradindu/root/lib/libCling.so
#9  0x00007ff5f4a23f5c in TClingDataMemberInfo::TypeName() const () from /home/saradindu/root/lib/libCling.so
#10 0x00007ff5fa1554c5 in TDataMember::Init(bool) () from /home/saradindu/root/lib/libCore.so.6.22
#11 0x00007ff5fa155fa6 in TDataMember::TDataMember(DataMemberInfo_t*, TClass*) () from /home/saradindu/root/lib/libCore.so.6.22
#12 0x00007ff5fa160213 in TListOfDataMembers::Get(DataMemberInfo_t*, bool) () from /home/saradindu/root/lib/libCore.so.6.22
#13 0x00007ff5fa16061f in TListOfDataMembers::Load() () from /home/saradindu/root/lib/libCore.so.6.22
#14 0x00007ff5fa1382a3 in TClass::GetListOfDataMembers(bool) () from /home/saradindu/root/lib/libCore.so.6.22
#15 0x00007ff5fa14444a in TClass::GetDataMember(char const*) const () from /home/saradindu/root/lib/libCore.so.6.22
#16 0x00007ff5fa147c02 in TBuildRealData::Inspect(TClass*, char const*, char const*, void const*, bool) () from /home/saradindu/root/lib/libCore.so.6.22
#17 0x00007ff5f49f0eba in TCling::InspectMembers(TMemberInspector&, void const*, TClass const*, bool) () from /home/saradindu/root/lib/libCling.so
#18 0x00007ff5fa13a2a5 in TClass::CallShowMembers(void const*, TMemberInspector&, bool) const () from /home/saradindu/root/lib/libCore.so.6.22
#19 0x00007ff5fa14629d in TClass::BuildRealData(void*, bool) () from /home/saradindu/root/lib/libCore.so.6.22
#20 0x00007ff5f9b9121d in TBufferFile::WriteClassBuffer(TClass const*, void*) () from /home/saradindu/root/lib/libRIO.so.6.22
#21 0x00007ff5fa17df87 in TStreamerBase::Streamer(TBuffer&) () from /home/saradindu/root/lib/libCore.so.6.22
#22 0x00007ff5f9b90ce6 in TBufferFile::WriteObjectClass(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#23 0x00007ff5f9b97be4 in TBufferIO::WriteObjectAny(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#24 0x00007ff5fa107ddd in TObjArray::Streamer(TBuffer&) () from /home/saradindu/root/lib/libCore.so.6.22
#25 0x00007ff5f9b90ce6 in TBufferFile::WriteObjectClass(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#26 0x00007ff5f9b97acf in TBufferIO::WriteObjectAny(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#27 0x00007ff5f9c54b0b in TStreamerInfo::Streamer(TBuffer&) () from /home/saradindu/root/lib/libRIO.so.6.22
#28 0x00007ff5f9b90ce6 in TBufferFile::WriteObjectClass(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#29 0x00007ff5f9b97be4 in TBufferIO::WriteObjectAny(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#30 0x00007ff5fa10317c in TList::Streamer(TBuffer&) () from /home/saradindu/root/lib/libCore.so.6.22
#31 0x00007ff5f9c33ee2 in TKey::TKey(TObject const*, char const*, int, TDirectory*) () from /home/saradindu/root/lib/libRIO.so.6.22
#32 0x00007ff5f9c061d5 in TFile::WriteStreamerInfo() () from /home/saradindu/root/lib/libRIO.so.6.22
#33 0x00007ff5f9c04c03 in TFile::Close(char const*) () from /home/saradindu/root/lib/libRIO.so.6.22
#34 0x00007ff5f9c051f6 in TFile::~TFile() () from /home/saradindu/root/lib/libRIO.so.6.22
#35 0x00007ff5f9c054dd in TFile::~TFile() () from /home/saradindu/root/lib/libRIO.so.6.22
#36 0x00007ff5fa100a48 in TList::Delete(char const*) () from /home/saradindu/root/lib/libCore.so.6.22
#37 0x00007ff5fa041376 in TROOT::~TROOT() () from /home/saradindu/root/lib/libCore.so.6.22
#38 0x00007ff5f84c58a7 in __run_exit_handlers (status=0, listp=0x7ff5f866b718 <__exit_funcs>, run_list_atexit=run_list_atexit
entry=true, run_dtors=run_dtors
entry=true) at exit.c:108
#39 0x00007ff5f84c5a60 in __GI_exit (status=<optimized out>) at exit.c:139
#40 0x00007ff5f84a308a in __libc_start_main (main=0x55b93ba1c9df <main>, argc=3, argv=0x7ffd1dc7c008, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffd1dc7bff8) at ../csu/libc-start.c:342
#41 0x000055b93ba1b9ce in _start ()
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at http://root.cern.ch/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  __GI___libc_free (mem=0x545f63696d6f7461) at malloc.c:3102
#7  0x00007ff5f48fa962 in ROOT::TMetaUtils::GetFullyQualifiedTypeName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, clang::QualType const&, clang::ASTContext const&) () from /home/saradindu/root/lib/libCling.so
#8  0x00007ff5f48faaad in ROOT::TMetaUtils::GetFullyQualifiedTypeName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, clang::QualType const&, cling::Interpreter const&) () from /home/saradindu/root/lib/libCling.so
#9  0x00007ff5f4a23f5c in TClingDataMemberInfo::TypeName() const () from /home/saradindu/root/lib/libCling.so
#10 0x00007ff5fa1554c5 in TDataMember::Init(bool) () from /home/saradindu/root/lib/libCore.so.6.22
#11 0x00007ff5fa155fa6 in TDataMember::TDataMember(DataMemberInfo_t*, TClass*) () from /home/saradindu/root/lib/libCore.so.6.22
#12 0x00007ff5fa160213 in TListOfDataMembers::Get(DataMemberInfo_t*, bool) () from /home/saradindu/root/lib/libCore.so.6.22
#13 0x00007ff5fa16061f in TListOfDataMembers::Load() () from /home/saradindu/root/lib/libCore.so.6.22
#14 0x00007ff5fa1382a3 in TClass::GetListOfDataMembers(bool) () from /home/saradindu/root/lib/libCore.so.6.22
#15 0x00007ff5fa14444a in TClass::GetDataMember(char const*) const () from /home/saradindu/root/lib/libCore.so.6.22
#16 0x00007ff5fa147c02 in TBuildRealData::Inspect(TClass*, char const*, char const*, void const*, bool) () from /home/saradindu/root/lib/libCore.so.6.22
#17 0x00007ff5f49f0eba in TCling::InspectMembers(TMemberInspector&, void const*, TClass const*, bool) () from /home/saradindu/root/lib/libCling.so
#18 0x00007ff5fa13a2a5 in TClass::CallShowMembers(void const*, TMemberInspector&, bool) const () from /home/saradindu/root/lib/libCore.so.6.22
#19 0x00007ff5fa14629d in TClass::BuildRealData(void*, bool) () from /home/saradindu/root/lib/libCore.so.6.22
#20 0x00007ff5f9b9121d in TBufferFile::WriteClassBuffer(TClass const*, void*) () from /home/saradindu/root/lib/libRIO.so.6.22
#21 0x00007ff5fa17df87 in TStreamerBase::Streamer(TBuffer&) () from /home/saradindu/root/lib/libCore.so.6.22
#22 0x00007ff5f9b90ce6 in TBufferFile::WriteObjectClass(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#23 0x00007ff5f9b97be4 in TBufferIO::WriteObjectAny(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#24 0x00007ff5fa107ddd in TObjArray::Streamer(TBuffer&) () from /home/saradindu/root/lib/libCore.so.6.22
#25 0x00007ff5f9b90ce6 in TBufferFile::WriteObjectClass(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#26 0x00007ff5f9b97acf in TBufferIO::WriteObjectAny(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#27 0x00007ff5f9c54b0b in TStreamerInfo::Streamer(TBuffer&) () from /home/saradindu/root/lib/libRIO.so.6.22
#28 0x00007ff5f9b90ce6 in TBufferFile::WriteObjectClass(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#29 0x00007ff5f9b97be4 in TBufferIO::WriteObjectAny(void const*, TClass const*, bool) () from /home/saradindu/root/lib/libRIO.so.6.22
#30 0x00007ff5fa10317c in TList::Streamer(TBuffer&) () from /home/saradindu/root/lib/libCore.so.6.22
#31 0x00007ff5f9c33ee2 in TKey::TKey(TObject const*, char const*, int, TDirectory*) () from /home/saradindu/root/lib/libRIO.so.6.22
#32 0x00007ff5f9c061d5 in TFile::WriteStreamerInfo() () from /home/saradindu/root/lib/libRIO.so.6.22
#33 0x00007ff5f9c04c03 in TFile::Close(char const*) () from /home/saradindu/root/lib/libRIO.so.6.22
#34 0x00007ff5f9c051f6 in TFile::~TFile() () from /home/saradindu/root/lib/libRIO.so.6.22
#35 0x00007ff5f9c054dd in TFile::~TFile() () from /home/saradindu/root/lib/libRIO.so.6.22
#36 0x00007ff5fa100a48 in TList::Delete(char const*) () from /home/saradindu/root/lib/libCore.so.6.22
#37 0x00007ff5fa041376 in TROOT::~TROOT() () from /home/saradindu/root/lib/libCore.so.6.22
#38 0x00007ff5f84c58a7 in __run_exit_handlers (status=0, listp=0x7ff5f866b718 <__exit_funcs>, run_list_atexit=run_list_atexit
entry=true, run_dtors=run_dtors
entry=true) at exit.c:108
#39 0x00007ff5f84c5a60 in __GI_exit (status=<optimized out>) at exit.c:139
#40 0x00007ff5f84a308a in __libc_start_main (main=0x55b93ba1c9df <main>, argc=3, argv=0x7ffd1dc7c008, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffd1dc7bff8) at ../csu/libc-start.c:342
#41 0x000055b93ba1b9ce in _start ()
===========================================================


free(): double free detected in tcache 2
Aborted (core dumped)

Can you post the complete code producing this scratch?

please find the attached code.

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <unistd.h>
#include <stdlib.h>
#include <algorithm>

#include "struct.h"
#include "ReadGDML.h"
#include "ReadEDepSim.h"
#include "ReadOptical.h"
#include "ReadDigit.h"
#include "CameraImage.h"
#include "FastFitter.h"
#include "LensDisplayer.h"
#include "LineIntersector.h"
#include "StructDefinition.h"
#include "TrackMatcher.h"
#include "Volume3DReconstructer.h"
#include "EnergyEstimator.h"
#include "TFile.h"
#include "TTree.h"
#include "TMath.h"
#include "TApplication.h"
#include "TF1.h"
#include "TH2I.h"
#include "TCanvas.h"
#include "TVector3.h"
#include "TH1.h"
#include "TRandom.h"
#include "TSpectrum.h"
#include "TChain.h"
#include "TVirtualFitter.h"
#include "Riostream.h"
Int_t npeaks=10 ;
Int_t A;
Int_t B;
Int_t t;
float taufast=6e-9;
float tauslow=1.6e-6;


void usage() {
   std::cout << "Input parameters needed: " << std::endl;
   std::cout << "-i 'root file with spill data '" << std::endl;
   std::cout << std::endl;
}


int main(int argc, char **argv) {
   std::string inFilename="";
   std::string outFilename="";
   int iflag=0, oflag=0; //mandatory
   int opt;

   outFilename = "interaction.root";

   while( (opt = getopt(argc, argv, "i:o")) != -1) {
      switch(opt) {
         case 'o':
            if(optarg) outFilename = optarg;
            oflag = 1;
            break;
         case 'i':
            if(optarg) inFilename = optarg;
            iflag = 1;
            break;
         default:
            usage();
            return 1;
      }
   }

   if(!iflag) {
      std::cout << "ERROR: missing REQUIRED parameters!" << std::endl;
      usage();
      return 1;
   }

   std::cout << "Sensor file: " << inFilename << std::endl;
   std::cout << "Output file: " << outFilename << std::endl;
   std::cout << "*****************************************************" << std::endl;
   std::cout << "Reading data" << std::endl;

   // open file

   TFile *m_f = new TFile(inFilename.c_str(), "READ");
   if (!m_f) {
      std::cout << "ERROR: missing the file!" << std::endl;
   }

   TChain *tree = new TChain();
   for (int i=1;i<39;i++){
      tree->Add(Form("%s%d","*m_f?#CAM_",i));
   }

   if (!tree) {
      std::cout << "ERROR: missing the event trees!" << std::endl;
   }

   Int_t idEvent;
   std::vector<double> *energy=0 ;
   std::vector<double> *time=0 ;

   tree->SetBranchAddress("time", &time);
   tree->SetBranchAddress("idEvent",&idEvent);
   tree->SetBranchAddress("energy",&energy);

   TH1F *peakno = new TH1F("peakno","peakno",20000,0,20000);

   int N = tree->GetEntries();

   std::cout << "N= " << N << std::endl;

   for (int i=0; i<N; i++)
   {
      tree->GetEntry(i);
      ULong_t nsig = time->size();
      double *sig = time->data();
      double *esig = energy->data();

      if (idEvent==1) {
         for (ULong_t j=0; j<nsig; j++){
            std::cout << "sig[j] " << sig[j] << "j " << j <<"idEvent = " <<idEvent  <<"esig = " << esig[j] <<  std::endl;
            peakno->Fill(sig[j]);
         }

         TSpectrum *s = new TSpectrum(npeaks);
         Int_t nfound = s->Search(peakno,0.0,"",0.15);
         Int_t peaks;

         auto *xpeaks = s->GetPositionX();
         for (Int_t p=0;p<nfound;p++ )
         {
            Float_t xp =xpeaks[p];
            std::cout << "xp " << xp << std::endl;
            Int_t bin =peakno->GetXaxis()->FindBin(xp);
            std::cout << "bin = " << bin << std::endl;
            Float_t yp=peakno->GetBinContent(bin);
            std::cout << "yp = " <<yp <<   std::endl;
            std::cout << " p = " << p << "nfound = " << nfound << std::endl;
            if (yp > 6.0) {peaks=p+1;}
         }
         std::cout << "peak number is = " << nfound <<"The modified peak no is = " << peaks << std::endl;
      }
   }

   TFile *outputFile = new TFile(outFilename.c_str(),"RECREATE");
   std::cout << "Output file written to " << outFilename << std::endl;

   peakno->Write();
}

I have intended a bit your code to make it easier to read:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <unistd.h>
#include <stdlib.h>
#include <algorithm>

#include "struct.h"
#include "ReadGDML.h"
#include "ReadEDepSim.h"
#include "ReadOptical.h"
#include "ReadDigit.h"
#include "CameraImage.h"
#include "FastFitter.h"
#include "LensDisplayer.h"
#include "LineIntersector.h"
#include "StructDefinition.h"
#include "TrackMatcher.h"
#include "Volume3DReconstructer.h"
#include "EnergyEstimator.h"
#include "TFile.h"
#include "TTree.h"
#include "TMath.h"
#include "TApplication.h"
#include "TF1.h"
#include "TH2I.h"
#include "TCanvas.h"
#include "TVector3.h"
#include "TH1.h"
#include "TRandom.h"
#include "TSpectrum.h"
#include "TChain.h"
#include "TVirtualFitter.h"
#include "Riostream.h"

Int_t npeaks=10 ;
Int_t A;
Int_t B;
Int_t t;
float taufast=6e-9;
float tauslow=1.6e-6;

void usage() {
   std::cout << "Input parameters needed: " << std::endl;
   std::cout << "-i 'root file with spill data '" << std::endl;
   std::cout << std::endl;
}

int main(int argc, char **argv) {

   std::string inFilename="";
   std::string outFilename="";
   int iflag=0, oflag=0; //mandatory
   int opt;

   outFilename = "interaction.root";


   while( (opt = getopt(argc, argv, "i:o")) != -1) {
      switch(opt) {
         case 'o':
            if(optarg) outFilename = optarg;
            oflag = 1;
            break;
         case 'i':
            if(optarg) inFilename = optarg;
            iflag = 1;
            break;
         default:
            usage();
            return 1;
      }
   }

   if (!iflag) {
      std::cout << "ERROR: missing REQUIRED parameters!" << std::endl;
      usage();
      return 1;
   }

   std::cout << "Sensor file: " << inFilename << std::endl;
   std::cout << "Output file: " << outFilename << std::endl;
   std::cout << "*****************************************************" << std::endl;
   std::cout << "Reading data" << std::endl;

   // open file
   TFile *m_f = new TFile(inFilename.c_str(), "READ");
   if (!m_f) std::cout << "ERROR: missing the file!" << std::endl;

   TChain *tree = new TChain();
   for (int i=1; i<39; i++) tree->Add(Form("%s%d","*m_f?#CAM_",i));

   if (!tree) std::cout << "ERROR: missing the event trees!" << std::endl;

   Int_t idEvent;
   std::vector<double> *energy=0 ;
   std::vector<double> *time=0 ;

   tree->SetBranchAddress("time", &time);
   tree->SetBranchAddress("idEvent",&idEvent);
   tree->SetBranchAddress("energy",&energy);

   TH1F *peakno = new TH1F("peakno","peakno",20000,0,20000);

   int N = tree->GetEntries();
   std::cout << "N= " << N << std::endl;

   for (int i=0; i<N; i++) {
      tree->GetEntry(i);
      ULong_t nsig = time->size();
      double *sig = time->data();
      double *esig = energy->data();

      if (idEvent==1){

         for (ULong_t j=0; j<nsig; j++){
            std::cout << "sig[j] " << sig[j] << "j " << j <<"idEvent = " <<idEvent  <<"esig = " << esig[j] <<  std::endl;
            peakno->Fill(sig[j]);
         }

         TSpectrum *s = new TSpectrum(npeaks);
         Int_t nfound = s->Search(peakno,0.0,"",0.15);
         Int_t peaks;
         auto *xpeaks = s->GetPositionX();

         for (Int_t p=0;p<nfound;p++ ) {
            Float_t xp =xpeaks[p];
            std::cout << "xp " << xp << std::endl;
            Int_t bin =peakno->GetXaxis()->FindBin(xp);
            std::cout << "bin = " << bin << std::endl;
            Float_t yp=peakno->GetBinContent(bin);
            std::cout << "yp = " <<yp <<   std::endl;
            std::cout << " p = " << p << "nfound = " << nfound << std::endl;
            if (yp > 6.0) {peaks=p+1;}
         }
         std::cout << "peak number is = " << nfound <<"The modified peak no is = " << peaks << std::endl;
      }
   }

   TFile *outputFile = new TFile(outFilename.c_str(),"RECREATE");
   std::cout << "Output file written to " << outFilename << std::endl;
   peakno->Write();
}

In the output you sent we see:

Reading data
N= 0
Output file written to interaction.root

N = 0 suggest the TChain tree has 0 entry. Are you sure the building of the chain worked properlly ? are the file names, generated by Form, correct ?

When I am opening the input file inside the code it is fine. Then the number of entries are fine. sensor_0.root is one input file among 100.

‘’’
TChain *tree = new TChain();
for (int i=1;i<39;i++){
tree->Add(Form(“%s%d”,“sensors_0.root?#CAM_”,i));
}
‘’’

But I can not give the input file file name inside the code. I have to open it using TFile.
And i have to give the input file name while compiling.

./Compiler -i sensor_0.root

The problem arises can not open TFile correctly.

‘’’
// open file

    TFile *m_f = new TFile(inFilename.c_str(), "READ");
    if (!m_f) {

            std::cout << "ERROR: missing the file!" << std::endl;

    }


   TChain *tree = new TChain();
   for (int i=1;i<39;i++){
           tree->Add(Form("%s%d","*m_f**?**#CAM_",i));
   }

‘’’
I tried to use *m_f inside TChain. I think this is not correct way to do.I can not understand what should be the correct command here.
Thank you.

So when you do:

./Compiler -i sensor_0.root

You get the following message from you program:

ERROR: missing the file!

Right?
can you print inFilename.c_str() to see what it is ?

No, actually it is not finding the input file inside the TChain. I gave command

tree->Add(Form("%s%d","*m_f**?**#CAM_",i));   

I gave *m_f but it is saying not finding the input file. If i give the input file directly then its fine. I have to give a generic form of the input.

Two question:

  1. What are the file names you are trying to add in the TChain?

  2. Can you add the line:

printf("%s\n", inFilename.c_str());

before the line:

tree->Add(Form("%s%d","*m_f**?**#CAM_",i)); 

to see the actual file names you are trying to put in the chain.

Yes, it prints the input file name sensor_0.root.