#include "TString.h" #include "TFile.h" #include "TTree.h" void improvedrootreader() { TString filename; TString treename; char yesOrNo; double branchNum; TString newFile, treeName, treeComment; cout << "Enter file name : "; cin >> filename; TFile file(Form("%s\.root",filename.Data())); cout << "Opening file : "<< filename << endl; cout << "Listing objects within this file." << endl; file.ls(); cout << "Is there a TTree listed? (y/n) : "; cin >> yesOrNo; if (yesOrNo == 'n') { break; } else if (yesOrNo == 'y') { cout << "Enter TTree name : "; cin >> treename; } else { cout << "Invalid input. Terminating." << endl; break; } cout << "Printing contents of "<< treename << endl; TTree* tree = (TTree*)file.Get(treename); tree->Print(); cout << "Do you want to make a new TTree? (y/n) : "; cin >> yesOrNo; if (yesOrNo == 'n') { break; } else if (yesOrNo == 'y') { cout << "Enter name of new file : " ; cin >> newFile; //cout << "Enter name of new tree : "; //cin >> treeName; //cout << "Enter any comments : "; //cin >> treeComment; cout << "Enter number of new branches (max of ten) : "; cin >> branchNum; } else { cout << "Invalid input. Terminating." << endl; break; } // Unfortunately it is not possible to have user defined inputs for the branches. // To dynamical, but using it to list files, print trees etc, is doable form user input //TString vars[branchNum]; TFile newfile(Form("%s\.root",newFile.Data()), "RECREATE"); TTree newtree("newtree","a new tree"); //Define 5 branches for use, so they initialize empty. TBranch* b[10]; for (int i =0; i < branchNum; i++) { cout << "Type variable name :(type n to stop) "; TString varName; cin >> varName; if (varName != 'n') { TString varNameR = "&"+varName ; tree->SetBranchAddress(Form("%s",varName.Data()), Form("&%s",varName)); b[i] = newtree.Branch(Form("%s",varName.Data()), Form("%s",varNameR.Data()), i); // vars[i] = "TBranch* " + varName +" = newtree.Branch(" + varName + ", &" + varName +", i)"; //cout << vars[i] << endl; //vars[i]; } if (varName == 'n') break; } for(int i=0; i < tree->GetEntries(); i++) { tree->GetEntry(i); newtree.Fill(); } cout << "End of code : " << newtree->GetEntries() << endl; newfile.Write(); }