Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:32

0001 
0002 // Copyright 2012-2025, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 // Author: David Lawrence
0005 
0006 #pragma once
0007 #include <string>
0008 
0009 using std::string;
0010 
0011 #include <JANA/Calibrations/JCalibrationFile.h>
0012 #include <JANA/Services/JParameterManager.h>
0013 
0014 
0015 /// The JResource class is used to manage local resource files.
0016 /// These would typically be larger files that are costly to download
0017 /// every time the program is run. (i.e. if they come from the
0018 /// JCalibration system.) Files are kept in a central location
0019 /// on the local filesystem so only one copy needs to exist.
0020 ///
0021 /// The JResource constructor takes two arguments. The first
0022 /// is a pointer to a JCalibration object. If not NULL, this is used
0023 /// to retrieve the URL of the file's location on the web. This allows
0024 /// the JResource to automatically download the file if needed.
0025 ///
0026 /// If no JCalibration object is supplied (i.e. it's NULL), then it will
0027 /// simply look for a file with the specified namepath relative to the
0028 /// root directory used to hold the resources. The root resource directory
0029 /// can be specified multiple ways. These are, in order of precedence:
0030 ///
0031 /// 1. Passed as second argument to the constructor
0032 /// 2. Specified in JANA:RESOURCE_DIR configuration parameter
0033 /// 3. Specified in JANA_RESOURCE_DIR environment variable
0034 /// 4. Specified in JANA:RESOURCE_DEFAULT_PATH configuration parameter
0035 /// 5. Create a user directory in /tmp called "resources"
0036 ///
0037 /// Note that in nearly all instances, no second argument should
0038 /// be passed to the constructor so that the value can be changed
0039 /// via run time parameters.
0040 ///
0041 /// Resource files can be of any format but if they are in an ASCII
0042 /// format compatible with JCalibrationFile then the file can be
0043 /// parsed using the Get(...) methods of JResource which map
0044 /// directly to those of JCalibrationFile.
0045 ///
0046 /// To get the location of a resource on the local file system,
0047 /// use the method:
0048 ///
0049 /// string GetResource(string namepath);
0050 ///
0051 /// This will return a string with the full path to the resource file on
0052 /// the local file system. The call will automatically download
0053 /// the resource and install it if it does not already exist locally.
0054 /// The download location will be retrieved using the specified
0055 /// namepath and the JCalibration object passed in to the constructor.
0056 /// The calibration DB should have an entry for the namepath that is
0057 /// a map of key-values with two options for how the URL is specified:
0058 ///
0059 /// Option 1.) The DB provides a "URL_base" string and a "path"
0060 /// string. These are combined to make the full URL, and the
0061 /// "path" is appended to the resource_dir to generate the local
0062 /// path. Alternatively, "URL_base" may be provided via the
0063 /// JANA:RESOURCE_URL configuration parameter in which case it need
0064 /// not be present in the calib DB. If the config. parameter is
0065 /// supplied, it will be used instead of any "URL_base" values found
0066 /// in the calib DB.
0067 ///
0068 /// Option 2.) The DB provides a "URL" string only. This is used
0069 /// as the full URL and as a key to the resources map to find
0070 /// the local path. If none exists, this local path is taken
0071 /// to be the namepath specified (appended to resource_dir).
0072 ///
0073 /// Option 1. takes precedent. If either the "URL_base" or "path"
0074 /// strings are present, then the other must be as well or an
0075 /// exception is thrown. If neither is present, then the URL
0076 /// string is checked and used. If it also does not exist, an
0077 /// exception is thrown.
0078 ///
0079 ///
0080 /// A text file named "resources" is maintained at the top level of
0081 /// the resources directory tree to record what URLs have been
0082 /// downloaded and where the files are stored. This file is necessary
0083 /// if option 2 above is used to store URLs in the calibration DB,
0084 /// but is only informational if option 1 is used. It is ignored
0085 /// completely if no calibration database is used.
0086 ///
0087 /// The templated Get(namepath, T vals [, event_number]) method will
0088 /// first call the GetResource() method described above, but will
0089 /// then use a JCalibrationFile object to parse the resource file,
0090 /// filling the container passed in for "vals". See the documentation
0091 /// for the JCalibration class for more info on the allowed types
0092 /// for "vals".
0093 
0094 
0095 class JResource {
0096 public:
0097     JResource(std::shared_ptr<JParameterManager> params, JCalibration *jcalib = NULL, string resource_dir = "");
0098 
0099     virtual ~JResource();
0100 
0101     template<class T>
0102     bool Get(string namepath, T &vals, int event_number = 0);
0103 
0104     string GetResource(string namepath);
0105 
0106     string GetLocalPathToResource(string namepath);
0107 
0108     map<string, string> GetLocalResources(void) { return resources; }
0109 
0110     JCalibration *GetJCalibration(void) { return jcalib; }
0111 
0112     void GetResourceFromURL(const string &URL, const string &fullpath);
0113 
0114     string Get_MD5(string fullpath);
0115 
0116 protected:
0117 
0118     // Used to get URL of remote resource
0119     JCalibration *jcalib;
0120 
0121     // Keep list of namepaths in JCalibration
0122     vector<string> calib_namepaths;
0123 
0124     // Used to convert files to values in STL containers
0125     JCalibrationFile *jcalibfile;
0126 
0127     // Full path to top-most directory of resource files
0128     string resource_dir;
0129 
0130     // Map of URLs to namepaths for existing resources
0131     // key is URL and value is relative path (which should
0132     // be the same as the namepath)
0133     map<string, string> resources;
0134 
0135     void ReadResourceInfoFile(void);
0136 
0137     void WriteResourceInfoFile(void);
0138 
0139     // Argument for the external curl program in case it is used
0140     string curl_args;
0141 
0142 private:
0143 
0144     // Holds user specified URL_base that superceeds any found
0145     // in calib DB
0146     bool overide_URL_base;
0147     string URL_base;
0148     bool check_md5;
0149 
0150 };
0151 
0152 //----------------------
0153 // Get
0154 //----------------------
0155 template<class T>
0156 bool JResource::Get(string namepath, T &vals, int event_number) {
0157     /// Get the specified resource and parse it, placing the values in the
0158     /// specified "vals" container. This first calls GetResource(namepath)
0159     /// to download the resource (if necessary) and then uses a
0160     /// JCalibrationFile::Get() method to parse the file and fill the
0161     /// "vals" container.
0162 
0163     // Call to GetResource to (optionally) download and install resource file
0164     string fullpath = GetResource(namepath);
0165     string path = fullpath.substr(resource_dir.size() + 1); // chop off resource_dir + "/"
0166 
0167     // Have JCalibrationFile parse the resource file
0168     return jcalibfile->Get(path, vals, event_number);
0169 }
0170 
0171 
0172 using JLargeCalibration [[deprecated("Renamed to JResource")]]= JResource;