Commit 2d556f25 authored by Keerthi Nakkalil's avatar Keerthi Nakkalil
Browse files

Efficient and better code. The TP files are read once to store the values in a tree

parent 5bd07819
//TFile* TPfile = new TFile("TP.root","update");
//TList* TPlist = new TList;
void TreeEnergyWrite(){
......@@ -29,33 +26,30 @@ void TreeEnergyWrite(){
}
void TreeTPWrite(int c, int r){
std::cout << " Looking at pixel " << c << "_" << r << std::endl;
int energy, TPcol1, TPcol2, TPcol3;
double voltage,tot, tot_var, toa, toa_var;
void TreeTPWrite(){
int energy, col, row, pc;
double voltage,tot, tot_var, toa, toa_var, time_ref;
string line_TP;
TFile* Efile = new TFile("Energy_table.root");
TTree* ETree = (TTree*)Efile->Get("ETree");
ETree->SetBranchAddress("Ecol1",&energy);
ETree->SetBranchAddress("Ecol4",&voltage);
TFile* TPfile = new TFile("TP.root","update");
std::string TPtreeName = "TPtree" + std::to_string(c) + "_" + std::to_string(r);
TTree* TPtree = new TTree(TPtreeName.c_str()," TP tree");
// TTree* Mergedtree = new TTree("MergedTree", "Merged trees");
// Mergedtree->SetDirectory(TPfile);
TPtree->Branch("TPcol1",&TPcol1,"TPcol1/I");
TPtree->Branch("TPcol2",&TPcol2,"TPcol2/I");
TPtree->Branch("TPcol3",&TPcol3,"TPcol3/I");
TFile* TPfile = new TFile("TP.root","update");
TTree* TPtree = new TTree("TPtree"," TP tree");
TPtree->Branch("col",&col,"col/I");
TPtree->Branch("row",&row,"row/I");
TPtree->Branch("pc",&pc,"pc/I");
TPtree->Branch("tot",&tot,"tot/D");
TPtree->Branch("tot_var",&tot_var,"tot_var/D");
TPtree->Branch("toa",&toa,"toa/D");
TPtree->Branch("toa_var",&toa_var,"toa_var/D");
ETree->SetBranchAddress("Ecol1",&energy);
ETree->SetBranchAddress("Ecol4",&voltage);
TPtree->Branch("time_ref",&time_ref,"time_ref/D");
auto voltageBranch = TPtree->Branch("voltage",&voltage,"voltage/D");// make a new branch and connect it to the same variable as the branch address of the energy tree
for(int i = 0; i < ETree->GetEntries(); i++){
ETree->GetEntry(i);
......@@ -64,68 +58,36 @@ void TreeTPWrite(int c, int r){
//std:: cout << " The TPfilenames are : " << TPfilename.c_str() <<std::endl;
std::ifstream TPdatafile(TPfilename.c_str());
voltageBranch->Fill();
if(TPdatafile.is_open()){
std::cout << " The data file " << TPfilename.c_str() << " is opened ..." <<std::endl;
// make a new branch and connect it to the same variable as the branch address of the energy tree
auto voltageBranch = TPtree->Branch("voltage", &voltage, "voltage/D");
// reading through individial tp_dat_ files
while(getline(TPdatafile,line_TP)){
if(line_TP.rfind("#", 0) == 0) {
continue;
}
istringstream ssTP(line_TP);
ssTP >> TPcol1 >> TPcol2 >> TPcol3 >> tot >> tot_var >> toa >> toa_var;
if(TPcol1==c && TPcol2 ==r){
if(line_TP.rfind("#", 0) == 0) {
continue;
}
istringstream ssTP(line_TP);
ssTP >> col >> row >> pc >> tot >> tot_var >> toa >> toa_var >> time_ref;
// std:: cout << col << " " << row << " " << pulsecount << " " << toa << " " << toa_var << std::endl;
TPtree->Fill();
voltageBranch->Fill();
break;
}
} //While loop getline
}//if statement TPdatafile open
}//For loop GetEntries
//Save only the new version of the tree
// TPtree->Write("",TObject::kOverwrite);
// TPtree->Write("",TObject::kWriteDelete);
// TPtree->Write();
// TPtree->Scan();
//Add new trees (incl voltage col) to list and merge the trees
TList* TPlist = new TList;
std::cout << " The tree for the pixel " << c << " and " << r << " is added to the list " << std::endl;
TPlist->Add(TPtree);
TTree* newtree = TTree::MergeTrees(TPlist);
newtree->SetName("MergedTree");
// TTree* Mergedtree = new TTree("MergedTree", "Merged trees");
newtree->SetDirectory(TPfile);
// Mergedtree = TTree::MergeTrees(TPlist);
// Mergedtree->Write("",TObject::kWriteDelete);
newtree->Write("",TObject::kWriteDelete);
TPtree->Write("",TObject::kOverwrite);
TPfile->Close();
//Efile->Close();
}
void TreeTest(){
void TreeTest(){
int col, row ;
TreeEnergyWrite();
for(col=0;col<2;col++){
for(row=0;row<2;row++){
TreeTPWrite(col,row);
}
}
//std::cout <<" The size of the tree list is " << TPlist->GetSize() << std::endl;
//TPfile->Close();
TreeTPWrite();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment