Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
Std.cxx
1#include "Std.h"
2#include "CompressionMap.h"
3#include "Const.h"
4#include "Cout.h"
5#include "XMLNode.h"
6
7#include <TCanvas.h>
8#include <TClonesArray.h>
9#include <TCollection.h>
10#include <TDatime.h>
11#include <TDirectory.h>
12#include <TList.h>
13#include <TLorentzVector.h>
14#include <TMath.h>
15#include <TMathBase.h>
16#include <TNamed.h>
17#include <TPad.h>
18#include <TROOT.h>
19#include <TSeqCollection.h>
20#include <TString.h>
21#include <TSystem.h>
22#include <TSystemDirectory.h>
23#include <TVector3.h>
24#include <TVirtualPad.h>
25#include <fstream>
26#include <iostream>
27#include <vector>
28
29
30NamespaceImp(Hal::Std);
31namespace Hal::Std {
32
33 void CopyFiles(TString from, TString to, Bool_t hidden) {
34 TSystemFile file(from, from);
35 TString full_name = file.GetName();
36 full_name.ReplaceAll("//", "");
37 if (full_name.EndsWith("/")) { full_name = TString(full_name(0, full_name.Length() - 1)); }
38 TString name(full_name(full_name.Last('/'), full_name.Length()));
39 if (name.BeginsWith(".") && hidden == kFALSE) return;
40 if (name.EqualTo(".") || name.EqualTo("..")) return;
41 if (file.IsDirectory()) {
42 gSystem->mkdir(to);
43 TSystemDirectory dir(from, from);
44 TList* list = dir.GetListOfFiles();
45 for (int i = 0; i < list->GetEntries(); i++) {
46 TSystemFile* subfile = (TSystemFile*) list->At(i);
47 TString subname = subfile->GetName();
48 if (subname.EqualTo(".") || subname.EqualTo("..")) continue;
49 TString from_path = Form("%s/%s", from.Data(), subname.Data());
50 TString to_path = Form("%s//%s", to.Data(), subname.Data());
51 CopyFiles(from_path, to_path);
52 }
53 } else {
54 if (name.Contains("..")) return;
55 if ((name.BeginsWith(".")) && hidden == kFALSE) return;
56 gSystem->CopyFile(from, to);
57 }
58 }
59
60 TString GetDate() {
61 TDatime* time = new TDatime();
62 Int_t day = time->GetDay();
63 Int_t month = time->GetMonth();
64 Int_t year = time->GetYear();
65 delete time;
66 return Form("%04d-%02d-%02d", year, month, day);
67 }
68
69 TString GetTime() {
70 TDatime* time = new TDatime();
71 Int_t h = time->GetHour();
72 Int_t m = time->GetMinute();
73 delete time;
74 return Form("%02d:%02d", h, m);
75 }
76
77 TString GetUniqueName(TString name) {
78 TObject* obj = gDirectory->Get(name);
79 if (obj == NULL) return name;
80 Int_t id = 0;
81 while (gDirectory->Get(Form("%s_%i", name.Data(), id))) {
82 id++;
83 }
84 return Form("%s_%i", name.Data(), id);
85 }
86
87 TString UpdateEnumToString(Hal::ECutUpdate upd) {
88 switch (upd) {
89 case Hal::ECutUpdate::kNo: return ""; break;
90 case Hal::ECutUpdate::kEvent: return "Event"; break;
91 case Hal::ECutUpdate::kTrack: return "Track"; break;
92 case Hal::ECutUpdate::kTwoTrack: return "TwoTrack"; break;
93 case Hal::ECutUpdate::kTwoTrackBackground: return "TwoTrackBackground"; break;
94 default: return ""; break;
95 }
96 }
97
98 TString GetConfigParameter(TString par_name) {
99 TString home = gSystem->Getenv("HOME");
100 Hal::XMLFile parser(Form("%s/.hal_config.xml", home.Data()));
101 Hal::XMLNode* root = parser.GetRootNode();
102 std::vector<TString> arr = Hal::Std::ExplodeString(par_name, '/');
103 Hal::XMLNode* node = root->GetChild(arr[0]);
104 for (int i = 1; i < (int) arr.size(); i++) {
105 node = node->GetChild(arr[i]);
106 }
107 if (node == nullptr) {
108 Hal::Cout::PrintInfo(Form("Node %s not found in hal configuration file", par_name.Data()), Hal::EInfo::kLowWarning);
109 return "";
110 }
111 TString value = node->GetValue();
112 return value;
113 }
114
115 std::vector<TString> GetListOfFiles(TString path, TString extension, Bool_t fullPath, Int_t recursive) {
116 TString pathLocal, pathFull;
117 TString pathPwd = gSystem->pwd();
118 if (path == "") {
119 pathLocal = ".";
120 pathFull = Form("%s", pathPwd.Data());
121 } else if (path.BeginsWith("/")) {
122 pathLocal = path;
123 pathFull = path;
124 } else {
125 pathLocal = path;
126 pathFull = Form("%s/%s", pathPwd.Data(), pathLocal.Data());
127 }
128 if (path.EndsWith("/")) { path = path(0, path.Length() - 1); }
129 if (pathLocal.EndsWith("/")) { pathLocal = pathLocal(0, pathLocal.Length() - 1); }
130 if (pathFull.EndsWith("/")) { pathFull = pathFull(0, pathFull.Length() - 1); }
131
132 TSystemDirectory* dir = new TSystemDirectory(pathLocal, pathLocal);
133 TList* list = dir->GetListOfFiles();
134 std::vector<TString> listOfFiles;
135 for (int i = 0; i < list->GetEntries(); i++) {
136 TSystemFile* subfile = (TSystemFile*) list->At(i);
137 TString filename = subfile->GetName();
138 if (filename == "." || filename == "..") continue;
139 if (filename.EndsWith(Form(".%s", extension.Data())) && extension.Length()) {
140 if (fullPath) {
141 listOfFiles.push_back(Form("%s/%s", pathFull.Data(), filename.Data()));
142 } else {
143 if (path.Length() != 0) {
144 listOfFiles.push_back(Form("%s/%s", path.Data(), filename.Data()));
145 } else {
146 listOfFiles.push_back(filename);
147 }
148 }
149 }
150 if (subfile->IsDirectory() && recursive != 0) {
151 std::vector<TString> sublist =
152 GetListOfFiles(Form("%s/%s", path.Data(), filename.Data()), extension, fullPath, recursive - 1);
153 listOfFiles.insert(std::end(listOfFiles), std::begin(sublist), std::end(sublist));
154 }
155 }
156 delete dir;
157 delete list;
158 return listOfFiles;
159 }
160
161 std::vector<std::vector<TVirtualPad*>> GetGridPad(Int_t x_pads, Int_t y_pads, Float_t x_margin, Float_t y_margin) {
162 TPad* padsav = (TPad*) gPad;
163 if (gPad == NULL) {
164 TCanvas* c = new TCanvas();
165 c->cd();
166 padsav = (TPad*) gPad;
167 }
168 std::vector<std::vector<TVirtualPad*>> array2d;
169 ResizeVector2D(array2d, x_pads, y_pads);
170 Double_t colls = x_pads;
171 Double_t rows = y_pads;
172 Double_t x_active = 1.0 - TMath::Abs(x_margin);
173 Double_t y_active = 1.0 - TMath::Abs(y_margin);
174 Double_t x_shift = 1.0 / (colls - 1 + 1. / x_active);
175 Double_t y_shift = 1.0 / (rows - 1 + 1. / y_active);
176 Double_t x_pad = x_shift / x_active;
177 Double_t y_pad = y_shift / y_active;
178 int glob = 0;
179
180 double x1, x2, y1, y2;
181 auto trimPads = [&](double i, double j, double m1, double m2, double m3, double m4) {
182 x1 = i * x_shift;
183 x2 = x1 + x_pad;
184 y2 = 1. - j * y_shift;
185 y1 = y2 - y_pad;
186 if (x1 < 0) x1 = 0;
187 if (x1 > 1) x1 = 1;
188 if (x2 < 0) x2 = 0;
189 if (x2 > 1) x2 = 1;
190 if (y1 < 0) y1 = 0;
191 if (y1 > 1) y1 = 1;
192 if (y2 < 0) y2 = 0;
193 if (y2 > 1) y2 = 1;
194 auto* pad = new TPad(Form("pad_%i_%i", int(i), int(j)), Form("pad_%i_%i", int(i), int(j)), x1, y1, x2, y2);
195 pad->SetTopMargin(m1);
196 pad->SetRightMargin(m2);
197 pad->SetBottomMargin(m3);
198 pad->SetLeftMargin(m4);
199 pad->Draw();
200 array2d[int(i)][int(j)] = pad;
201 glob++;
202 padsav->cd();
203 };
204
205 if (x_margin >= 0 && y_margin >= 0) { // OK
206 for (int i = x_pads - 1; i >= 0; i--) {
207 for (int j = 0; j < y_pads; j++) {
208 trimPads(i, j, 0, 0.0, y_margin, x_margin);
209 }
210 }
211 }
212 if (x_margin >= 0 && y_margin < 0) { // ok
213 y_margin = TMath::Abs(y_margin);
214 for (int i = x_pads - 1; i >= 0; i--) {
215 for (int j = y_pads - 1; j >= 0; j--) {
216 trimPads(i, j, y_margin, 0, 0, x_margin);
217 }
218 }
219 }
220
221 if (x_margin < 0 && y_margin < 0) { // ok
222 y_margin = TMath::Abs(y_margin);
223 x_margin = TMath::Abs(x_margin);
224 for (int i = 0; i < x_pads; i++) {
225 for (int j = y_pads - 1; j >= 0; j--) {
226 trimPads(i, j, y_margin, x_margin, 0, 0);
227 }
228 }
229 }
230
231 if (x_margin < 0 && y_margin >= 0) { // OK
232 x_margin = TMath::Abs(x_margin);
233 for (int i = 0; i < x_pads; i++) {
234 for (int j = 0; j < y_pads; j++) {
235 trimPads(i, j, 0, x_margin, y_margin, 0);
236 }
237 }
238 }
239 return array2d;
240 }
241
242 TString GetHalrootPlus() {
243 TString hal_var = gSystem->Getenv("HAL");
244 TString vmcworkdir = gSystem->Getenv("VMCWORKDIR");
245 TString main_dir = hal_var;
246 TString internalPathXml = "hal_plus/conversion_table/generators_patterns.xml";
247 TString mainVal;
248 std::vector<TString> location;
249 if (hal_var.Length() == 0) {
250 mainVal = vmcworkdir;
251 location.push_back("%s/../share/");
252 location.push_back("%s/../../share/");
253 location.push_back("%s/external/Hal/features/");
254 location.push_back("%s/hal/features/");
255 location.push_back("%s/../external/hal/features/");
256 location.push_back("%s/external/hal/features/");
257 } else {
258 location.push_back("%s/share/");
259 location.push_back("%s/../hal/features/");
260 mainVal = hal_var;
261 }
262 for (unsigned int i = 0; i < location.size(); i++) {
263 TString make_install = Form(location[i].Data(), mainVal.Data());
264 make_install = make_install + internalPathXml;
265 if (FileExists(make_install)) {
266 make_install.ReplaceAll("conversion_table/generators_patterns.xml", "");
267 return make_install;
268 }
269 }
270
271 Hal::Cout::PrintInfo("Hal plus not found!", Hal::EInfo::kError);
272 return "";
273 }
274
275 TString GetJsRoot() {
276 TString jsrootdir = gSystem->Getenv("JSROOT");
277 TString rootdir = gSystem->Getenv("ROOTSYS");
278 TString coreName = "JSRootCore.js";
279 if (GetJsRootVer() >= 6) { coreName = "JSRoot.core.js"; }
280
281 TString rootdirFair = rootdir + "/share/root/js/scripts/" + coreName;
282 rootdir = rootdir + "/js/scripts/" + coreName;
283 rootdir.ReplaceAll("//", "/");
284 std::cout << "PATH " << rootdir << std::endl;
285 std::vector<TString> location;
286 location.push_back(rootdirFair);
287 location.push_back(rootdir);
288 TString mainVal = "";
289 if (jsrootdir.Length() > 0) { return jsrootdir; }
290
291 for (auto place : location) {
292 if (FileExists(place)) {
293 place.ReplaceAll("scripts/" + coreName, "");
294 return place;
295 }
296 }
297 Hal::Cout::PrintInfo("JSROOT not found!", Hal::EInfo::kError);
298 return "";
299 }
300
301 Int_t VersionId(TString name) {
302 TString year(name(3, 4));
303 Int_t year_ver = year.Atoi();
304 TString month(name(0, 3));
305 Int_t month_ver = 0;
306 TString month_arr[12] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "sep", "oct", "nov", "dec"};
307 for (int i = 0; i < 12; i++) {
308 if (month.EqualTo(month_arr[i])) month_ver = i + 1;
309 }
310 return year_ver * 100 + month_ver;
311 }
312
313 TVector3 CalcualteBoostVector(Double_t energy_per_nucleon, Int_t n_proj, Int_t p_proj, Int_t n_tar, Int_t p_tar) {
314 Double_t Np = n_proj;
315 Double_t Pp = p_proj;
316 Double_t Nt = n_tar;
317 Double_t Pt = p_tar;
318 Double_t mass1 = Hal::Const::NeutronMass() * Np + Hal::Const::ProtonMass() * Pp;
319 Double_t mass2 = Hal::Const::NeutronMass() * Nt + Hal::Const::ProtonMass() * Pt;
320 Double_t energy1 = energy_per_nucleon * (Np + Pp);
321 Double_t energy2 = mass2;
322 Double_t mom1 = TMath::Sqrt(energy1 * energy1 - mass1 * mass1);
323 TLorentzVector vec1(0, 0, mom1, energy1), vec2(0, 0, 0, energy2);
324 return -(vec1 + vec2).BoostVector();
325 }
326
327 Bool_t FileExists(TString path) {
328 std::ifstream test_file;
329 test_file.open(path);
330 if (test_file.good()) {
331 test_file.close();
332 return kTRUE;
333 } else {
334 test_file.close();
335 return kFALSE;
336 }
337 }
338
339 void CompressArray(TClonesArray* array, const CompressionMap& map) {
340 for (int i = 0; i < map.GetSize(); i++) {
341 if (map.GetNewIndex(i) == -1) { array->RemoveAt(i); }
342 }
343 array->Compress();
344 }
345
346} // namespace Hal::Std
347Int_t Hal::Std::GetJsRootVer() {
348 Int_t ver = ::ROOT::GetROOT()->GetVersionInt();
349 if (ver < 62400) return 5;
350 return 6;
351}
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
XMLNode * GetChild(TString name, Int_t count=0) const
Definition XMLNode.cxx:93
TString GetValue() const
Definition XMLNode.h:120