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