FileEvent.hpp 2.39 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef __HEPNOS_FILE_EVENT_H
#define __HEPNOS_FILE_EVENT_H

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

namespace hepnos {

namespace fs = boost::filesystem;

class FileSubRun;
Matthieu Dorier's avatar
Matthieu Dorier committed
16
class FileDataStore;
Matthieu Dorier's avatar
Matthieu Dorier committed
17 18 19 20 21

class FileEvent : public ProductAccessor<FileProductAccessorBackend> {

    private:

Matthieu Dorier's avatar
Matthieu Dorier committed
22
        friend class FileDataStore;
Matthieu Dorier's avatar
Matthieu Dorier committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        friend class FileSubRun;
        friend class FileObjectIterator<FileEvent>;

        void createRefAndSetID() {
            std::size_t h = std::hash<std::string>()(_path);
            std::string linkName;
            std::stringstream ss;
            do {
                ss.str("");
                ss << _path << "../../../../.ref/";
                ss << std::setfill('0') << std::setw(20) << h;
                h += 1;
            } while(fs::exists(ss.str()));
            _eventID = h-1;
            fs::create_symlink(_path, ss.str());
        }

        FileEvent(std::uint64_t eventNumber, const std::string& dir) 
        : ProductAccessor<FileProductAccessorBackend>(dir)
        , _eventNumber(eventNumber)
        , _path(dir) {
            createRefAndSetID();
        }

Matthieu Dorier's avatar
Matthieu Dorier committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        FileEvent(const std::string& dir)
        : ProductAccessor<FileProductAccessorBackend>(dir)
        , _eventNumber(0)
        , _path(dir) {
            if(_path.back() != '/') _path += std::string("/");
            std::size_t i,j;
            j = _path.size()-1;
            if(_path[j] == '/') j--;
            i = j;
            while(_path[i] != '/') i--;
            i += 1;
            while(_path[i] == '0') i++;
            j += 1;
            std::string eventDir(&dir[i], j-i);
            if(eventDir.size() > 0)
                _eventNumber = std::stoi(eventDir);
        }

Matthieu Dorier's avatar
Matthieu Dorier committed
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
        FileEvent()
        : ProductAccessor<FileProductAccessorBackend>("")
        , _eventNumber(std::numeric_limits<std::uint64_t>::max())
        , _path("") {}

        std::uint64_t _eventNumber;
        std::string   _path;
        std::size_t   _eventID;

    public:

        std::uint64_t getEventNumber() const {
            return _eventNumber;
        }

        std::size_t getEventID() const {
            return _eventID;
        }

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

}
#endif