GitLab maintenance scheduled for Tomorrow, 2020-03-31, from 17:00 to 18:00 CT - Services will be unavailable during this time.

FileSubRun.hpp 2.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#ifndef __HEPNOS_FILE_SUB_RUN_H
#define __HEPNOS_FILE_SUB_RUN_H

#include <string>
#include <limits>
#include <boost/filesystem.hpp>
#include <hepnos/FileProductAccessorBackend.hpp>
#include <hepnos/ProductAccessor.hpp>
#include <hepnos/FileEvent.hpp>

namespace hepnos {

namespace fs = boost::filesystem;

class FileRun;

class FileSubRun : public ProductAccessor<FileProductAccessorBackend> {

    private:

        friend class FileRun;
        friend class FileObjectIterator<FileSubRun>;

        FileSubRun(std::uint64_t subRunNumber, const std::string& dir) 
        : ProductAccessor<FileProductAccessorBackend>(dir)
        , _subRunNumber(subRunNumber)
        , _path(dir) {}

        FileSubRun()
        : ProductAccessor<FileProductAccessorBackend>("")
        , _subRunNumber(std::numeric_limits<std::uint64_t>::max())
        , _path("") {}

        std::uint64_t _subRunNumber;
        std::string   _path;

    public:

        typedef FileObjectIterator<FileEvent> iterator;

        std::uint64_t getSubRunNumber() const {
            return _subRunNumber;
        }

        bool isValid() const {
            return _subRunNumber != std::numeric_limits<std::uint64_t>::max();
        }

        FileEvent createEvent() {
            fs::directory_iterator begin(_path), end;
            size_t eventNumber = std::count_if(begin, end,
                    [](const fs::directory_entry& d) {
                    return fs::is_directory(d.path());
                    });
            std::stringstream ss;
            ss << _path << std::setfill('0') << std::setw(12) << eventNumber << "/";
            std::string dir = ss.str();
            fs::create_directory(dir);
            return FileEvent(eventNumber, dir);
        }

        FileEvent openEvent(std::uint64_t eventNumber) {
            std::stringstream ss;
            ss << _path << std::setfill('0') << std::setw(12) << eventNumber << "/";
            std::string dir = ss.str();
            if(fs::is_directory(dir))
                return FileEvent(eventNumber, dir);
            else
                return FileEvent();
        }

        std::size_t numEvents() const {
            fs::directory_iterator begin(_path), end;
            return std::count_if(begin, end,
                    [](const fs::directory_entry& d) {
                    return fs::is_directory(d.path());
                    });
        }

        iterator begin() {
            return iterator(_path, 0, numEvents());
        }

        iterator end() {
            auto n = numEvents();
            return iterator(_path, n, n);
        }
};

}
#endif