ROOT Version: 6.18
Platform: macOS Catalina
Compiler: gcc
I am trying to run a simulation and some reconstruction of the simulated data. I do this in two different pieces of code - that is in one piece of code I create a root file called GeneratedSignals.root with a TTree called SigTree, which I then read in a second piece of code using TTree *readtree = (TTree*)file->Get("SigTree");
After I’ve done the reconstruction I want to save the reconstructed data to new branches in the same TTree as before so that I can then directly compare “truth” level and reconstructed data in a separate macro.
I write the TTree using readtree->Write(nullptr,TObject::kOverwrite);
but it won’t overwrite the new branches, meaning that every time I change something I have to rerun both pieces of code.
I feel there must surely be a way of stopping this from happening but I haven’t yet found anything on here that helps.
Here’s a shortened version of the problematic code, and I’m attaching a dropbox link to GeneratedSignals.root as it is immediately after creation with only the truth level values in it:
(https://www.dropbox.com/s/ko49ftskopyrxi6/GeneratedSignals.root?dl=0)
#include <iostream>
#include <vector>
#include "TFile.h"
#include "TTree.h"
#define NPOINTS 1024
#define BinsPerNs 1024./400.
#define NoEventsToPrint 10
Int_t fProcessing = 2;
int main(Int_t argc, char **argv){
TFile *file = TFile::Open("GeneratedSignals.root","update");
if (!file) {
std::cout<<"No file named GeneratedSignals.root found"<<std::endl;
return 0;
}
TTree *readtree = (TTree*)file->Get("SigTree");
//file->GetObject("SigTree",readtree);
Int_t TotEvents=readtree->GetEntries();
Double_t sigfinal[1024];//Final signal - what the DAQ would register, so the sum of all individual hits
Double_t sigProc[NPOINTS];
Int_t EventIndex;//Index of event
Int_t HitsPerEvent;//Number of hits per event
Int_t tbins[NPOINTS];//Time in bins
std::vector<Int_t> *HitTimes = 0;//Hit arrival time in bins
std::vector<Double_t> *HitAmp = 0;//Hit amplitude
//read branches written in SignalGeneratorToy.cpp
readtree->SetBranchAddress("SigFinal",sigfinal);
readtree->SetBranchAddress("EventNo",&EventIndex);
readtree->SetBranchAddress("HitsPerEvent",&HitsPerEvent);
readtree->SetBranchAddress("HitTimes",&HitTimes);//Hit arrival time in bins
readtree->SetBranchAddress("HitAmplitude",&HitAmp);
readtree->SetBranchAddress("TBins",&tbins);//Runs from 1 to 1024
std::vector<Double_t> *tProcRecoBins= new std::vector<Double_t>; //Reconstructed hit times from TSpectrum on raw data
std::vector<Double_t> *tUnProcRecoBins =new std::vector<Double_t>; //Reconstructed hit times from TSpectrum on RRC processed data
Int_t NoRecoUnProc=0;//Number of hits reconstructed by TSpectrum without digital processing
Int_t NoRecoProc=0;//Number of hits reconstructed by TSpectrum from digitally processed signals
//create new branch for reconstructed time
TBranch *BTRecoUnProc=readtree->Branch("TRecoUnProc","vector<Double_t>",&tUnProcRecoBins);
TBranch *BTRecoProc =readtree->Branch("TRecoProc","vector<Double_t>",&tProcRecoBins);
TBranch *BNoRecoUnProc=readtree->Branch("NoRecoUnProc",&NoRecoUnProc,"NoRecoUnProc/I");
TBranch *BNoRecoProc=readtree->Branch("NoRecoProc",&NoRecoProc,"NoRecoProc/I");
readtree->Print();
std::cout<<TotEvents<<std::endl;
for(Int_t eventno=0;eventno<TotEvents;eventno++){
readtree->GetEntry(eventno);
tProcRecoBins->clear();
tUnProcRecoBins->clear();
//Changing these variables and rerunning only this piece of code doesn't update the TTree
NoRecoUnProc = 10;
tUnProcRecoBins->push_back(6);
if(fProcessing==2){
NoRecoProc = 20;
tProcRecoBins->push_back(12);
}
if(fProcessing==1&&eventno%100==0) std::cout<<"Event "<<eventno<<" HitsPerEvent "<<HitsPerEvent<<" NoRecoUnProc "<<NoRecoUnProc<<std::endl;
if(fProcessing==2&&eventno%100==0) std::cout<<"Event "<<eventno<<" HitsPerEvent "<<HitsPerEvent<<" NoRecoUnProc "<<NoRecoUnProc<<" NoRecoProc "<<NoRecoProc<<std::endl;
BNoRecoUnProc->Fill();
BNoRecoProc->Fill();
BTRecoUnProc->Fill();
BTRecoProc->Fill();
}
readtree->Write(nullptr,TObject::kOverwrite);
delete file;
}