Missing Entries in histogram

Dear Root Experts

I have simulation with 1K events but number of histogram entries is only 908, where are these 92 missing events? Please suggest what could be the reason?

Hi,

you probably have a continue statement somewhere between the start of the loop over all simulated events and the line where you fill your histogram.

Hi

 for (int i = 0; i < energy.GetSize(); i++)
      {
          int ix = x[i] - x_max;
          int iy = y[i] - y_max;
          float z_shifted = abs(z[i]) - zmin;
          int iz = z_shifted - z_max;
         // cout << nev << " " << i << " " << ix << " " << iy << " " << iz << endl;
          if (abs(ix) > 1 || abs(iy) > 1 || abs(iz) > 1) continue;

          //cout << ix << endl;
          vector<int> v = {ix, iy, iz};
          h1max[v]->Fill(energy[i]);
      }
    }

There is continue here this if is checking for the max bin Can I remove this continue

There you go, 92 elements of the energy object do not satisfy this criterion. No idea whether you can remove this requirement, you know your data much better than I.

Just in case check also energy.GetSize() is equal to 1000 :wink:

I check sometime Entries are more than 1000 for energy.Getsize()

my whole macro is here:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <stdlib.h>

#include "TROOT.h"
#include "TTree.h"
#include "TChain.h"
#include "TFile.h"
#include "TString.h"
#include "TSystem.h"
#include "TCanvas.h"
#include "TLegend.h"
#include "TH1.h"
#include "TH2.h"
#include "TH3.h"
#include "TColor.h"
#include "Riostream.h"
#include "TDatime.h"
#include "TMath.h"
#include "TStyle.h"
#include "TLorentzVector.h"

#include <TTreeReader.h>
#include <TTreeReaderValue.h>
#include <TTreeReaderArray.h>


stringstream ss;

void celldepC()
{
    // *************************************************************************
    // Rootfile and Tree
    TFile* f = new TFile("lumidet_PbWO4_1K.edm4hep.root");
    f->cd();

    //TTree* t = (TTree*)f->Get("events");
    TTreeReader myReader("events", f);                     // name of tree and file

    TTreeReaderArray<Float_t> energy(myReader, "LumiDirectPCALHits.energy");
    TTreeReaderArray<Float_t> x(myReader, "LumiDirectPCALHits.position.x");
    TTreeReaderArray<Float_t> y(myReader, "LumiDirectPCALHits.position.y");
    TTreeReaderArray<Float_t> z(myReader, "LumiDirectPCALHits.position.z");
    // Rootfile and Tree
    // *************************************************************************
    // *************************************************************************
    // Histograms
    map<string,TH1*> h1;
    map<string,TH2*> h2;
    map<string,TH3*> h3;

    float xmin = -50 - 0.5;
    float xmax = 50 + 0.5;
    int nx = 100 + 1;
    float ymin = -50 - 0.5;
    float ymax = 50 + 0.5;
    int ny = 100 + 1;
    float zmin = 66000 - 0.5;
    float zmax = 66300 + 0.5;
    int nz = 300 + 1;

    // 3D histogram x, y, z. total energy deposit as T axis
    h3["energy"] = new TH3F("energy", "Total Energy Deposit;x;y;z", nx, xmin, xmax, ny, ymin, ymax, nz, 0, zmax - zmin);

    // Histograms
    // *************************************************************************

    // *************************************************************************
    // Event Loop
    // The first for loop to fill 3D histogram
   // int nev_max = 10000;
    int nev = 0;
    /*while (myReader.Next())
    {
       // if (nev >= nev_max) break;
        nev++;
        if (nev % 100 == 0) cout << "Event: " << nev << endl;

        float tot_e = 0;
        for (int i = 0; i < energy.GetSize(); i++)
        {
            tot_e += energy[i];
        }
    }*/
    // Event Loop
    // *************************************************************************
    // *************************************************************************
    map<vector<int>,TH1*> h1max;

    // *************************************************************************
    // Plot Energy spectrum of the max bin
    // 27 X 1D histograms around and of the bin with the max energy deposit
    for (int ix = -1; ix <= 1; ix++)
    {
        for (int iy = -1; iy <= 1; iy++)
        {
            for (int iz = -1; iz <= 1; iz++)
            {
                ss.str(""); ss << "hist_" << ix << "_" << iy << "_" << iz;
                vector<int> v = {ix,iy,iz};
                h1max[v] = new TH1F(TString(ss.str()), "", 100, 0., 0.1);
            }
        }
    }
    
    int nevts_nohits = 0;
    myReader.Restart();
    while (myReader.Next()) {
      // Identify Max Energy Bin
      // *************************************************************************
      float e_max = 0;
      float x_max = -999;
      float y_max = -999;
      float z_max = -999;
      if (energy.GetSize() == 0)
        nevts_nohits++;

      for (int i = 0; i < energy.GetSize(); ++i) {
        if (energy[i] > e_max) {
          e_max = energy[i];
          x_max = x[i];
          y_max = y[i];
          z_max = abs(z[i]) - zmin;
        }
      }
      cout << "max-E : " << e_max << endl;
      cout << "max-X : " << x_max << endl;
      cout << "max-Y : " << y_max << endl;
      cout << "max-Z : " << z_max << endl;
      cout << "Entries: "<< energy.GetSize() <<endl;
      nev++;
      if (nev % 100 == 0) cout << "Event: " << nev << endl;

      for (int i = 0; i < energy.GetSize(); i++)
      {
          int ix = x[i] - x_max;
          int iy = y[i] - y_max;
          float z_shifted = abs(z[i]) - zmin;
          int iz = z_shifted - z_max;
         // cout << nev << " " << i << " " << ix << " " << iy << " " << iz << endl;
          if (abs(ix) > 1 || abs(iy) > 1 || abs(iz) > 1) continue;

          //cout << ix << endl;
          vector<int> v = {ix, iy, iz};
         h1max[v]->Fill(energy[i]);
         
      }
    }
    cout << "Total number of events: " << nev << ", without hits: " << nevts_nohits << endl;
    gStyle->SetOptStat(111111);
    // Loop again
    // *************************************************************************
    for (map<vector<int>, TH1*>::const_iterator itr = h1max.begin(); itr != h1max.end(); itr++)
    {
        int ix = itr->first[0];
        int iy = itr->first[1];
        int iz = itr->first[2];
        TCanvas* c = new TCanvas("c", "", 1000, 1000);
        c->cd();
        itr->second->Draw("");

        ss.str(""); ss << "test_" << ix << "_" << iy << "_" << iz << ".png";
        c->SaveAs(TString(ss.str()));
        c->Close();
    }

}

Not sure I understand the question at this point. We figured you have a continue statement, and some entries get lost because of it. What do you want to do next?

Actually when I remove continue statement I see 0 entry in the histogram

You create 3*3*3 histograms in “h1max”. It looks like 908 entries go into “h_0_0_0” and the remaining ones into other histograms (or are lost if “ix”, or “iy”, or “iz” are “incorrect”).

Kindly suggest better way to make these histograms with all entries

You draw all histograms, so sum up their displayed numbers of entries.

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