|
| 1 | +#include <cmath> |
| 2 | +#include <fmt/format.h> |
| 3 | +#include <string_view> |
| 4 | +#include <fstream> |
| 5 | + |
| 6 | +#include "TSystem.h" |
| 7 | + |
| 8 | +#include "CCDB/CcdbApi.h" |
| 9 | +#include "DataFormatsTPC/LtrCalibData.h" |
| 10 | +#include "TPCBase/ParameterGas.h" |
| 11 | +#include <TJAlienCredentials.h> |
| 12 | + |
| 13 | +float getTPCvdrift(int run, std::string_view ltrUrl = "http://alice-ccdb.cern.ch") |
| 14 | +{ |
| 15 | + o2::ccdb::CcdbApi c; |
| 16 | + c.init("http://alice-ccdb.cern.ch"); |
| 17 | + std::map<std::string, std::string> headers, metadataRCT, metadata, mm; |
| 18 | + headers = c.retrieveHeaders(fmt::format("RCT/Info/RunInformation/{}", run), metadataRCT, -1); |
| 19 | + printf("\nLooking for vdrift for run %d\n", run); |
| 20 | + const auto sor = std::stol(headers["SOR"].data()); |
| 21 | + |
| 22 | + const auto defaultDriftV = o2::tpc::ParameterGas::Instance().DriftV; |
| 23 | + |
| 24 | + std::string_view calibType = "TPC/Calib/LaserTracks"; |
| 25 | + // |
| 26 | + // query present run up to +-3days |
| 27 | + const auto queryInterval = 3l * 24l * 60l * 60l * 1000l; |
| 28 | + TJAlienCredentials* cred = new TJAlienCredentials(); |
| 29 | + cred->loadCredentials(); |
| 30 | + cred->selectPreferedCredentials(); |
| 31 | + CredentialsKind cmk = cred->getPreferedCredentials(); |
| 32 | + TJAlienCredentialsObject cmo = cred->get(cmk); |
| 33 | + |
| 34 | + |
| 35 | + const auto queryString = fmt::format("curl --cert {} --key {} --insecure -H \"If-Not-Before: {}\" -H \"If-Not-After: {}\" -H \"Accept: application/json\" {}/browse/{}", cmo.certpath.c_str(), cmo.keypath.c_str(), sor - queryInterval, sor + queryInterval, ltrUrl.data(), calibType.data()); |
| 36 | + fmt::print("Query: {}\n", queryString); |
| 37 | + const auto queryResultTString = gSystem->GetFromPipe(queryString.data()); |
| 38 | + std::string queryResult(queryResultTString); |
| 39 | + |
| 40 | + // find closest entry in time |
| 41 | + long minDist = 9999999999999; |
| 42 | + long minTime = sor; |
| 43 | + size_t pos = 0; |
| 44 | + const std::string_view searchString("validFrom"); |
| 45 | + while ((pos = queryResult.find(searchString.data(), pos)) < queryResult.size()) { |
| 46 | + const auto startPosTime = queryResult.find(":", pos) + 1; |
| 47 | + const auto endPosTime = queryResult.find(",", pos); |
| 48 | + const auto startValidity = std::atol(queryResult.substr(startPosTime, endPosTime - startPosTime).data()); |
| 49 | + fmt::print("add object {}\n", startValidity); |
| 50 | + if (std::abs(startValidity - sor) < minDist) { |
| 51 | + minTime = startValidity; |
| 52 | + minDist = std::abs(startValidity - sor); |
| 53 | + } |
| 54 | + pos = endPosTime; |
| 55 | + } |
| 56 | + fmt::print("{} closest to {} is at {}\n", calibType, sor, minTime); |
| 57 | + |
| 58 | + // |
| 59 | + // Get object closest to present run and return the drfit veloctiy calibration factor |
| 60 | + c.init(ltrUrl.data()); |
| 61 | + const auto ltrCalib = c.retrieveFromTFileAny<o2::tpc::LtrCalibData>(calibType.data(), metadata, minTime); /// timestamp in the run of interest |
| 62 | + const auto corr = ltrCalib->getDriftVCorrection(); |
| 63 | + float vcorr = defaultDriftV / corr; |
| 64 | + if (ltrCalib->refVDrift != 0) { |
| 65 | + printf("refVDrift different from zero: %f (default was %f)\n", ltrCalib->refVDrift, defaultDriftV); |
| 66 | + vcorr = ltrCalib->refVDrift / corr; |
| 67 | + } |
| 68 | + printf("vdrift = %f\n", vcorr); |
| 69 | + |
| 70 | + ofstream fp("vdrift.txt"); |
| 71 | + fp << vcorr << endl; |
| 72 | + fp.close(); |
| 73 | + |
| 74 | + return vcorr; |
| 75 | +} |
0 commit comments