Skip to content
Snippets Groups Projects
Select Git revision
  • cdeb89b9a2516eb9fd1f5768d3cf6470ff5a0961
  • master default protected
  • jul25_patches
  • nightly_master
  • online_mvd_readconf_cleanup protected
  • online_much_readconf_cleanup protected
  • cleanup_rich_v25a
  • jul24_patches
  • nov23_patches
  • DC_2404
  • nighly_master
  • DC_Jan24
  • DC_Nov23
  • DC_Oct23
  • feb23_patches
  • L1Algo-dev9
  • dec21_patches protected
  • apr21_patches protected
  • RC2_jul25
  • dev_2025_46
  • dev_2025_45
  • dev_2025_44
  • dev_2025_43
  • dev_2025_42
  • dev_2025_41
  • dev_2025_40
  • dev_2025_39
  • dev_2025_38
  • dev_2025_37
  • dev_2025_36
  • dev_2025_35
  • dev_2025_34
  • dev_2025_33
  • dev_2025_32
  • dev_2025_31
  • dev_2025_30
  • RC_jul25
  • dev_2025_29
38 results

CbmFileUtils.cxx

Blame
  • CbmFileUtils.cxx 3.38 KiB
    /* Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
       SPDX-License-Identifier: GPL-3.0-only
       Authors: Florian Uhlig [committer] */
    
    #include "CbmFileUtils.h"
    
    #include "Logger.h"  // for LOG, info and error
    
    #include "TArchiveFile.h"  // for TArchiveFile
    #include "TFile.h"         // for TFile
    #include "TObjArray.h"
    
    #include <string>  // for string, find, substr
    
    #include <sys/stat.h>  // for stcuct stat
    
    
    namespace Cbm
    {
      namespace File
      {
        bool IsRootFile(std::string filename)
        {
          // Currently plain root files and root files stored in a zip file are supported.
          // The destiction between the two is a "#" in the filename string  which separates the
          // name of the zip file from the name of the root file which is inside the zip file.
          // In case the filename contains a hash (e.g. multi.zip#file.root) the hash
          // separates the name of a zipfile (multi.zip) which contains the real root file
          // (file.root).
          // This nameing convention (e.g. multi.zip#file.root) is sed by ROOT.
    
          // If a filename string contains a hash "#"
          // split the string at the # in the name of the zipfile and
          // the name of the contained root file.
          std::string checkFilename {""};
          std::string membername {""};
          std::size_t found = filename.find("#");
          if (found != std::string::npos) {
            checkFilename = filename.substr(0, found);
            membername    = filename.substr(found + 1);
          }
          else {
            checkFilename = filename;
          }
    
          bool wasfound = kFALSE;
    
          // Check if the file exist
          // In case of a root file contained in a zip archive check if the zip file
          // exist
          struct stat buffer;
          if (stat(checkFilename.c_str(), &buffer) == 0) { wasfound = kTRUE; }
          else {
            wasfound = kFALSE;
            LOG(error) << "Input File " << checkFilename << " not found";
          }
    
          // In case of a zip archive check if the archive contains the root file
          if (membername.compare("") != 0) {
            TFile* fzip = TFile::Open(checkFilename.c_str());
            if (fzip->IsOpen()) {
              TArchiveFile* archive = fzip->GetArchive();
              if (archive) {
                TObjArray* members = archive->GetMembers();
                if (members->FindObject(membername.c_str()) == 0) {
                  LOG(error) << "File " << membername << " not found in zipfile " << checkFilename;
                  wasfound = kFALSE;
                }
                else {
                  LOG(info) << "File " << membername << " found in zipfile " << checkFilename;
                  wasfound = kTRUE;
                }
              }
              else {
                LOG(error) << "Zipfile " << checkFilename << " does not contain an archive";
                wasfound = kFALSE;
              }
              fzip->Close();
              delete fzip;
            }
            else {
              LOG(error) << "Could not open zipfile " << checkFilename;
              wasfound = kFALSE;
            }
          }
          else {
            TFile* rootfile = TFile::Open(checkFilename.c_str());
            if (rootfile->IsOpen()) {
              LOG(info) << "File " << checkFilename << " is a ROOT file.";
              wasfound = kTRUE;
            }
            else {
              LOG(error) << "File " << checkFilename << " is no ROOT file.";
              wasfound = kFALSE;
            }
            rootfile->Close();
            delete rootfile;
          }
    
          return wasfound;
        }
      }  // namespace File
    }  // namespace Cbm