Changeset 11893


Ignore:
Timestamp:
08/11/11 23:09:13 (13 years ago)
Author:
tbretz
Message:
Implemented a new determination fo run-number after noon in the fadctrl; created the possibility to add the date to the raw-data path; allow to set the path as program option; for all this moved some code from the datalogger to DimWriteStatistics
Location:
trunk/FACT++/src
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/DataCalib.cc

    r11885 r11893  
    221221    file.AddColumn('F', "TriggerOffsetRms",  1024*1440, "V");
    222222
    223     fFileName = FormFileName(GetRunId(), "drs.fits");
     223    fFileName = FormFileName("drs.fits");
    224224
    225225    if (!file.OpenFile(fFileName))
  • trunk/FACT++/src/DataCalib.h

    r11844 r11893  
    3131
    3232public:
    33     DataCalib(uint32_t id, DimDescribedService &dim, MessageImp &imp) : DataProcessorImp(id, imp), fDim(dim)
     33    DataCalib(const std::string &path, uint32_t id, DimDescribedService &dim, MessageImp &imp) : DataProcessorImp(path, id, imp), fDim(dim)
    3434    {
    3535    }
  • trunk/FACT++/src/DataProcessorImp.cc

    r11726 r11893  
    22
    33#include "FAD.h"
     4
     5#include "DimWriteStatistics.h" // weird dependency
    46
    57using namespace std;
     
    1315//! @param extension a string containing the extension to be appened to the file name
    1416//
    15 string DataProcessorImp::FormFileName(uint32_t runid, const string &extension)
     17string DataProcessorImp::FormFileName(const string &path, uint32_t runid, const string &extension)
    1618{
    1719    ostringstream name;
    18     name << Time().NightAsInt() << '_' << setfill('0') << setw(3) << runid << '.' << extension;
     20
     21    if (!path.empty())
     22    {
     23        name << path;
     24        if (path[path.length()-1] != '/')
     25            name << '/';
     26    }
     27
     28    const Time time;
     29    name << time.GetAsStr("%Y/%m/%d/");
     30
     31    try
     32    {
     33        DimWriteStatistics::CreateDirectory(name.str());
     34    }
     35    catch (const runtime_error &)
     36    {
     37        // File creation will fail anyway
     38        //Error(e.what());
     39    }
     40
     41    name << time.NightAsInt() << '_' << setfill('0') << setw(3) << runid << '.' << extension;
    1942    return name.str();
    2043}
  • trunk/FACT++/src/DataProcessorImp.h

    r11726 r11893  
    1414class DataProcessorImp : public MessageImp
    1515{
    16     uint32_t fRunId;
     16    std::string fPath;
     17    uint32_t    fRunId;
    1718
    1819    int Write(const Time &time, const std::string &txt, int qos)
     
    2627
    2728public:
    28     DataProcessorImp(uint32_t id, MessageImp &imp) : fRunId(id), fMsg(imp) { }
     29    DataProcessorImp(const std::string &path, uint32_t id, MessageImp &imp) : fPath(path), fRunId(id), fMsg(imp) { }
    2930    virtual ~DataProcessorImp() { }
    3031
     
    3536    const std::string &GetFileName() const { return fFileName; }
    3637
    37     uint32_t GetRunId() const { return fRunId; }
     38    std::string GetPath() const { return fPath; }
     39    uint32_t    GetRunId() const { return fRunId; }
    3840
    39     static std::string FormFileName(uint32_t runid, const std::string &extension);
     41    static std::string FormFileName(const std::string &path, uint32_t runid, const std::string &extension);
     42    std::string FormFileName(const std::string &extension)
     43    {
     44        return FormFileName(fPath, fRunId, extension);
     45    }
    4046};
    4147
     
    4753
    4854public:
    49     DataDump(uint32_t id, MessageImp &imp) : DataProcessorImp(id, imp) { }
     55    DataDump(const std::string &path, uint32_t id, MessageImp &imp) : DataProcessorImp(path, id, imp) { }
    5056
    5157    bool Open(RUN_HEAD* h);
     
    5763{
    5864public:
    59     DataDebug(uint32_t id, MessageImp &imp) : DataDump(id, imp) { }
     65    DataDebug(const std::string &path, uint32_t id, MessageImp &imp) : DataDump(path, id, imp) { }
    6066
    6167    bool WriteEvt(EVENT *e);
  • trunk/FACT++/src/DataWriteFits.cc

    r11886 r11893  
    6666
    6767    //Form filename, based on runid and run-type
    68     fFileName = FormFileName(GetRunId(), "fits");
     68    fFileName = FormFileName("fits");
    6969
    7070    if (!fFile.OpenFile(fFileName))
  • trunk/FACT++/src/DataWriteFits.h

    r11743 r11893  
    1919
    2020public:
    21     DataWriteFits(uint32_t runid, MessageImp &imp) :
    22         DataProcessorImp(runid, imp), fConv(0), fFile(imp)
     21    DataWriteFits(const std::string path, uint32_t runid, MessageImp &imp) :
     22        DataProcessorImp(path, runid, imp), fConv(0), fFile(imp)
    2323    {
    2424    }
  • trunk/FACT++/src/DataWriteRaw.cc

    r11726 r11893  
    2020bool DataWriteRaw::Open(RUN_HEAD *h)
    2121{
    22     const string name = FormFileName(GetRunId(), "bin");
     22    const string name = FormFileName("bin");
    2323    if (access(name.c_str(), F_OK)==0)
    2424    {
  • trunk/FACT++/src/DataWriteRaw.h

    r11726 r11893  
    6868
    6969public:
    70     DataWriteRaw(uint32_t id, MessageImp &imp) : DataProcessorImp(id, imp), fPosTail(0) { }
     70    DataWriteRaw(const std::string &path, uint32_t id, MessageImp &imp) : DataProcessorImp(path, id, imp), fPosTail(0) { }
    7171    ~DataWriteRaw() { if (fOut.is_open()) Close(); }
    7272
  • trunk/FACT++/src/DimWriteStatistics.cc

    r11534 r11893  
    1010#include <sys/statvfs.h> //for getting disk free space
    1111#include <sys/stat.h>    //for getting files sizes
     12
     13#include <boost/filesystem.hpp>
    1214
    1315#include "Time.h"
     
    8890
    8991     return -1;
     92}
     93
     94// --------------------------------------------------------------------------
     95//
     96//! Check if a given path exists
     97//! @param path the path to be checked
     98//! @return whether or not the given path exists
     99//
     100bool DimWriteStatistics::DoesPathExist(const string &path, MessageImp &log)
     101{
     102    namespace fs = boost::filesystem;
     103
     104    const fs::path fullPath = fs::system_complete(fs::path(path));
     105
     106    if (!fs::exists(fullPath))
     107       return false;
     108
     109    if (!fs::is_directory(fullPath))
     110    {
     111        log.Error("Path given for checking '" + path + "' designate a file name. Please provide a path name only");
     112        return false;
     113    }
     114
     115    if (access(path.c_str(), R_OK|W_OK|X_OK) != 0)
     116    {
     117        log.Error("Missing read, write or execute permissions on directory '" + path + "'");
     118        return false;
     119    }
     120
     121    return true;
     122}
     123
     124// --------------------------------------------------------------------------
     125//
     126//! Check if a given path exists
     127//! @param path the path to be checked
     128//! @return whether or not the creation has been successfull
     129//
     130void DimWriteStatistics::CreateDirectory(string path)
     131{
     132    namespace fs = boost::filesystem;
     133
     134    //remove last '/', if present
     135    if (path[path.size()-1] == '/')
     136        path = path.substr(0, path.size()-1);
     137
     138    //create boost path
     139    const fs::path fullPath = fs::system_complete(fs::path(path));
     140
     141    //if path does not exist, check if upper levels exist already
     142    if (fs::exists(fullPath))
     143    {
     144        //if path already exist, make sure it does not designate a file (filenames cannot be checked if they do not exist)
     145        if (fs::is_directory(fullPath))
     146            return;
     147
     148        throw runtime_error("Path to be created is a file  '" + path + "'");
     149    }
     150
     151    // we're hitting "/", which SHOULD have existed...
     152    if (path.size() <= 1)
     153        throw runtime_error("Path to be created looks suspicious '"+path+"'");
     154
     155    CreateDirectory(path.substr(0, path.find_last_of('/')));
     156
     157    //path does not exist, and upper level have been created already by recusrion.
     158    const mode_t rightsMask = S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH; //everybody read, owner writes
     159
     160    if (mkdir(path.c_str(), rightsMask)==0)
     161        return;
     162
     163    ostringstream str;
     164    str << "mkdir() failed for '" << path << "': " << strerror(errno) << " [errno=" << errno << "]";
     165    throw runtime_error(str.str());
    90166}
    91167
  • trunk/FACT++/src/DimWriteStatistics.h

    r11534 r11893  
    8080    ///Returns the size on disk of a given file
    8181    static int64_t GetFileSizeOnDisk(const std::string& file, MessageImp &imp);
     82
     83    static bool DoesPathExist(const std::string &path, MessageImp &log);
     84
     85    static void CreateDirectory(std::string path);
    8286};
    8387
  • trunk/FACT++/src/EventBuilderWrapper.h

    r11889 r11893  
    1010#endif
    1111#include <boost/thread.hpp>
     12#include <boost/filesystem.hpp>
    1213#include <boost/date_time/posix_time/posix_time_types.hpp>
    1314
     
    2526namespace ba = boost::asio;
    2627namespace bs = boost::system;
     28namespace fs = boost::filesystem;
    2729
    2830using ba::ip::tcp;
     
    112114    bool fDebugLog;
    113115
     116    string   fPath;
    114117    uint32_t fRunNumber;
    115118
    116     void InitRunNumber()
    117     {
    118         // FIXME: Add a check that we are not too close to noon!
    119         //const int night = Time().NightAsInt();
    120 
     119protected:
     120    int64_t InitRunNumber()
     121    {
    121122        fRunNumber = 1000;
    122123
     124        // Ensure that the night doesn't change during our check
     125        const int check = Time().NightAsInt();
     126
    123127        while (--fRunNumber>0)
    124128        {
    125             const string name = DataProcessorImp::FormFileName(fRunNumber, "");
     129            const string name = DataProcessorImp::FormFileName(fPath, fRunNumber, "");
    126130
    127131            if (access((name+"bin").c_str(), F_OK) == 0)
     
    133137        }
    134138
     139        if (check != Time().NightAsInt())
     140            return InitRunNumber();
     141
    135142        fRunNumber++;
    136143
     144        if (fRunNumber==1000)
     145        {
     146            fMsg.Error("You have a file with run-number 1000 in "+fPath);
     147            return -1;
     148        }
     149
    137150        ostringstream str;
    138         str << "Starting with run number " << fRunNumber;
     151        str << "Set next run-number to " << fRunNumber << " determined from " << fPath;
    139152        fMsg.Message(str);
    140153
    141         fMsg.Info(" ==> TODO: Run-number detection doesn't work when noon passes!");
    142154        fMsg.Info(" ==> TODO: Crosscheck with database!");
     155
     156        return check;
     157    }
     158
     159    int64_t InitRunNumber(const string &path)
     160    {
     161        if (!DimWriteStatistics::DoesPathExist(path, fMsg))
     162            return -1;
     163
     164        //const fs::path fullPath = fs::system_complete(fs::path(path));
     165
     166        fPath = path;
     167
     168        return InitRunNumber();
    143169    }
    144170
     
    176202        for (size_t i=0; i<40; i++)
    177203            ConnectSlot(i, tcp::endpoint());
    178 
    179         InitRunNumber();
    180204    }
    181205    virtual ~EventBuilderWrapper()
     
    499523        switch (fFileFormat)
    500524        {
    501         case kNone:  file = new DataDump(runid,  fMsg); break;
    502         case kDebug: file = new DataDebug(runid, fMsg); break;
    503         case kFits:  file = new DataWriteFits(runid,  fMsg); break;
    504         case kRaw:   file = new DataWriteRaw(runid,   fMsg); break;
    505         case kCalib: file = new DataCalib(runid, fDimDrsCalibration, fMsg); break;
     525        case kNone:  file = new DataDump(fPath, runid,  fMsg); break;
     526        case kDebug: file = new DataDebug(fPath, runid, fMsg); break;
     527        case kFits:  file = new DataWriteFits(fPath, runid,  fMsg); break;
     528        case kRaw:   file = new DataWriteRaw(fPath, runid,   fMsg); break;
     529        case kCalib: file = new DataCalib(fPath, runid, fDimDrsCalibration, fMsg); break;
    506530        }
    507531
  • trunk/FACT++/src/datalogger.cc

    r11837 r11893  
    414414bool DataLogger::CreateDirectory(string path)
    415415{
    416     //remove last '/', if present
    417     if (path[path.size()-1] == '/')
    418         path = path.substr(0, path.size()-1);
    419 
    420     //create boost path
    421     const boost::filesystem::path fullPath =  boost::filesystem::system_complete(boost::filesystem::path(path));
    422 
    423     //if path does not exist, check if upper levels exist already
    424     if (boost::filesystem::exists(fullPath))
    425     {
    426         //if path already exist, make sure it does not designate a file (filenames cannot be checked if they do not exist)
    427         if (boost::filesystem::is_directory(fullPath))
    428             return true;
    429 
    430         Error("Path to be created contains a file name: '" + path + "'");
     416    try
     417    {
     418        DimWriteStatistics::CreateDirectory(path);
     419        return true;
     420    }
     421    catch (const runtime_error &e)
     422    {
     423        Error(e.what());
    431424        return false;
    432425    }
    433 
    434     if (path.size() <= 1)
    435     {//we're hitting "/", which SHOULD have existed...
    436         Error("Something unexpected happened while creating a path");
    437     }
    438     CreateDirectory(path.substr(0, path.find_last_of('/')));
    439 
    440     //path does not exist, and upper level have been created already by recusrion.
    441     const mode_t rightsMask = S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH; //everybody read, owner writes
    442 
    443     const int returnValue = mkdir(path.c_str(), rightsMask);
    444 
    445     if (returnValue != 0)
    446     {
    447         ostringstream str;
    448         str << "mkdir() failed for '" << path << "': " << strerror(errno) << " [errno=" << errno << "]";
    449         Error(str.str());
    450         return false;
    451     }
    452 
    453     return true;
    454426}
    455427// --------------------------------------------------------------------------
     
    461433bool DataLogger::DoesPathExist(string path)
    462434{
    463     const boost::filesystem::path fullPath = boost::filesystem::system_complete(boost::filesystem::path(path));
    464 
    465     if (!boost::filesystem::exists(fullPath))
    466        return false;
    467 
    468     if (!boost::filesystem::is_directory(fullPath))
    469     {
    470         Error("Path given for checking '" + path + "' designate a file name. Please provide a path name only");
    471         return false;
    472     }
    473 
    474     if (access(path.c_str(), R_OK|W_OK|X_OK) != 0)
    475     {
    476         Error("Missing read, write or execute permissions on directory '" + path + "'");
    477         return false;
    478     }
    479 
    480     return true;
    481 }
     435    return DimWriteStatistics::DoesPathExist(path, *this);
     436}
     437
    482438// --------------------------------------------------------------------------
    483439//
  • trunk/FACT++/src/fadctrl.cc

    r11892 r11893  
    10211021        }
    10221022
     1023        if (fNightAsInt!=Time().NightAsInt())
     1024        {
     1025            ostringstream out;
     1026            out << "Night changed from " << fNightAsInt << " to " << Time().NightAsInt() << "... determining new run-number.";
     1027            T::Info(out);
     1028
     1029            // FIXME: What about an error state?
     1030            fNightAsInt = InitRunNumber();
     1031            if (fNightAsInt<0)
     1032                return FAD::kConnected;
     1033        }
     1034
    10231035        const uint32_t runno = StartNewRun(evt.Get<uint64_t>(), evt.Get<uint64_t>(8), *fTargetConfig);
    10241036
     
    10261038        str << "Starting configuration for run " << runno << " (" << name << ")";
    10271039        T::Message(str.str());
     1040
     1041        if (runno>=1000)
     1042            T::Warn("Run number exceeds logical maximum of 999 - this is no problem for writing but might give raise to problems in the analysis.");
    10281043
    10291044        const FAD::Configuration &conf = fTargetConfig->second;
     
    18131828    }
    18141829
     1830    int64_t fNightAsInt;
     1831
    18151832    int EvalOptions(Configuration &conf)
    18161833    {
     
    18231840        // ---------- Setup event builder ---------
    18241841        SetMaxMemory(conf.Get<unsigned int>("max-mem"));
     1842
     1843        fNightAsInt = InitRunNumber(conf.Get<string>("destination-folder"));
     1844        if (fNightAsInt<0)
     1845            return 1;
    18251846
    18261847        // ---------- Setup run types ---------
     
    18381859            {
    18391860                T::Error("Run-type "+*it+" defined twice.");
    1840                 return 1;
     1861                return 2;
    18411862            }
    18421863
     
    18461867                !CheckConfigVal<bool>(conf, true, "enable-dwrite.",            *it) ||
    18471868                !CheckConfigVal<bool>(conf, true, "enable-continous-trigger.", *it))
    1848                 return 2;
     1869                return 3;
    18491870
    18501871            target.fDenable          = conf.GetDef<bool>("enable-drs.", *it);
     
    18561877            {
    18571878                if (!CheckConfigVal<uint16_t>(conf, 0xffff, "trigger-rate.", *it))
    1858                     return 3;
     1879                    return 4;
    18591880
    18601881                target.fTriggerRate = conf.GetDef<uint16_t>("trigger-rate.", *it);
     
    18681889                if (!CheckConfigVal<uint16_t>(conf, FAD::kMaxRoiValue, "roi.",    *it) &&
    18691890                    !CheckConfigVal<uint16_t>(conf, FAD::kMaxRoiValue, str.str(), *it))
    1870                     return 4;
     1891                    return 5;
    18711892
    18721893                target.fRoi[i] = conf.HasDef(str.str(), *it) ?
     
    18821903                if (!CheckConfigVal<uint16_t>(conf, FAD::kMaxDacValue, "dac.",    *it) &&
    18831904                    !CheckConfigVal<uint16_t>(conf, FAD::kMaxDacValue, str.str(), *it))
    1884                     return 5;
     1905                    return 6;
    18851906
    18861907                target.fDac[i] = conf.HasDef(str.str(), *it) ?
     
    19031924            const tcp::endpoint endpoint = GetEndpoint(addr);
    19041925            if (endpoint==tcp::endpoint())
    1905                 return 1;
     1926                return 7;
    19061927
    19071928            for (int i=0; i<num; i++)
     
    19231944            const tcp::endpoint endpoint = GetEndpoint(base);
    19241945            if (endpoint==tcp::endpoint())
    1925                 return 10;
     1946                return 8;
    19261947
    19271948            const ba::ip::address_v4::bytes_type ip = endpoint.address().to_v4().to_bytes();
     
    19301951            {
    19311952                T::Out() << kRed << "EvalConfiguration - IP address given by --base-addr out-of-range." << endl;
    1932                 return 11;
     1953                return 9;
    19331954            }
    19341955
     
    19571978                const tcp::endpoint endpoint = GetEndpoint(*i);
    19581979                if (endpoint==tcp::endpoint())
    1959                     return 12;
     1980                    return 10;
    19601981
    19611982                AddEndpoint(endpoint);
Note: See TracChangeset for help on using the changeset viewer.