File indexing completed on 2025-07-01 08:59:04
0001 #include "TFile.h"
0002 #include "TH2D.h"
0003 #include "TTreeReader.h"
0004 #include "TTreeReaderArray.h"
0005 #include <cmath>
0006 #include <iostream>
0007
0008
0009
0010
0011
0012
0013
0014
0015 void example_macro(TString inFileName = "input.root",
0016 TString outFileName = "ana.root") {
0017
0018
0019 TFile* file = new TFile(inFileName);
0020 if (!file || file->IsZombie()) {
0021 std::cerr << "Error: Cannot open input file " << inFileName << std::endl;
0022 return;
0023 }
0024
0025
0026 TTreeReader myReader("events", file);
0027
0028
0029 TTreeReaderArray<double> px_mc(myReader, "MCParticles.momentum.x");
0030 TTreeReaderArray<double> py_mc(myReader, "MCParticles.momentum.y");
0031 TTreeReaderArray<double> pz_mc(myReader, "MCParticles.momentum.z");
0032 TTreeReaderArray<int> status(myReader, "MCParticles.generatorStatus");
0033 TTreeReaderArray<int> pdg(myReader, "MCParticles.PDG");
0034
0035
0036 TFile* output = new TFile(outFileName, "recreate");
0037 TH2D* hPtEtaMc = new TH2D("hPtEtaMc",
0038 "MC Particle Distribution;p_{t,gen} (GeV/c);#eta_{gen}",
0039 1500, 0.0, 10.0,
0040 200, -5.0, 5.0);
0041
0042
0043 int nEvents = myReader.GetEntries();
0044 std::cout << "Total Events: " << nEvents << std::endl;
0045
0046 for (int iEvent = 0; iEvent < nEvents; ++iEvent) {
0047 myReader.SetEntry(iEvent);
0048
0049
0050 if (iEvent % 1000 == 0) {
0051 std::cout << "Processing Event: " << iEvent << std::endl;
0052 }
0053
0054
0055 for (int iParticle = 0; iParticle < status.GetSize(); ++iParticle) {
0056
0057
0058 if (status[iParticle] != 1) {
0059 continue;
0060 }
0061
0062
0063 double px = px_mc[iParticle];
0064 double py = py_mc[iParticle];
0065 double pz = pz_mc[iParticle];
0066
0067
0068 double p_mc = std::sqrt(px*px + py*py + pz*pz);
0069
0070
0071 double pt_mc = std::sqrt(px*px + py*py);
0072
0073
0074 double theta_mc = std::acos(pz / p_mc);
0075 double eta_mc = -std::log(std::tan(theta_mc / 2.0));
0076
0077
0078
0079
0080
0081 hPtEtaMc->Fill(pt_mc, eta_mc);
0082
0083 }
0084
0085 }
0086
0087
0088 output->cd();
0089 hPtEtaMc->Write();
0090 output->Save();
0091 output->Close();
0092
0093
0094 file->Close();
0095 delete file;
0096 delete output;
0097
0098 std::cout << "Analysis complete. Output saved to: " << outFileName << std::endl;
0099 }