ATLAS Offline Software
Loading...
Searching...
No Matches
MonitoringFile_MoveVertexMonitoring.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3 */
4
6
7#include <vector>
8#include <stdlib.h>
9#include <stdio.h>
10#include <TFile.h>
11#include <TH1.h>
12#include <TKey.h>
13#include <string>
14
15namespace dqutils {
16 std::atomic<int> padding = 0;
17
18// define helper methods here rather then in this central MonitoringFile.h beast
19 int updateHists(const std::string& inFileName, const std::string& inStem, const std::string& outFileName = "",
20 const std::string& outStem = "");
21 bool makeDirectories(const std::string& dirName);
22 bool makeDir(const std::string& dirName);
23 void Copy(TFile* source, TFile* target, const std::string& inDir, const std::string& outDir,
24 const std::string& inHist = "", const std::string& outHist = "");
25 void CopyHist(TFile* source, TFile* target, const std::string& inDir, const std::string& outDir,
26 const std::string& inHist, const std::string& outHist);
27
28//main method here
29 void MonitoringFile::VxMon_move(const std::string& inFilename, bool isIncremental) {
30 std::string stream_ref = "physics_MinBias";
31 int stream_check = -1;
32 stream_check = inFilename.find(stream_ref);
33
34 if (!isIncremental && stream_check >= 0) {
35 TFile* f = TFile::Open(inFilename.c_str(), "UPDATE");
36 if (f == 0 || !f->IsOpen()) {
37 delete f;
38 return;
39 }
40 if (f->GetSize() < 1000.) {
41 delete f;
42 return;
43 }
44
45 bool dirExists = false;
46 std::string run_dir;
47 std::string lb_dir;
48 int writeEOS = 0;
49 int writeLOCAL = -1;
50 bool IsFirstTime = true;
51 std::string AthenaTAG;
52
53 const char* AtlasVersion = getenv("AtlasVersion");
54 if (!AtlasVersion) {
55 delete f;
56 return;
57 }
58 AthenaTAG = AtlasVersion;
59 std::string out_inStem;
60 std::string out_EOS;
61 std::string out_LOCAL;
62 std::string out_outStem;
63
64 TIter run_keys(f->GetListOfKeys());
65 TIter lb_keys(f->GetListOfKeys());
66 TKey* key_run(0);
67 TKey* key_lb(0);
68 while ((key_run = dynamic_cast<TKey*>(run_keys())) != 0) {
69 TObject* obj_run = key_run->ReadObj();
70 TDirectory* tdir_run = dynamic_cast<TDirectory*>(obj_run);
71 if (tdir_run != 0) {
72 std::string tdir_run_name(tdir_run->GetName());
73 if (tdir_run_name.find("run") != std::string::npos) {
74 run_dir = std::move(tdir_run_name);
75 out_EOS = "root://eosatlas.cern.ch//eos/atlas/atlascerngroupdisk/perf-idtracking/VertexMonitoring/VxMon_" +
76 run_dir + "_" + AthenaTAG + ".root";
77 out_LOCAL = "VxMon_" + run_dir + "_" + AthenaTAG + ".root";
78
79 lb_keys = tdir_run->GetListOfKeys();
80 while ((key_lb = dynamic_cast<TKey*>(lb_keys())) != 0) {
81 TObject* obj_lb = key_lb->ReadObj();
82 TDirectory* tdir_lb = dynamic_cast<TDirectory*>(obj_lb);
83 if (tdir_lb != 0) {
84 std::string tdir_lb_name(tdir_lb->GetName());
85 if (tdir_lb_name.find("lb") != std::string::npos) {
86 lb_dir = std::move(tdir_lb_name);
87
88 dirExists = f->GetDirectory((run_dir + "/" + lb_dir + "/InDetGlobal/PrimaryVertexMultiplicity").c_str());
89 if (dirExists) {
90 out_inStem = run_dir + "/" + lb_dir + "/InDetGlobal/PrimaryVertexMultiplicity/nVx";
91 out_outStem = "nVx_" + lb_dir;
92 if (writeEOS == 0) {
93 writeEOS = updateHists(inFilename, out_inStem, out_EOS, out_outStem);
94 if (writeEOS != 0 && IsFirstTime) writeLOCAL = updateHists(inFilename, out_inStem, out_LOCAL,
95 out_outStem);
96 } else {
97 writeLOCAL = updateHists(inFilename, out_inStem, out_LOCAL, out_outStem);
98 }
99
100 if (writeEOS != 0 && !IsFirstTime && writeLOCAL != 0) {
101 delete f;
102 return;
103 }
104 ;
105 if (writeEOS != 0 && writeLOCAL != 0) {
106 delete f;
107 return;
108 }
109 ;
110 IsFirstTime = false;
111 }
112 }
113 } else {
114 delete obj_lb;
115 }
116 }
117
118 if (writeEOS != 0 && writeLOCAL == 0) {
119 int return_code = system(
120 ("xrdcp VxMon_" + run_dir + "_" + AthenaTAG + ".root root://eosatlas.cern.ch//eos/atlas/atlascerngroupdisk/perf-idtracking/VertexMonitoring/VxMon_" + run_dir + "_" + AthenaTAG +
121 ".root").c_str());
122 if (return_code == 0) std::ignore = remove(out_LOCAL.c_str()); //returns zero on success
123 else {
124 delete f;
125 return;
126 }
127 }
128 }
129 } else {
130 delete obj_run;
131 }
132 }
133
134 f->Close();
135 delete f;
136 }
137 return;
138 }
139
140 //----------------------------------------
141 // main macro
142 //----------------------------------------
143 int updateHists(const std::string& inFileName, const std::string& inStem, const std::string& fileName,
144 const std::string& stem) {
145 std::string outFileName = fileName;
146 std::string outStem = stem;
147 //open original file
148 TFile* source = TFile::Open(inFileName.c_str());
149 if (!source) {
150 //std::cout << "Couldn't open input file, " << inFileName << std::endl;
151 return 1;
152 }//else std::cout << "opening input file, " << inFileName << std::endl;
153
154 //find out whether inStem is a histogram or a directory
155 bool isDir = true;
156 std::string path = inStem;
157 std::string hist;
158
159 if (inStem[inStem.size() - 1] != '/') {
160 std::string::size_type lastSlash = inStem.find_last_of('/');
161 hist = inStem.substr(lastSlash + 1, inStem.size() - lastSlash - 1);
162 path = inStem.substr(0, lastSlash + 1);
163 isDir = (source->FindObjectAny(hist.c_str()))->InheritsFrom("TDirectory");
164 if (isDir) {
165 path = inStem;
166 hist.clear();
167 }
168 }
169
170 if (path[path.size() - 1] == '/') path.resize(path.size() - 1);
171 if (!source->GetDirectory(path.c_str())) {
172 //std::cout << "path " << path << " directory doesn't exist in input file" << std::endl;
173 return 1;
174 }
175
176 //open target file
177 std::cout.fill(' ');
178 if (outFileName.empty()) {
179 outFileName = std::string(inFileName, 0, inFileName.find(".root"));
180 outFileName += "_trimmed.root";
181 }
182
183 TFile* target = TFile::Open(outFileName.c_str(), "update");
184 if (!target) {
185 //std::cout << "coundn't open output file " << outFileName << std::endl;
186 return 1;
187 }//else std::cout << "opening output file " << outFileName << std::endl;
188
189 if (outStem.empty()) outStem = inStem;
190 std::string targetPath = outStem;
191 std::string targetHist;
192 if (!isDir) {
193 std::string::size_type lastSlash = outStem.find_last_of('/');
194 targetPath = outStem.substr(0, lastSlash + 1);
195 targetHist = outStem.substr(lastSlash + 1, outStem.size() - lastSlash - 1);
196 }
197
198 if (targetPath[targetPath.size() - 1] == '/') targetPath.resize(targetPath.size() - 1);
199
200 target->cd();
201 if (!makeDirectories(targetPath)) {
202 //std::cout << "couldn't create outStem directories in output" << std::endl;
203 return 1;
204 }
205 //copy relevant objects
206 if (!target->IsWritable()) return 1;
207
208 Copy(source, target, path, targetPath, hist, targetHist);
209
210 delete target;
211 delete source;
212 return 0;
213 }
214
215 bool makeDirectories(const std::string& dirName) {
216 bool success = true;
217
218 if (!dirName.empty()) {
219 std::string::size_type firstSlash = dirName.find('/');
220 if (firstSlash == std::string::npos) {
221 success &= makeDir(dirName);
222 } else {
223 std::string subdir(dirName, 0, firstSlash);
224 if (!subdir.empty()) success &= makeDir(subdir);
225
226 std::string newSubdir(dirName, firstSlash + 1, dirName.size() - firstSlash);
227 success &= makeDirectories(newSubdir);
228 }
229 }
230 return success;
231 }
232
233 bool makeDir(const std::string& dirName) {
234 padding += 3;
235 std::cout << std::setw(padding) << " ";
236
237 if (!gDirectory->FindKey(dirName.c_str())) {
238 gDirectory->mkdir(dirName.c_str());
239// std::cout << "makeDir=" << dirName << std::endl;
240 } else
241// std::cout << "object " << dirName << " already exists in directory " << gDirectory->GetName() <<
242// std::endl;
243 padding -= 3;
244 return gDirectory->cd(dirName.c_str());
245 }
246
247 void Copy(TFile* source, TFile* target, const std::string& inDir, const std::string& outDir,
248 const std::string& inHist, const std::string& outHist) {
249 padding += 3;
250
251 if (!inHist.empty()) {
252 CopyHist(source, target, inDir, outDir, inHist, outHist);
253 } else {
254 TDirectory* sourceDir = source->GetDirectory(inDir.c_str());
255 TDirectory* targetDir = target->GetDirectory(outDir.c_str());
256
257 TIter nextKey(sourceDir->GetListOfKeys());
258
259 TKey* key;
260 while ((key = (TKey*) nextKey())) {
261 std::string keyName = key->GetName();
262 std::string className(key->GetClassName());
263
264 if (className == "TDirectoryFile") {
265 std::string newInDir = inDir;
266 newInDir += '/';
267 newInDir += keyName;
268
269 std::string newOutDir = outDir;
270 newOutDir += '/';
271 newOutDir += keyName;
272
273 if (!targetDir->FindKey(keyName.c_str())) {
274 /*
275 std::cout << std::setw(padding) << " ";
276 std::cout << "creating Dir " << newOutDir << std::endl;
277 */
278 targetDir->mkdir(keyName.c_str());
279 }
280 /*
281 std::cout << std::setw(padding) << " ";
282 std::cout << "moving to " << newInDir << ", a " << className << std::endl;
283 */
284 Copy(source, target, newInDir, newOutDir);
285 } else {
286 CopyHist(source, target, inDir, outDir, keyName, keyName);
287 }
288 }
289 }
290
291 padding -= 3;
292 }
293
294 void CopyHist(TFile* source, TFile* target, const std::string& inDir, const std::string& outDir,
295 const std::string& inHist, const std::string& outHist) {
296 TDirectory* sourceDir = source->GetDirectory(inDir.c_str());
297 TDirectory* targetDir = target->GetDirectory(outDir.c_str());
298
299 targetDir->cd();
300 TKey* key = sourceDir->FindKey(inHist.c_str());
301 TObject* object = key->ReadObj();
302 bool permission = true;
303
304 if (targetDir->FindKey(outHist.c_str())) permission = false;
305 if (permission) object->Write(outHist.c_str(), TObject::kOverwrite);
306 }
307}
static void VxMon_move(const std::string &inFilename, bool isIncremental=false)
bool makeDirectories(const std::string &dirName)
void Copy(TFile *source, TFile *target, const std::string &inDir, const std::string &outDir, const std::string &inHist="", const std::string &outHist="")
bool makeDir(const std::string &dirName)
void CopyHist(TFile *source, TFile *target, const std::string &inDir, const std::string &outDir, const std::string &inHist, const std::string &outHist)
int updateHists(const std::string &inFileName, const std::string &inStem, const std::string &outFileName="", const std::string &outStem="")