BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
renameBranch.C
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2// //
3// renameBranch.C //
4// //
5// An example macro how to rename Branch in the ROOT file //
6// //
7////////////////////////////////////////////////////////////////////////////////
8
9int renameBranch(TString filePath = "", TString treeName = "bmndata", TString oldName = "VC", TString newName = "VETO")
10{
11 if (filePath == "") {
12 cout << "ERROR: File path was not specified" << endl;
13 return -1;
14 }
15
16 gSystem->ExpandPathName(filePath);
17
18 TFile* rootFile = new TFile(filePath.Data(), "UPDATE");
19 if (!rootFile) {
20 cout << "ERROR: File was not found: " << filePath << endl;
21 return -2;
22 }
23
24 TTree* oldTree = (TTree*)rootFile->Get(treeName.Data());
25 if (!oldTree) {
26 cout << "ERROR: Tree was not found: " << treeName << endl;
27 return -3;
28 }
29 TBranch* branch = nullptr;
30 branch = oldTree->GetBranch(oldName.Data());
31 if (branch)
32 branch->SetName(newName.Data());
33 // DigiRunHeader * rh = (DigiRunHeader *)rootFile->Get(oldObjectName.Data());
34 // rootFile->WriteObject(rh, newObjectName.Data());
35
36 TString oldObjectName = "BmnDigiRunHeader";
37 TString newObjectName = "DigiRunHeader";
38 TKey* k = nullptr;
39 k = rootFile->GetKey(oldObjectName.Data());
40 if (k) {
41 k->SetName(newObjectName.Data());
42 }
43
44 rootFile->Write();
45 rootFile->Close();
46
47 delete rootFile;
48 return 0;
49}
50
51int renameBranch_dir(TString dirPath = "",
52 TString treeName = "bmndata",
53 TString oldName = "VC",
54 TString newName = "VETO")
55{
56 if (dirPath == "") {
57 cout << "ERROR: Directory path was not specified" << endl;
58 return -1;
59 }
60
61 gSystem->ExpandPathName(dirPath);
62
63 TSystemDirectory dir(dirPath.Data(), dirPath.Data());
64 TList* file_list = dir.GetListOfFiles();
65 if (file_list == NULL) {
66 cout << "WARNING: There are no files in the directory: " << dirPath << endl;
67 return 0;
68 }
69
70 int rename_count = 0;
71 TSystemFile* cur_file;
72 TIter iterFile(file_list);
73 while ((cur_file = (TSystemFile*)iterFile.Next())) {
74 TString file_name = cur_file->GetName();
75 if ((file_name == ".") || (file_name == ".."))
76 continue;
77
78 TString file_dir = cur_file->GetTitle();
79 if (cur_file->IsDirectory()) {
80 // NOW - do nothing
81 // del_files_not_in_db(file_dir, period_number, limit_size_in_mb, isOnlyShow);
82 } // if directory
83 else
84 {
85 TString file_path = file_dir + "/" + file_name;
86 if (file_name.EndsWith("_digi.root")) {
87 int error_code = renameBranch(file_path, treeName, oldName, newName);
88 if (error_code == 0) {
89 printf("successful rename of %s\n", file_path.Data());
90 rename_count++;
91 }
92 } // if (file_name.EndsWith(".root"))
93 } // if not a directory
94 } // while file list
95
96 if (rename_count > 0)
97 cout << endl
98 << rename_count << " files have been successfully processed where the tree was renamed (dir: " << dirPath
99 << ")" << endl;
100
101 return 0;
102}
R__EXTERN TSystem * gSystem
T * Get(const char *namecycle)
TKey * GetKey(const char *name, Short_t cycle=9999) const override
Int_t Write(const char *name=nullptr, Int_t opt=0, Int_t bufsize=0) const override
void Close(Option_t *option="") override
TObject * Next()
const char * GetName() const override
const char * GetTitle() const override
virtual void SetName(const char *name)
const char * Data() const
virtual TList * GetListOfFiles() const
virtual Bool_t IsDirectory(const char *dir=nullptr) const
virtual TBranch * GetBranch(const char *name)
int renameBranch(TString filePath="", TString treeName="bmndata", TString oldName="VC", TString newName="VETO")
Definition renameBranch.C:9
int renameBranch_dir(TString dirPath="", TString treeName="bmndata", TString oldName="VC", TString newName="VETO")