CbmRoot
Loading...
Searching...
No Matches
CbmFileUtils.cxx
Go to the documentation of this file.
1/* Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Florian Uhlig [committer] */
4
5#include "CbmFileUtils.h"
6
7#include "Logger.h" // for LOG, info and error
8
9#include "TArchiveFile.h" // for TArchiveFile
10#include "TFile.h" // for TFile
11#include "TObjArray.h"
12
13#include <string> // for string, find, substr
14
15#include <sys/stat.h> // for stcuct stat
16
17
18namespace Cbm
19{
20 namespace File
21 {
22 bool IsRootFile(std::string filename)
23 {
24 // Currently plain root files and root files stored in a zip file are supported.
25 // The destiction between the two is a "#" in the filename string which separates the
26 // name of the zip file from the name of the root file which is inside the zip file.
27 // In case the filename contains a hash (e.g. multi.zip#file.root) the hash
28 // separates the name of a zipfile (multi.zip) which contains the real root file
29 // (file.root).
30 // This nameing convention (e.g. multi.zip#file.root) is sed by ROOT.
31
32 // If a filename string contains a hash "#"
33 // split the string at the # in the name of the zipfile and
34 // the name of the contained root file.
35 std::string checkFilename {""};
36 std::string membername {""};
37 std::size_t found = filename.find("#");
38 if (found != std::string::npos) {
39 checkFilename = filename.substr(0, found);
40 membername = filename.substr(found + 1);
41 }
42 else {
43 checkFilename = filename;
44 }
45
46 bool wasfound = kFALSE;
47
48 // Check if the file exist
49 // In case of a root file contained in a zip archive check if the zip file
50 // exist
51 struct stat buffer;
52 if (stat(checkFilename.c_str(), &buffer) == 0) { wasfound = kTRUE; }
53 else {
54 wasfound = kFALSE;
55 LOG(error) << "Input File " << checkFilename << " not found";
56 }
57
58 // In case of a zip archive check if the archive contains the root file
59 if (membername.compare("") != 0) {
60 TFile* fzip = TFile::Open(checkFilename.c_str());
61 if (fzip->IsOpen()) {
62 TArchiveFile* archive = fzip->GetArchive();
63 if (archive) {
64 TObjArray* members = archive->GetMembers();
65 if (members->FindObject(membername.c_str()) == 0) {
66 LOG(error) << "File " << membername << " not found in zipfile " << checkFilename;
67 wasfound = kFALSE;
68 }
69 else {
70 LOG(info) << "File " << membername << " found in zipfile " << checkFilename;
71 wasfound = kTRUE;
72 }
73 }
74 else {
75 LOG(error) << "Zipfile " << checkFilename << " does not contain an archive";
76 wasfound = kFALSE;
77 }
78 fzip->Close();
79 delete fzip;
80 }
81 else {
82 LOG(error) << "Could not open zipfile " << checkFilename;
83 wasfound = kFALSE;
84 }
85 }
86 else {
87 TFile* rootfile = TFile::Open(checkFilename.c_str());
88 if (rootfile->IsOpen()) {
89 LOG(info) << "File " << checkFilename << " is a ROOT file.";
90 wasfound = kTRUE;
91 }
92 else {
93 LOG(error) << "File " << checkFilename << " is no ROOT file.";
94 wasfound = kFALSE;
95 }
96 rootfile->Close();
97 delete rootfile;
98 }
99
100 return wasfound;
101 }
102 } // namespace File
103} // namespace Cbm
bool IsRootFile(std::string filename)
Check if a the file exist and is a root file.