Thresold set up in Yaxis count in histogram

Hi all,
My code is following

#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 npeaks=10 ;
int final_nfound;
int final_nfound1;
int A;
int B;
int 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;

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

   int 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 idEvent;
   std::vector<double> *energy=0 ;
   std::vector<double> *time=0 ;

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

   const int n=9;
TH1F *peakno[n];
   TH1F *intno[n];
   TH1F *inttotal= new TH1F ("inttotal","inttotal",100,0,100);

   for (int i=0;i<n;i++) {
      peakno[i] = new TH1F(Form("peakno%d",i),Form("peakno%d",i),20000,0,20000);
      intno[i]  = new TH1F(Form("intno%d",i),Form("intno%d",i),100,0,100);
   }

   int N = tree->GetEntries();

   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 && idEvent<=8) {
         for (ULong_t j=0; j<nsig; j++) {
           if (esig[j] < 6.0e-6 ) continue;
            peakno[idEvent]->Fill(sig[j]);
         }
      }
   }

   for (int i=0;i<n;i++) {
      TSpectrum *s = new TSpectrum(npeaks);
      int nfound = s->Search(peakno[i],0.0,"",0.1);
      intno[i]->Fill(nfound);
      std::cout << "idEvent = " << i << "no of peaks= " << nfound << std::endl;
      inttotal->Add(intno[i]);
   }

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

   for (int i=0;i<n;i++) {
      peakno[i]->Write();
      intno[i]->Write();
   }
   inttotal->Write();
   outputFile->Close();
}

After running the code i generated few histogram with no of peaks. The output is like,

Reading data
Warning in <TSpectrum::SearchHighRes>: Peak buffer full
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
idEvent = 0no of peaks= 10
idEvent = 1no of peaks= 2
idEvent = 2no of peaks= 3
idEvent = 3no of peaks= 1
idEvent = 4no of peaks= 1
idEvent = 5no of peaks= 5
idEvent = 6no of peaks= 2
idEvent = 7no of peaks= 3
idEvent = 8no of peaks= 1
Output file written to interaction.root

Now in the histogram,
peakno0.pdf (80.3 KB)

there is no actual peak…There is no counts… I want to put a thresold into the yaxis count in the code so that this histogram will not be generated. Is it possible to give yaxis count before generating the histogram into this code ?

If we see another example of histogram,
peakno1.pdf (111.7 KB)
this shows two nice peaks.

Thank you.

You can save the histograms this way:

for (int i=0;i<n;i++)  {
   if (peakno[i]->GetMaximum() > treshold) {
      peakno[i]->Write();
      intno[i]->Write();
   }
}

And you can do a similar test to on search the peaks on the histogram having theirs maximum greater than treshold:

for (int i=0;i<n;i++) {
   if (peakno[i]->GetMaximum() > treshold) {
      TSpectrum *s = new TSpectrum(npeaks);
      int nfound = s->Search(peakno[i],0.0,"",0.1);
      intno[i]->Fill(nfound);
      std::cout << "idEvent = " << i << "no of peaks= " << nfound << std::endl;
      inttotal->Add(intno[i]);
   }
}

Thank you very much sir.

regards,
saradindu

When I am executing my code, I have to excute like this …

./Read_spill -i sensors_0.root where sensors_0.root is my input file. I have to do this for each sensor file from 0 to 99 manually.
How to read all the inputs automatically and save the informations ? Can I do that inside code ?

I was trying to create a text file input.txt and run a command ./Read_spill input.txt and inside input.txt there will be ,
sensors_0.root
sensors_1.root
…
so on. The code will read all of them and do the simulation. How do I can do that ?

Thank you.

You can loop over the 100 files using a "for loop"as follow:

for (int file_number = 0; file_number < 100; file_number++ {
   ...
   TFile *m_f = new TFile(Form("sensors_%d.root", file_number), "READ");
   ...
}

Thank you . But there is a part in the code where i use ,

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


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

where inFilename is changed according to input ( whether it is sensors_0.root / sensors_1.root …etc). How do i modify this ?

Thank you

You type the command:

./Read_spill -i sensors_0.root

Instead of given sensors_0.root simply to a loop over all the sensors_i.rootfiles.

So, the input file will be a root file containing for loop?

no, you will loop over the root files… TFile with a Form() … as I said before.

      for (int file_number = 0; file_number < 2; file_number++) {
      TFile *m_f = new TFile(Form("sensors_%d.root", file_number), "READ");
      }
     
      int k;
     TChain *tree = new TChain();
       for (int k=1;k<39;k++){
          tree->Add(Form("./%s/CAM_%d",inFilename.c_str(),k));
       }

I have used this loop . But, when I am using the command ./Read_spill -i sensors_0.root the result is coming for only one input file.

Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
Event No = 1 :  No of Peaks = 2
Event No = 2 :  No of Peaks = 3
Event No = 3 :  No of Peaks = 1
Event No = 4 :  No of Peaks = 1
Event No = 6 :  No of Peaks = 2
Event No = 8 :  No of Peaks = 1

I meant something like that:

#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 npeaks=10 ;
int final_nfound;
int final_nfound1;
int A;
int B;
int 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;
//    }


  for (int file_number = 0; file_number < 2; file_number++) {
     TFile *m_f = new TFile(Form("sensors_%d.root", file_number), "READ");

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

      int 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 idEvent;
      std::vector<double> *energy=0 ;
      std::vector<double> *time=0 ;

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

      const int n=9;
      TH1F *peakno[n];
      TH1F *intno[n];
      TH1F *inttotal= new TH1F ("inttotal","inttotal",100,0,100);

      for (int i=0;i<n;i++) {
         peakno[i] = new TH1F(Form("peakno%d",i),Form("peakno%d",i),20000,0,20000);
         intno[i]  = new TH1F(Form("intno%d",i),Form("intno%d",i),100,0,100);
      }

      int N = tree->GetEntries();

      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 && idEvent<=8) {
            for (ULong_t j=0; j<nsig; j++) {
              if (esig[j] < 6.0e-6 ) continue;
               peakno[idEvent]->Fill(sig[j]);
            }
         }
      }

      for (int i=0;i<n;i++) {
         TSpectrum *s = new TSpectrum(npeaks);
         int nfound = s->Search(peakno[i],0.0,"",0.1);
         intno[i]->Fill(nfound);
         std::cout << "idEvent = " << i << "no of peaks= " << nfound << std::endl;
         inttotal->Add(intno[i]);
      }

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

      for (int i=0;i<n;i++) {
         peakno[i]->Write();
         intno[i]->Write();
      }
      inttotal->Write();
      outputFile->Close();
   }
}

The code is giving this error.

Output file: interaction.root
*****************************************************
Reading data
Error in <TFile::TFile>: file .//CAM_1 does not exist
Error in <TFile::TFile>: file .//CAM_2 does not exist
Error in <TFile::TFile>: file .//CAM_3 does not exist
Error in <TFile::TFile>: file .//CAM_4 does not exist
Error in <TFile::TFile>: file .//CAM_5 does not exist
Error in <TFile::TFile>: file .//CAM_6 does not exist
Error in <TFile::TFile>: file .//CAM_7 does not exist
Error in <TFile::TFile>: file .//CAM_8 does not exist
Error in <TFile::TFile>: file .//CAM_9 does not exist
Error in <TFile::TFile>: file .//CAM_10 does not exist
Error in <TFile::TFile>: file .//CAM_11 does not exist
Error in <TFile::TFile>: file .//CAM_12 does not exist
Error in <TFile::TFile>: file .//CAM_13 does not exist
Error in <TFile::TFile>: file .//CAM_14 does not exist
Error in <TFile::TFile>: file .//CAM_15 does not exist
Error in <TFile::TFile>: file .//CAM_16 does not exist
Error in <TFile::TFile>: file .//CAM_17 does not exist
Error in <TFile::TFile>: file .//CAM_18 does not exist
Error in <TFile::TFile>: file .//CAM_19 does not exist
Error in <TFile::TFile>: file .//CAM_20 does not exist
Error in <TFile::TFile>: file .//CAM_21 does not exist
Error in <TFile::TFile>: file .//CAM_22 does not exist
Error in <TFile::TFile>: file .//CAM_23 does not exist
Error in <TFile::TFile>: file .//CAM_24 does not exist
Error in <TFile::TFile>: file .//CAM_25 does not exist
Error in <TFile::TFile>: file .//CAM_26 does not exist
Error in <TFile::TFile>: file .//CAM_27 does not exist
Error in <TFile::TFile>: file .//CAM_28 does not exist
Error in <TFile::TFile>: file .//CAM_29 does not exist
Error in <TFile::TFile>: file .//CAM_30 does not exist
Error in <TFile::TFile>: file .//CAM_31 does not exist
Error in <TFile::TFile>: file .//CAM_32 does not exist
Error in <TFile::TFile>: file .//CAM_33 does not exist
Error in <TFile::TFile>: file .//CAM_34 does not exist
Error in <TFile::TFile>: file .//CAM_35 does not exist
Error in <TFile::TFile>: file .//CAM_36 does not exist
Error in <TFile::TFile>: file .//CAM_37 does not exist
Error in <TFile::TFile>: file .//CAM_38 does not exist
Output file written to interaction.root

I will need your code and the data file in order to try

Hello Sir,
Following is the code,

#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 npeaks=10 ;
int final_nfound;
int final_nfound1;
int A;
int B;
int 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;

   std::string str1=inFilename.substr(8,2);
   int fileno=stoi(str1);
   std::cout << fileno << std::endl;
   int idEvent;



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


   int 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 idEvent;
   std::vector<double> *energy=0 ;
   std::vector<double> *time=0 ;

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

   const int n=9;
   TH1F *peakno[n];
   TH1F *intno[n];
   TH1F *inttotal= new TH1F ("inttotal","inttotal",100,0,100);
   int eventno;

   for (int i=0;i<n;i++) {
      peakno[i] = new TH1F(Form("peakno%d",i),Form("peakno%d",i),20000,0,20000);
      intno[i]  = new TH1F(Form("intno%d",i),Form("intno%d",i),100,0,100);
   }

   int N = tree->GetEntries();

   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 && idEvent<=8) {
         for (ULong_t j=0; j<nsig; j++) {
           if (esig[j] < 6.0e-6 ) continue;
            peakno[idEvent]->Fill(sig[j]);
         }
      }
   }

   for (int i=0;i<n;i++) {
           if (peakno[i]->GetMaximum()>50){
      TSpectrum *s = new TSpectrum(npeaks);
      int nfound = s->Search(peakno[i],0.0,"",0.05);
      intno[i]->Fill(nfound);
      std::cout <<"Event No = " << fileno*10+i << " :  No of Peaks = " << nfound <<   std::endl;
      inttotal->Add(intno[i]);
   }
   }

   TFile *outputFile = new TFile(outFilename.c_str(),"RECREATE");
  std::cout << "Output file written to " << outFilename << std::endl;
for (int i=0;i<n;i++) {
           if (peakno[i]->GetMaximum()>50){
      peakno[i]->Write();
      intno[i]->Write();
   }
   }
   inttotal->Write();
   outputFile->Close();
}
     ```

And I am trying to attach input file but it is too big.

a few sensors files are enough. 2 or 3

One single sensor file is not going…

Sir, the code is working with multiple inputs . I used #!/bin/bash file.

1 Like

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