Adding Key :: Trees

Can you copy/paste the print out?

Can you give me the names of the files you would like to add in the Chain ?

Please find the attached files. I can not attach input file due to size limitation.

./Read_spill -i sensors_0.root 
Sensor file: sensors_0.root
Output file: interaction.root
*****************************************************
Reading data
sensors_0.root
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
Error in <TFile::TFile>: file m_f does not exist
N= 0
Output file written to interaction.root

 *** Break *** segmentation violation
munmap_chunk(): invalid pointer
Aborted (core dumped)

Ok, can you tell me what are the NAMES of the files you want to add in the chain ?

And now instead of *m_f if i give the name of input directly that is sensor_0.root

the output is nice . I am attaching that.
‘’'Sensor file: sensors_0.root
Output file: interaction.root


Reading data
sensors_0.root
N= 342
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
xp 4961.5
bin = 4962
yp = 10
p = 0nfound = 3
xp 1831.5
bin = 1832
yp = 3
p = 1nfound = 3
xp 9384.5
bin = 9385
yp = 3
p = 2nfound = 3
peak number is = 3The modified peak no is = 1
xp 1832.5
bin = 1833
yp = 27
p = 0nfound = 3
xp 9385.5
bin = 9386
yp = 16
p = 1nfound = 3
xp 4962.5
bin = 4963
yp = 9
p = 2nfound = 3
peak number is = 3The modified peak no is = 3
xp 1831.5
bin = 1832
yp = 81
p = 0nfound = 2
xp 9385.5
bin = 9386
yp = 23
p = 1nfound = 2
peak number is = 2The modified peak no is = 2
xp 1831.5
bin = 1832
yp = 117
p = 0nfound = 2
xp 9384.5
bin = 9385
yp = 35
p = 1nfound = 2
peak number is = 2The modified peak no is = 2
xp 1831.5
bin = 1832
yp = 119
p = 0nfound = 3
xp 9384.5
bin = 9385
yp = 35
p = 1nfound = 3
xp 4961.5
bin = 4962
yp = 30
p = 2nfound = 3
peak number is = 3The modified peak no is = 3
xp 1831.5
bin = 1832
yp = 125
p = 0nfound = 2
xp 9384.5
bin = 9385
yp = 39
p = 1nfound = 2
peak number is = 2The modified peak no is = 2
xp 1831.5
bin = 1832
yp = 152
p = 0nfound = 2
xp 9384.5
bin = 9385
yp = 44
p = 1nfound = 2
peak number is = 2The modified peak no is = 2
xp 1831.5
bin = 1832
yp = 156
p = 0nfound = 2
xp 9384.5
bin = 9385
yp = 47
p = 1nfound = 2
peak number is = 2The modified peak no is = 2
xp 1831.5
bin = 1832
yp = 157
p = 0nfound = 3
xp 9384.5
bin = 9385
yp = 47
p = 1nfound = 3
xp 4961.5
bin = 4962
yp = 40
p = 2nfound = 3
peak number is = 3The modified peak no is = 3
xp 1831.5
bin = 1832
yp = 160
p = 0nfound = 3
xp 9384.5
bin = 9385
yp = 50
p = 1nfound = 3
xp 4961.5
bin = 4962
yp = 40
p = 2nfound = 3
peak number is = 3The modified peak no is = 3
xp 1831.5
bin = 1832
yp = 162
p = 0nfound = 3
xp 9384.5
bin = 9385
yp = 51
p = 1nfound = 3
xp 4961.5
bin = 4962
‘’’

Inside TChain i have to give the input name directly . I can not do that because i am compiling in another folder where i use ./Compiler -i input. The code should read the input and do the addition inside TChain.It is not getting input correctly if i dont give it inside TChain.

It is not really useful to build a chain with always the same file name. Of course, it works, but it is meaningless.

Once again, tell me what are the names of the files you would like to put in the chain and I will write the for loop which does that correctly.

Hi sir, Thank you. I explain a little bit more .
I have two folders one is tools where the code is written named as Read_spill.cpp and another is build where i give three commands to run the code.

  1. cmake …/.
  2. cmake
  3. ./Read_spill -i sensors_x.root [ here i have to change x each time from 1 to 100 ] .

Now in each sensors_x.root i have trees CAM_1, CAM_2,…, CAM_38 . I have to add all the CAM to get a single tree . Then from that tree i will get no of entries and make a histogram.

Inside chain I need to add all the CAM files for each sensors file. I do not want to add all the sensors file. I want to read one sensor file and add all the CAM files inside it to get a single tree.

Thank you

So you have the files:

sensors_1.root 
sensors_2.root 
sensors_3.root 
sensors_4.root 
sensors_5.root 
...
sensors_100.root 

And in each file the trees:

CAM_1
...
CAM_38

And you need to analyze all that trees in one go.
in that case, the code to build the chain is:

   TChain *tree = new TChain();
   for (int i=1; i<=100; i++) {
      for (int j=1; j<=38; j++) {
         printf("Adding %s in the chain\n",Form("./sensors_%d.root/CAM_%d",i,j));
          tree->Add(Form("./sensors_%d.root/CAM_%d",i,j));
      }
   }

Thank you very much. But i don’t want to add all the sensors_x.root file in one go.

Will have to open a single sensor file (sensors_0.root) and analyse all the trees inside it in one go .
Then again I have to open another sensor file (sensor_1.root) and again have to analyse all the trees inside it in one go.
Then again i have to open another sensor file (sensor_2.root) and analyse all the trees inside it in one go.

In that case do:

   for (int j=1; j<=38; j++) {
         printf("Adding %s in the chain\n",
                Form("./%s/CAM_%d",inFilename.c_str(),j)
                );
         tree->Add(
                Form("./%s/CAM_%d",inFilename.c_str(),j)
                );
   }

Its working fine now. Thank you very much for your kind advise and kind help.

regards,
Saradindu

Hi,
I am trying to add these files with a threshold value. Like, all these files (CAM_1, CAM_2,…) contain spectrums where the peaks have counts . I want to put a threshold of 50 in count in each of them and then do the addition. How do I can do that?

Thank you.

What does that mean exactly? Do you want to select only the peaks greater than 50?

Yes, for individual file i want to select those peaks with counts greater than 50 . Then i will add all the files. And in the final file i want to select peaks with a smaller thresold like 20 or so.

In the program in the final file i am able to give a cut using the search function.

May be post the code you already have in order to clarify your request.

The code is

#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;

        }


       Int_t k;
TChain *tree = new TChain();
       for (int k=1;k<39;k++){

               tree->Add(Form("./%s/CAM_%d",inFilename.c_str(),k));

       }

        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==0){

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

                        peakno->Fill(sig[j]);
                }
TSpectrum *s = new TSpectrum(npeaks);
                        Int_t nfound = s->Search(peakno,0.0,"",0.1);
                        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;


        }


        }

        }

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

        peakno->Write();


}

Thanks.
Note: to include code in the forum post you should use 3 back-quotes ```, not 3 quotes ‘’’

Okay.Thank you.

I have indented your code to make it a bit 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;

   Int_t k;
   TChain *tree = new TChain();
   for (int k=1;k<39;k++) tree->Add(Form("./%s/CAM_%d",inFilename.c_str(),k));

   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==0){
         for (ULong_t j=0; j<nsig; j++) peakno->Fill(sig[j]);
         TSpectrum *s = new TSpectrum(npeaks);
         Int_t nfound = s->Search(peakno,0.0,"",0.1);
         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;
         }
      }
   }

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

I guess you should put some logic dealing with the threshold in the loop for (Int_t p=0;p<nfound;p++ ). As I do not fully understand your exact goal I guess that’s up to you to decide what to do…

Thank you.
What i am trying to do : Here using TChain I am adding all the trees to get a single tree.
The trees are CAM_1, CAM_2…and so on.
If we read each tree individually we will get spectrum. Suppose I open CAM_1 and get peaks at 1000 and 2000 with counts 70 and 80. Then again open CAM_2 where i get peak 1000 and 2000 with counts 20 and 40…and so on. Ultimately I will add all the files for all 38 trees which is done inside TChain. So, in end result the count of peak 1000 will be addition of all counts from CAM_1, CAM_2,… and exactly for 2000 also.
Now i am trying to use a thresold for smaller peaks which are detected sometimes by peakfinder. I want to set a thresold in the count of CAM_1, CAM_2,… Suppose if the thresold is 50 then if the count of a peak at 3000 in CAM_3 is 50 it will not be considered. So while adding in TChain i am trying to give a cut of 50 in individual CAM_… So, the smaller peaks with less counts will not be considered.
And when everything is ready and we get the final tree I will set a cut of 20 in the final file to give another filter in the spectrum.

Is this possible ? I can not undertstand how do i set the thresold in count before adding in Tchain. Is it clear ?

Thank you.