BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
show_root_stats.C
Go to the documentation of this file.
1// limits to define out-of-range values and to exclude them while calculating the averages
2#define MIN_FILE_SIZE_MB 1
3#define MAX_FILE_SIZE_GB 4
4#define MIN_EVENT_SIZE_KB 1
5#define MAX_EVENT_SIZE_KB 200
6
7// bool isTerminal = isatty(fileno(stdout));
8const string red("\033[0;91m"), orange("\033[0;33m"), cyan("\033[0;96m"), reset("\033[0m");
9
10class UniqueRunNumber;
11int calculate_size(TString dir_path,
12 const char* root_tree_name,
13 TH1F* hist_file_size,
14 TH1F* hist_event_size,
15 double& sum_file_size_mb,
16 double& sum_event_size_kb,
17 int& correct_file_count,
18 map<TString, double>& wrong_files,
19 map<TString, double>& wrong_events);
20
21/**
22 * Function for creating histograms on sizes of ROOT (digit) files stored in a specified directory amd events in the
23 * files
24 */
25int show_root_stats(TString dir_path, const char* root_tree_name = "bmndata")
26{
27 if (dir_path == "") {
28 cout << "ERROR: Directory path was not specified" << endl;
29 return -1;
30 }
31 gSystem->ExpandPathName(dir_path);
32
33 TH1F* hist_file_size =
34 new TH1F("hist_file_size", "ROOT file sizes", MAX_FILE_SIZE_GB * 1024 / 50, 0, MAX_FILE_SIZE_GB * 1024);
35 TAxis* xaxis1 = hist_file_size->GetXaxis();
36 xaxis1->SetTitle("ROOT FIle Size, MB");
37
38 TH1F* hist_event_size = new TH1F("hist_event_size", "Event sizes", MAX_EVENT_SIZE_KB / 5, 0, MAX_EVENT_SIZE_KB);
39 TAxis* xaxis2 = hist_event_size->GetXaxis();
40 xaxis2->SetTitle("Event Size, KB");
41
42 int correct_file_count = 0;
43 double sum_file_size_mb = 0, sum_event_size_kb = 0;
44 map<TString, double> wrong_files;
45 map<TString, double> wrong_events;
46 calculate_size(dir_path, root_tree_name, hist_file_size, hist_event_size, sum_file_size_mb, sum_event_size_kb,
47 correct_file_count, wrong_files, wrong_events);
48
49 if (correct_file_count > 0) {
50 double average_file_size = sum_file_size_mb / correct_file_count;
51 double average_event_size = sum_event_size_kb / correct_file_count;
52
53 cout << cyan << "-------------------------------------------------------" << endl;
54 cout << correct_file_count << " ROOT files have been successfully processed (dir: " << dir_path << ")" << endl;
55 cout << "Average ROOT file size: " << average_file_size << " MB" << endl;
56 cout << "Average event size: " << average_event_size << " KB" << endl;
57 cout << "-------------------------------------------------------" << endl << reset;
58
59 TCanvas* canvas_size = new TCanvas("canvas_size", "canvas_size", 1200, 640);
60 canvas_size->Divide(2, 1);
61 canvas_size->cd(1);
62 hist_file_size->Draw();
63 canvas_size->cd(2);
64 hist_event_size->Draw();
65 }
66
67 if (wrong_files.size() > 0) {
68 cout << endl
69 << "Wrong file size: (< " << MIN_FILE_SIZE_MB << " MB or > " << MAX_FILE_SIZE_GB << " GB):" << endl;
70 for (auto it = wrong_files.begin(); it != wrong_files.end(); ++it)
71 cout << "File '" << (*it).first << "': " << (*it).second << " MB" << endl;
72 cout << red;
73 cout << "Total wrong file size: " << wrong_files.size() << endl;
74 cout << reset;
75 }
76 cout << endl;
77 if (wrong_events.size() > 0) {
78 cout << endl
79 << "Wrong event size: (< " << MIN_EVENT_SIZE_KB << " KB or > " << MAX_EVENT_SIZE_KB << " KB):" << endl;
80 for (auto it = wrong_events.begin(); it != wrong_events.end(); ++it)
81 cout << "File '" << (*it).first << ": " << (*it).second << " KB" << endl;
82 cout << red;
83 cout << "Total wrong event size: " << wrong_events.size() << endl;
84 cout << reset;
85 }
86 cout << endl;
87
88 return 0;
89}
90
91// non-user function for creating histograms on root file sizes and stored event size for a given directory
92// (recursively) returns the success code: 0 - without errors, error code - otherwise
94 const char* root_tree_name,
95 TH1F* hist_file_size,
96 TH1F* hist_event_size,
97 double& sum_file_size_mb,
98 double& sum_event_size_kb,
99 int& correct_file_count,
100 map<TString, double>& wrong_files,
101 map<TString, double>& wrong_events)
102{
103 TSystemDirectory dir(dir_path.Data(), dir_path.Data());
104 TList* file_list = dir.GetListOfFiles();
105 if (file_list == nullptr) {
106 cout << "WARNING: There are no files in the directory: " << dir_path << endl;
107 return 0;
108 }
109
110 TSystemFile* cur_file;
111 TIter iterFile(file_list);
112 while ((cur_file = (TSystemFile*)iterFile.Next())) {
113 TString file_name = cur_file->GetName();
114 if ((file_name == ".") || (file_name == ".."))
115 continue;
116
117 TString file_dir = cur_file->GetTitle();
118 // if directory
119 if (cur_file->IsDirectory()) {
120 calculate_size(file_dir, root_tree_name, hist_file_size, hist_event_size, sum_file_size_mb,
121 sum_event_size_kb, correct_file_count, wrong_files, wrong_events);
122 } else {
123 TString file_path = file_dir + "/" + file_name;
124 if (file_name.EndsWith(".root")) {
125 Long64_t file_size_bytes = 0;
126 Long_t id = 0, flags = 0, modtime = 0;
127 gSystem->GetPathInfo(file_path.Data(), &id, &file_size_bytes, &flags, &modtime);
128
129 TFile root_file(file_path);
130 TTree* root_tree = (TTree*)root_file.Get(root_tree_name);
131 if (root_tree == nullptr) {
132 cout << "ERROR: there are no tree '" << root_tree_name << "' in file: " << file_path << endl;
133 root_file.Close();
134 continue;
135 }
136
137 double file_size_mb = file_size_bytes / 1024.0 / 1024.0;
138 double file_size_gb = file_size_mb / 1024.0;
139 int event_count = root_tree->GetEntries();
140 double event_size_kb = file_size_bytes / 1024.0 / event_count;
141
142 int is_file_wrong = 0;
143 if (file_size_gb > MAX_FILE_SIZE_GB) {
144 is_file_wrong = 1;
145 wrong_files.insert(pair<TString, double>(file_path, file_size_mb));
146 }
147 // do not take into account files with size < MIN_FILE_SIZE_MB
148 if (file_size_mb < MIN_FILE_SIZE_MB) {
149 is_file_wrong = 2;
150 wrong_files.insert(pair<TString, double>(file_path, file_size_mb));
151 }
152 // do not take into account files with event size < MIN_EVENT_SIZE_KB or > MAX_EVENT_SIZE_KB
153 else if (((event_size_kb < MIN_EVENT_SIZE_KB)) || (event_size_kb > MAX_EVENT_SIZE_KB))
154 {
155 is_file_wrong = 2;
156 wrong_events.insert(pair<TString, double>(file_path, event_size_kb));
157 } else {
158 sum_file_size_mb += file_size_mb;
159 sum_event_size_kb += event_size_kb;
160 correct_file_count++;
161 }
162
163 if (is_file_wrong == 1)
164 cout << orange;
165 else if (is_file_wrong == 2)
166 cout << red;
167 cout << file_path << " size: " << file_size_mb << " MB (event size = " << event_size_kb << " KB)"
168 << endl;
169 if (is_file_wrong > 0)
170 cout << reset;
171
172 hist_file_size->Fill(file_size_mb);
173 hist_event_size->Fill(event_size_kb);
174
175 root_file.Close();
176 } // if (file_name.EndsWith(".root"))
177 } // if not a directory
178 } // while file list
179
180 return 0;
181}
long Long_t
long long Long64_t
R__EXTERN TSystem * gSystem
TVirtualPad * cd(Int_t subpadnumber=0) override
T * Get(const char *namecycle)
void Close(Option_t *option="") override
TAxis * GetXaxis()
void Draw(Option_t *option="") override
virtual Int_t Fill(const char *name, Double_t w)
TObject * Next()
virtual void SetTitle(const char *title="")
const char * GetName() const override
const char * GetTitle() const override
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
const char * Data() const
virtual TList * GetListOfFiles() const
virtual Bool_t IsDirectory(const char *dir=nullptr) const
virtual Long64_t GetEntries() const
#define MIN_EVENT_SIZE_KB
#define MIN_FILE_SIZE_MB
#define MAX_FILE_SIZE_GB
#define MAX_EVENT_SIZE_KB
int calculate_size(TString dir_path, const char *root_tree_name, TH1F *hist_file_size, TH1F *hist_event_size, double &sum_file_size_mb, double &sum_event_size_kb, int &correct_file_count, map< TString, double > &wrong_files, map< TString, double > &wrong_events)
const string reset("\033[0m")
int show_root_stats(TString dir_path, const char *root_tree_name="bmndata")
Function for creating histograms on sizes of ROOT (digit) files stored in a specified directory amd e...
const string cyan("\033[0;96m")
const string red("\033[0;91m")
const string orange("\033[0;33m")