Fork me on GitHub

Opened 7 years ago

Closed 7 years ago

Last modified 4 years ago

#1173 closed How to (fixed)

how to access and fill particle.pt

Reported by: Goko Owned by:
Priority: minor Milestone:
Component: Delphes code Version: Delphes 3
Keywords: Cc:

Description

Hi all,
I want to access and fill histogram with particle pt.
I changed Example2.c a little bit.
Could you please help me what is wrong with it?

it gives this error:
* Break * segmentation violation

/*
Write this to the Terminal
root -l examples/Example2.C'("delphes_output.root")'
*/

#include "TH1.h"
#include "TSystem.h"

#ifdef __CLING__
R__LOAD_LIBRARY(libDelphes)
#include "classes/DelphesClasses.h"
#include "external/ExRootAnalysis/ExRootTreeReader.h"
#include "external/ExRootAnalysis/ExRootResult.h"
#endif

//------------------------------------------------------------------------------

struct MyPlots
{
  TH1 *ParticlePT;
};

//------------------------------------------------------------------------------

class ExRootResult;
class ExRootTreeReader;

//------------------------------------------------------------------------------

void BookHistograms(ExRootResult *result, MyPlots *plots)
{
  THStack *stack;
  TLegend *legend;
  TPaveText *comment;


  // book more histograms

  plots->fParticlePT = result->AddHist1D(
    "electron_pt", "electron P_{T}",
    "electron P_{T}, GeV/c", "number of electrons",
    50, 0.0, 100.0);

  // book general comment

  comment = result->AddComment(0.64, 0.86, 0.98, 0.98);
  comment->AddText("demonstration plot");
  comment->AddText("produced by Example2.C");

  result->Attach(plots->fParticlePT, comment);

}

//------------------------------------------------------------------------------

void AnalyseEvents(ExRootTreeReader *treeReader, MyPlots *plots)
{
  TClonesArray *branchParticle = treeReader->UseBranch("Particle");
  
  Long64_t allEntries = treeReader->GetEntries();

  cout << "** Chain contains " << allEntries << " events" << endl;

  Particle *particle;

  Long64_t entry;

  Int_t i;

  // Loop over all events
  for(entry = 0; entry < allEntries; ++entry)
  {
    // Load selected branches with data from specified event
    treeReader->ReadEntry(entry);

    // Loop over all electrons in event
    for(i = 0; i < branchParticle->GetEntriesFast(); ++i)
    {
      particle = (Particle*) branchParticle->At(i);
      plots->fParticlePT->Fill(particle->PT);
    }
  }
}

//------------------------------------------------------------------------------

void PrintHistograms(ExRootResult *result, MyPlots *plots)
{
  result->Print("png");
}

//------------------------------------------------------------------------------

void Example2(const char *inputFile)
{
  gSystem->Load("libDelphes");

  TChain *chain = new TChain("Delphes");
  chain->Add(inputFile);

  ExRootTreeReader *treeReader = new ExRootTreeReader(chain);
  ExRootResult *result = new ExRootResult();

  MyPlots *plots = new MyPlots;

  BookHistograms(result, plots);

  AnalyseEvents(treeReader, plots);

  PrintHistograms(result, plots);
 
  // write into a root file.

  result->Write("result.root");

  cout << "** Exiting..." << endl;

  delete plots;
  delete result;
  delete treeReader;
  delete chain;
}

//------------------------------------------------------------------------------

Attachments (1)

Example2.C (2.8 KB ) - added by Goko 7 years ago.

Download all attachments as: .zip

Change History (2)

by Goko, 7 years ago

Attachment: Example2.C added

comment:1 by Goko, 7 years ago

Resolution: fixed
Status: newclosed

I solved it.
Thank you.

Note: See TracTickets for help on using tickets.