updated root script

This commit is contained in:
James Szalkie 2026-04-20 13:37:31 -04:00
parent 38ac66a721
commit fe112a6004
3 changed files with 65 additions and 10 deletions

View File

@ -8,9 +8,18 @@
#include "TBranch.h" #include "TBranch.h"
#include <iostream> #include <iostream>
void aarootscript() { void aarootscript(int argument = 0) {
std::cout << "\n\n\n";
std::cout << "=========================================\n";
std::cout << "========= ANASEN Root Script =========\n";
std::cout << "=========================================\n";
TFile *f = new TFile("SimAnasen1.root"); TFile *f = new TFile("SimAnasen1.root");
TTree *tree1 = (TTree*)f->Get("tree1"); TTree *tree1 = (TTree*)f->Get("tree");
if (!tree1) {
std::cerr << "Error: tree1 not found in the file!" << std::endl;
return;
}
TTree *tree2 = (TTree*)f->Get("tree2"); TTree *tree2 = (TTree*)f->Get("tree2");
TTreeReader reader("tree"); TTreeReader reader("tree");
@ -24,11 +33,42 @@ void aarootscript() {
TTreeReader reader2("tree2"); TTreeReader reader2("tree2");
TTreeReaderValue<double> branchValue2(reader2, "Tb"); TTreeReaderValue<double> branchValue2(reader2, "Tb");
double totalSum2 = 0; double totalSum2 = 0;
int zeroCount = 0;
while (reader2.Next()) { while (reader2.Next()) {
totalSum2 += *branchValue2; totalSum2 += *branchValue2;
if (*branchValue2 == 0) {
zeroCount++;
}
} }
std::cout << "Total Sum: " << totalSum2 << std::endl; std::cout << "Total Sum: " << totalSum2 << std::endl;
std::cout << "Difference: " << totalSum - totalSum2 << std::endl; std::cout << "Difference: " << totalSum - totalSum2 << std::endl;
std::cout << "Zero Count: " << zeroCount << std::endl;
std::cout << "Making histograms..." << std::endl;
gErrorIgnoreLevel = 2001;
gROOT->ProcessLine(".x histcomp.C");
std::cout << "=========================================\n";
if (argument == 1) {
if (tree1) {
gROOT->ProcessLine("tree->Print();");
} else {
std::cout << "Tree1 not found!" << std::endl;
}
}
if (argument == 2) {
if (tree2) {
gROOT->ProcessLine("tree2->Print();");
} else {
std::cout << "Tree2 not found!" << std::endl;
}
}
std::cout << "\n\n\n";
} }

View File

@ -44,6 +44,7 @@ int main(int argc, char **argv){
c1->SetLogy(); c1->SetLogy();
c1->SetLogx(); c1->SetLogx();
c1->Print("eloss_light.png"); c1->Print("eloss_light.png");
int ELossTotal = 0;
auto g2 = elossHeavy; auto g2 = elossHeavy;
g2->SetTitle("Energy Loss Table;Kinetic Energy (MeV);dE/dx (MeV/(mg/cm^{2}))"); g2->SetTitle("Energy Loss Table;Kinetic Energy (MeV);dE/dx (MeV/(mg/cm^{2}))");
@ -299,8 +300,6 @@ int main(int argc, char **argv){
tree->Fill(); tree->Fill();
//Energy loss //Energy loss
double dl = (hitPos - vertex).Mag() / 10; // path length in cm (positions in mm) double dl = (hitPos - vertex).Mag() / 10; // path length in cm (positions in mm)
//double EkinLight = Pb.E() - Pb.M();// kinetic energy of light particle before loss
//double dedxLight = elossLight->Eval(EkinLight); // interpolate dE/dx
if (numEvent <= 10){ if (numEvent <= 10){
printf("Event %d: Ekin before loss = %f MeV, distance = %f cm\n", i, Tb, dl); printf("Event %d: Ekin before loss = %f MeV, distance = %f cm\n", i, Tb, dl);
} }
@ -321,12 +320,19 @@ int main(int argc, char **argv){
dx += step; dx += step;
counter++; counter++;
Tb = Tb - dE_step; // update kinetic energy for tree storage Tb = Tb - dE_step; // update kinetic energy for tree storage
if (Tb < 0) {
Tb = 0; // prevent negative kinetic energy
break;
}
} }
// fill tree2 with energy loss adjusted data // fill tree2 with energy loss adjusted data
if (Tb != 0) {
tree2->Fill(); tree2->Fill();
}
if (numEvent <= 10){ if (numEvent <= 10){
printf("Event %d: Tb after energy loss = %f MeV, energy loss = %f MeV\n", i, Tb, tb_temp - Tb); printf("Event %d: Tb after energy loss = %f MeV, energy loss = %f MeV\n", i, Tb, tb_temp - Tb);
} }
ELossTotal += (tb_temp - Tb);
}else{ }else{
// no valid SX3 hit: mark clearly invalid // no valid SX3 hit: mark clearly invalid
@ -344,7 +350,7 @@ int main(int argc, char **argv){
reTheta1 = TMath::QuietNaN(); reTheta1 = TMath::QuietNaN();
rePhi1 = TMath::QuietNaN(); rePhi1 = TMath::QuietNaN();
z0 = TMath::QuietNaN(); z0 = TMath::QuietNaN();
//Tb = -12354567; // mark kinetic energy as invalid for no hit case
// fill tree with original data (no energy loss for these events) // fill tree with original data (no energy loss for these events)
//tree->Fill(); //tree->Fill();
//tree2->Fill(); //tree2->Fill();
@ -379,6 +385,7 @@ int main(int argc, char **argv){
saveFile->Close(); saveFile->Close();
printf("=============== done. saved as %s. tree entries: %d, tree2 entries: %d\n", saveFileName.Data(), count, count2); printf("=============== done. saved as %s. tree entries: %d, tree2 entries: %d\n", saveFileName.Data(), count, count2);
printf("Average energy loss for light particle: %f MeV\n", (double)ELossTotal / count);
if(enableVis){ if(enableVis){
printf("Displaying geometry with %zu tracks from simulation\n", visTrackVertex.size()); printf("Displaying geometry with %zu tracks from simulation\n", visTrackVertex.size());

View File

@ -26,7 +26,7 @@ void histcomp() {
TBranch *br = (TBranch*)branches->At(i); TBranch *br = (TBranch*)branches->At(i);
TString name = br->GetName(); TString name = br->GetName();
printf("Processing branch: %s\n", name.Data()); //printf("Processing branch: %s\n", name.Data());
// Create histograms (auto-range using Draw first) // Create histograms (auto-range using Draw first)
TString h1name = "h1_" + name; TString h1name = "h1_" + name;
@ -34,8 +34,8 @@ void histcomp() {
// Temporary draw to get range // Temporary draw to get range
tree1->Draw(name, "", "goff"); tree1->Draw(name, "", "goff");
double min = tree1->GetMinimum(name); double min = fmax(tree1->GetMinimum(name), tree2->GetMinimum(name));
double max = tree1->GetMaximum(name); double max = fmin(tree1->GetMaximum(name), tree2->GetMaximum(name));
//if (min == max) continue; // skip constant branches //if (min == max) continue; // skip constant branches
@ -95,6 +95,14 @@ void histcomp() {
// Save plot (overwrite each run) // Save plot (overwrite each run)
TString filename = "plots/" + name + ".png"; TString filename = "plots/" + name + ".png";
c->SaveAs(filename); c->SaveAs(filename);
//c->SetLogx(0);
//c->SetLogy(0);
// Optional: save log plots as well
c->SetLogx(1);
c->SetLogy(1);
h1->SetTitle(name + " (log);"+name+";Counts");
c->SaveAs("plots/" + name + "_log.png");
// Clean up // Clean up
delete c; delete c;