// Run with program with // // root DrawHistograms.cpp #include "TFile.h" #include "TH1F.h" #include "TVector3.h" #include "TTreeReader.h" #include "TTreeReaderValue.h" void SameHisto() { // PRESETTINGS // Set path to root file std::string myPath = "../analysis/"; // Set name of root file std::string myFileName = ("Out_run_0.root", "Out_run_1.root"); // Open the file containing the tree TFile *myFile = TFile::Open(std::string(myPath+myFileName).c_str()); // Create a TTreeReader for the tree by passing the TTree's name and the TFile it is in TTreeReader myReader("Secondaries", myFile); // The branch "ParticleEnergy" contains a vector; access them as vEnergies TTreeReaderValue> vEnergies(myReader, "ParticleEnergy"); //The branch "ParticleID" contains a vector; access them as vID TTreeReaderValue> vPDGcode (myReader, "ParticleID"); // Create histograms for the values we read // A histogram for the energies ranging from 0 MeV to 20 MeV with 200 bins TH1F* h1EnergyAll = new TH1F("h1EnergyAll", ";E in MeV;#entries", 20, 0, 20); TH1F* h1EnergyGamma = new TH1F("h1EnergyGamma", ";E in MeV;#entries", 20, 0, 20); TH1F* h2EnergyAll = new TH1F("h2EnergyAll", ";E in MeV;#entries", 20, 0, 20); TH1F* h2EnergyGamma = new TH1F("h2EnergyGamma", ";E in MeV;#entries", 20, 0, 20); // READ THE TREE // Loop over all entries of the TTree. while (myReader.Next()) { int energySize = vEnergies->size(); for(int i = 0; iat(i); int myPDG= vPDGcode->at(i); h1EnergyAll->Fill(myEnergy); h2EnergyAll->Fill(myEnergy); if (myPDG==22){ h1EnergyGamma->Fill(myEnergy); h2EnergyGamma->Fill(myEnergy); } } } // DRAW THE HISTOGRAM // Energy Histogram TCanvas* cEnergy = new TCanvas(); cEnergy->cd(); h1EnergyGamma->Draw(); h2EnergyGamma->Draw("same"); //h1EnergyGamma->SetLineColor(kRed); //hEnergyGamma->Draw("same"); //h1EnergyGamma->SetLineColor(KRed); cEnergy->Update(); cEnergy->SaveAs("../images/EnergySpectrum.pdf"); }