BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
renameTree.C
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2// //
3// renameTree.C //
4// //
5// An example macro how to rename Tree in the ROOT file //
6// //
7////////////////////////////////////////////////////////////////////////////////
8
9int renameTree(TString filePath = "",
10 TString oldName = "cbmsim",
11 TString newName = "bmndata",
12 TString newTitle = "bmndigit")
13{
14 if (filePath == "") {
15 cout << "ERROR: File path was not specified" << endl;
16 return -1;
17 }
18
19 gSystem->ExpandPathName(filePath);
20
21 TFile* rootFile = new TFile(filePath.Data(), "UPDATE");
22 if (!rootFile) {
23 cout << "ERROR: File was not found: " << filePath << endl;
24 return -2;
25 }
26
27 TTree* oldTree = (TTree*)rootFile->Get(oldName.Data());
28 if (!oldTree) {
29 cout << "ERROR: Tree was not found: " << oldName << endl;
30 return -3;
31 }
32
33 // oldTree->SetNameTitle(newName.Data(), newName.Data());
34 oldTree->SetObject(newName.Data(), newTitle.Data());
35 gDirectory->Delete(TString::Format("%s;*", oldName.Data()));
36
37 rootFile->Write();
38 rootFile->Close();
39
40 delete rootFile;
41 return 0;
42}
43
44int renameTree_dir(TString dirPath = "",
45 TString oldName = "cbmsim",
46 TString newName = "bmndata",
47 TString newTitle = "bmndigit")
48{
49 if (dirPath == "") {
50 cout << "ERROR: Directory path was not specified" << endl;
51 return -1;
52 }
53
54 gSystem->ExpandPathName(dirPath);
55
56 TSystemDirectory dir(dirPath.Data(), dirPath.Data());
57 TList* file_list = dir.GetListOfFiles();
58 if (file_list == NULL) {
59 cout << "WARNING: There are no files in the directory: " << dirPath << endl;
60 return 0;
61 }
62
63 int rename_count = 0;
64 TSystemFile* cur_file;
65 TIter iterFile(file_list);
66 while ((cur_file = (TSystemFile*)iterFile.Next())) {
67 TString file_name = cur_file->GetName();
68 if ((file_name == ".") || (file_name == ".."))
69 continue;
70
71 TString file_dir = cur_file->GetTitle();
72 if (cur_file->IsDirectory()) {
73 // NOW - do nothing
74 // del_files_not_in_db(file_dir, period_number, limit_size_in_mb, isOnlyShow);
75 } // if directory
76 else
77 {
78 TString file_path = file_dir + "/" + file_name;
79 if (file_name.EndsWith(".root")) {
80 int error_code = renameTree(file_path, oldName, newName, newTitle);
81 if (error_code == 0)
82 rename_count++;
83 } // if (file_name.EndsWith(".root"))
84 } // if not a directory
85 } // while file list
86
87 if (rename_count > 0)
88 cout << endl
89 << rename_count << " files have been successfully processed where the tree was renamed (dir: " << dirPath
90 << ")" << endl;
91
92 return 0;
93}
#define gDirectory
R__EXTERN TSystem * gSystem
T * Get(const char *namecycle)
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
static TString Format(const char *fmt,...)
virtual TList * GetListOfFiles() const
virtual Bool_t IsDirectory(const char *dir=nullptr) const
virtual void SetObject(const char *name, const char *title)
int renameTree(TString filePath="", TString oldName="cbmsim", TString newName="bmndata", TString newTitle="bmndigit")
Definition renameTree.C:9
int renameTree_dir(TString dirPath="", TString oldName="cbmsim", TString newName="bmndata", TString newTitle="bmndigit")
Definition renameTree.C:44