|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// \file TrackSmearerService.cxx |
| 13 | +/// \brief Implementation for smearer service for the on-the-fly simulation |
| 14 | +/// \author Jesper Karlsson Gumprecht <jesper.gumprecht@cern.ch> |
| 15 | + |
| 16 | + |
| 17 | +#include "ALICE3/Core/TrackSmearerService.h" |
| 18 | +#include <Framework/CommonServices.h> |
| 19 | +#include <Framework/Plugins.h> |
| 20 | +#include <Framework/ServiceHandle.h> |
| 21 | +#include <Framework/ServiceSpec.h> |
| 22 | + |
| 23 | +#include <string> |
| 24 | +#include <TPDGCode.h> |
| 25 | + |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +bool o2::upgrade::TrackSmearerImpl::mIsInit = false; |
| 29 | +bool o2::upgrade::TrackSmearerImpl::mCleanLutWhenLoaded = true; |
| 30 | + |
| 31 | +void o2::upgrade::TrackSmearerImpl::initSmearer(o2::ccdb::BasicCCDBManager* ccdb, bool cleanLutWhenLoaded) |
| 32 | +{ |
| 33 | + if (mIsInit) { |
| 34 | + LOG(fatal) << "TrackSmearerImpl already initialized, cannot re-initialize"; |
| 35 | + } |
| 36 | + |
| 37 | + if (!ccdb) { |
| 38 | + LOG(fatal) << "CCDB manager is not set, cannot initialize TrackSmearerImpl"; |
| 39 | + } |
| 40 | + |
| 41 | + mIsInit = true; |
| 42 | + mCcdb = ccdb; |
| 43 | + mCcdb->setURL("http://alice-ccdb.cern.ch"); |
| 44 | + mCcdb->setTimestamp(-1); |
| 45 | + mCleanLutWhenLoaded = cleanLutWhenLoaded; |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +std::vector<std::unique_ptr<o2::delphes::DelphesO2TrackSmearer>> o2::upgrade::TrackSmearerImpl::smearerContainer; |
| 50 | +void o2::upgrade::TrackSmearerImpl::initCfg(int icfg, std::map<std::string, std::string> globalConfiguration) |
| 51 | +{ |
| 52 | + smearerContainer.emplace_back(std::make_unique<o2::delphes::DelphesO2TrackSmearer>()); |
| 53 | + smearerContainer[icfg]->setCcdbManager(mCcdb); |
| 54 | + smearerContainer[icfg]->setDownloadPath("./.ALICE3/"); |
| 55 | + smearerContainer[icfg]->setCleanupDownloadedFile(mCleanLutWhenLoaded); |
| 56 | + for (const auto& entry : globalConfiguration) { |
| 57 | + int pdg = 0; |
| 58 | + if (entry.first.find("lut") != 0) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + if (entry.first.find("lutEl") != std::string::npos) { |
| 62 | + pdg = kElectron; |
| 63 | + } else if (entry.first.find("lutMu") != std::string::npos) { |
| 64 | + pdg = kMuonMinus; |
| 65 | + } else if (entry.first.find("lutPi") != std::string::npos) { |
| 66 | + pdg = kPiPlus; |
| 67 | + } else if (entry.first.find("lutKa") != std::string::npos) { |
| 68 | + pdg = kKPlus; |
| 69 | + } else if (entry.first.find("lutPr") != std::string::npos) { |
| 70 | + pdg = kProton; |
| 71 | + } else if (entry.first.find("lutDe") != std::string::npos) { |
| 72 | + pdg = o2::constants::physics::kDeuteron; |
| 73 | + } else if (entry.first.find("lutTr") != std::string::npos) { |
| 74 | + pdg = o2::constants::physics::kTriton; |
| 75 | + } else if (entry.first.find("lutHe3") != std::string::npos) { |
| 76 | + pdg = o2::constants::physics::kHelium3; |
| 77 | + } else if (entry.first.find("lutAl") != std::string::npos) { |
| 78 | + pdg = o2::constants::physics::kAlpha; |
| 79 | + } |
| 80 | + |
| 81 | + std::string filename = entry.second; |
| 82 | + if (pdg == 0) { |
| 83 | + LOG(fatal) << "Unknown LUT entry " << entry.first << " for global configuration"; |
| 84 | + } |
| 85 | + LOG(info) << "Loading LUT for pdg " << pdg << " for config " << icfg << " from provided file '" << filename << "'"; |
| 86 | + if (filename.empty()) { |
| 87 | + LOG(warning) << "No LUT file passed for pdg " << pdg << ", skipping."; |
| 88 | + } |
| 89 | + // strip from leading/trailing spaces |
| 90 | + filename.erase(0, filename.find_first_not_of(" ")); |
| 91 | + filename.erase(filename.find_last_not_of(" ") + 1); |
| 92 | + if (filename.empty()) { |
| 93 | + LOG(warning) << "No LUT file passed for pdg " << pdg << ", skipping."; |
| 94 | + } |
| 95 | + bool success = smearerContainer[icfg]->loadTable(pdg, filename.c_str()); |
| 96 | + if (!success) { |
| 97 | + LOG(fatal) << "Having issue with loading the LUT " << pdg << " " << filename; |
| 98 | + } |
| 99 | + LOG(info) << "Successfully loaded LUT for pdg " << pdg; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +o2::delphes::DelphesO2TrackSmearer* o2::upgrade::TrackSmearerImpl::getSmearer(int icfg) |
| 104 | +{ |
| 105 | + if (icfg < 0 || icfg >= static_cast<int>(smearerContainer.size())) { |
| 106 | + LOG(fatal) << "Configuration index " << icfg << " out of bounds"; |
| 107 | + return nullptr; |
| 108 | + } |
| 109 | + if (!smearerContainer[icfg]) { |
| 110 | + LOG(fatal) << "nullptr entry in tracksmearer container"; |
| 111 | + return nullptr; |
| 112 | + } |
| 113 | + return smearerContainer[icfg].get(); |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +void o2::upgrade::TrackSmearerImpl::setReady() |
| 118 | +{ |
| 119 | + std::ofstream okFile(".TrackSmearerOK"); |
| 120 | + okFile.close(); |
| 121 | + LOG(info) << "Track smearer ready"; |
| 122 | +} |
| 123 | + |
| 124 | +void o2::upgrade::TrackSmearerImpl::waitReady() |
| 125 | +{ |
| 126 | + while (!std::ifstream(".TrackSmearerOK")) { |
| 127 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +struct TrackSmearerSupport : o2::framework::ServicePlugin { |
| 132 | + o2::framework::ServiceSpec* create() final |
| 133 | + { |
| 134 | + return new o2::framework::ServiceSpec{ |
| 135 | + .name = "track-smearer", |
| 136 | + .init = [](o2::framework::ServiceRegistryRef, o2::framework::DeviceState&, fair::mq::ProgOptions&) -> o2::framework::ServiceHandle { |
| 137 | + auto* wrapper = new o2::upgrade::TrackSmearerContainer(); |
| 138 | + auto* ptr = new o2::upgrade::TrackSmearerImpl(); |
| 139 | + wrapper->setInstance(ptr); |
| 140 | + return o2::framework::ServiceHandle{o2::framework::TypeIdHelpers::uniqueId<o2::upgrade::TrackSmearerContainer>(), |
| 141 | + wrapper, |
| 142 | + o2::framework::ServiceKind::Serial, |
| 143 | + "database-pdg"}; |
| 144 | + }, |
| 145 | + .configure = o2::framework::CommonServices::noConfiguration(), |
| 146 | + .exit = [](o2::framework::ServiceRegistryRef, void* service) { |
| 147 | + auto* s = reinterpret_cast<o2::upgrade::TrackSmearerContainer*>(service); |
| 148 | + delete s; |
| 149 | + }, |
| 150 | + .kind = o2::framework::ServiceKind::Serial |
| 151 | + }; |
| 152 | + } |
| 153 | +}; |
| 154 | + |
| 155 | +DEFINE_DPL_PLUGINS_BEGIN |
| 156 | +DEFINE_DPL_PLUGIN_INSTANCE(TrackSmearerSupport, CustomService); |
| 157 | +DEFINE_DPL_PLUGINS_END |
| 158 | + |
0 commit comments