File indexing completed on 2026-06-02 08:51:49
0001 #ifndef CONVOL_COEFF_FUNCTION_SERVICE_H
0002 #define CONVOL_COEFF_FUNCTION_SERVICE_H
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <ElementaryUtils/logger/CustomException.h>
0012 #include <ElementaryUtils/parameters/GenericType.h>
0013 #include <ElementaryUtils/PropertiesManager.h>
0014 #include <ElementaryUtils/string_utils/Formatter.h>
0015 #include <ElementaryUtils/string_utils/StringUtils.h>
0016 #include <ElementaryUtils/thread/Packet.h>
0017 #include <string>
0018
0019 #include "../beans/automation/Task.h"
0020 #include "../beans/gpd/GPDType.h"
0021 #include "../beans/List.h"
0022 #include "../modules/convol_coeff_function/ConvolCoeffFunctionModule.h"
0023 #include "../ServiceObjectTyped.h"
0024 #include "../utils/VectorUtils.h"
0025
0026 namespace PARTONS {
0027 class GPDService;
0028 }
0029
0030 namespace PARTONS {
0031
0032
0033
0034
0035
0036
0037 template<typename KinematicType, typename ResultType> class ConvolCoeffFunctionService: public ServiceObjectTyped<
0038 KinematicType, ResultType> {
0039
0040 public:
0041
0042 static const std::string CCF_SERVICE_COMPUTE_SINGLE_KINEMATIC;
0043 static const std::string CCF_SERVICE_COMPUTE_MANY_KINEMATIC;
0044
0045
0046
0047
0048 virtual ~ConvolCoeffFunctionService() {
0049 }
0050
0051 virtual void resolveObjectDependencies() {
0052
0053 ServiceObjectTyped<KinematicType, ResultType>::resolveObjectDependencies();
0054
0055 try {
0056 this->m_batchSize = ElemUtils::GenericType(
0057 ElemUtils::PropertiesManager::getInstance()->getString(
0058 "ccf.service.batch.size")).toUInt();
0059 } catch (const std::exception &e) {
0060 throw ElemUtils::CustomException(this->getClassName(), __func__,
0061 e.what());
0062 }
0063 }
0064
0065 virtual void computeTask(Task &task) {
0066
0067 ServiceObjectTyped<KinematicType, ResultType>::computeTask(task);
0068
0069 List<ResultType> resultList;
0070
0071 if (ElemUtils::StringUtils::equals(task.getFunctionName(),
0072 ConvolCoeffFunctionService::CCF_SERVICE_COMPUTE_MANY_KINEMATIC)) {
0073 resultList = computeManyKinematicTask(task);
0074 }
0075
0076 else if (ElemUtils::StringUtils::equals(task.getFunctionName(),
0077 ConvolCoeffFunctionService::CCF_SERVICE_COMPUTE_SINGLE_KINEMATIC)) {
0078 resultList.add(computeSingleKinematicTask(task));
0079
0080 }
0081
0082 else if (!this->computeGeneralTask(task)) {
0083 this->errorUnknownMethod(task);
0084 }
0085
0086 this->updateResultInfo(resultList, this->m_resultInfo);
0087
0088 this->m_resultListBuffer = resultList;
0089 }
0090
0091
0092
0093
0094
0095
0096
0097
0098 ResultType computeSingleKinematic(const KinematicType &kinematic,
0099 ConvolCoeffFunctionModule<KinematicType, ResultType>* pConvolCoeffFunctionModule,
0100 const List<GPDType>& gpdTypeList = List<GPDType>()) const {
0101
0102
0103 List<GPDType> restrictedByGPDTypeListFinal = getFinalGPDTypeList(
0104 pConvolCoeffFunctionModule, gpdTypeList);
0105
0106
0107 return pConvolCoeffFunctionModule->compute(kinematic,
0108 restrictedByGPDTypeListFinal);
0109 }
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119 List<ResultType> computeManyKinematic(List<KinematicType> &kinematics,
0120 ConvolCoeffFunctionModule<KinematicType, ResultType>* pConvolCoeffFunctionModule,
0121 const List<GPDType>& gpdTypeList = List<GPDType>()) {
0122
0123
0124 this->debug(__func__,
0125 ElemUtils::Formatter() << kinematics.size()
0126 << " CCF kinematic(s) will be computed with "
0127 << pConvolCoeffFunctionModule->getClassName());
0128
0129
0130 List<ResultType> results;
0131 List<ElemUtils::Packet> listOfPacket;
0132 List<GPDType> finalGPDTypeList = getFinalGPDTypeList(
0133 pConvolCoeffFunctionModule, gpdTypeList);
0134
0135
0136 if (finalGPDTypeList.size() != 0) {
0137
0138
0139 this->initComputationalThread(pConvolCoeffFunctionModule);
0140
0141
0142 this->info(__func__, "Thread(s) running ...");
0143
0144
0145 unsigned int i = 0;
0146 unsigned int j = 0;
0147
0148
0149 while (i != kinematics.size()) {
0150
0151 listOfPacket.clear();
0152 j = 0;
0153
0154 while ((j != this->m_batchSize) && (i != kinematics.size())) {
0155
0156 ElemUtils::Packet packet;
0157 KinematicType kinematic;
0158 kinematic = kinematics[i];
0159 packet << kinematic << finalGPDTypeList;
0160 listOfPacket.add(packet);
0161 i++;
0162 j++;
0163 }
0164
0165
0166 this->addTasks(listOfPacket);
0167 this->launchAllThreadAndWaitingFor();
0168 this->sortResultList();
0169
0170
0171 this->info(__func__,
0172 ElemUtils::Formatter()
0173 << "Kinematic(s) already computed: " << i);
0174
0175
0176 this->updateResultInfo(this->getResultList(),
0177 this->m_resultInfo);
0178
0179
0180 results.add(this->getResultList());
0181
0182
0183 this->clearResultListBuffer();
0184 }
0185
0186
0187 this->clearAllThread();
0188
0189 } else {
0190 this->info(__func__,
0191 "Nothing to compute with your computation configuration ; there is no GPDType available");
0192 }
0193
0194 return results;
0195 }
0196
0197
0198
0199
0200
0201
0202 virtual ConvolCoeffFunctionModule<KinematicType, ResultType>* newConvolCoeffFunctionModuleFromTask(
0203 const Task &task) const = 0;
0204
0205
0206
0207
0208
0209
0210 virtual KinematicType newKinematicFromTask(const Task &task) const = 0;
0211
0212
0213
0214
0215
0216
0217 virtual List<KinematicType> newListOfKinematicFromTask(
0218 const Task &task) const = 0;
0219
0220 protected:
0221
0222
0223
0224
0225 ConvolCoeffFunctionService(const std::string &className) :
0226 ServiceObjectTyped<KinematicType, ResultType>(className) {
0227 }
0228
0229 private:
0230
0231
0232
0233
0234
0235
0236 ResultType computeSingleKinematicTask(Task &task) const {
0237
0238
0239 KinematicType kinematic = newKinematicFromTask(task);
0240
0241
0242 List<GPDType> gpdTypeList = this->getGPDTypeListFromTask(task);
0243
0244
0245 ConvolCoeffFunctionModule<KinematicType, ResultType>* pConvolCoeffFunctionModule =
0246 newConvolCoeffFunctionModuleFromTask(task);
0247
0248
0249 ResultType result = computeSingleKinematic(kinematic,
0250 pConvolCoeffFunctionModule, gpdTypeList);
0251
0252
0253 this->m_pModuleObjectFactory->updateModulePointerReference(
0254 pConvolCoeffFunctionModule, 0);
0255 pConvolCoeffFunctionModule = 0;
0256
0257
0258 return result;
0259 }
0260
0261
0262
0263
0264
0265
0266 List<ResultType> computeManyKinematicTask(Task& task) {
0267
0268
0269 List<KinematicType> listOfKinematic = newListOfKinematicFromTask(task);
0270
0271
0272 List<GPDType> gpdTypeList = this->getGPDTypeListFromTask(task);
0273
0274
0275 ConvolCoeffFunctionModule<KinematicType, ResultType>* pConvolCoeffFunctionModule =
0276 newConvolCoeffFunctionModuleFromTask(task);
0277
0278
0279 List<ResultType> results = computeManyKinematic(listOfKinematic,
0280 pConvolCoeffFunctionModule, gpdTypeList);
0281
0282
0283 this->m_pModuleObjectFactory->updateModulePointerReference(
0284 pConvolCoeffFunctionModule, 0);
0285 pConvolCoeffFunctionModule = 0;
0286
0287
0288 return results;
0289 }
0290
0291
0292
0293
0294
0295
0296
0297 List<GPDType> getFinalGPDTypeList(
0298 ConvolCoeffFunctionModule<KinematicType, ResultType>* pConvolCoeffFunctionModule,
0299 const List<GPDType> &gpdTypeList) const {
0300
0301
0302 List<GPDType> restrictedByGPDTypeListFinal = gpdTypeList;
0303
0304
0305 restrictedByGPDTypeListFinal =
0306 pConvolCoeffFunctionModule->getListOfAvailableGPDTypeForComputation();
0307
0308
0309 if (!gpdTypeList.isEmpty()) {
0310 restrictedByGPDTypeListFinal = VectorUtils::intersection(
0311 restrictedByGPDTypeListFinal, gpdTypeList);
0312 }
0313
0314
0315 this->debug(__func__,
0316 ElemUtils::Formatter() << restrictedByGPDTypeListFinal.size()
0317 << " GPDType will be computed");
0318
0319
0320 return restrictedByGPDTypeListFinal;
0321 }
0322 };
0323
0324 template<typename KinematicType, typename ResultType>
0325 const std::string ConvolCoeffFunctionService<KinematicType, ResultType>::CCF_SERVICE_COMPUTE_SINGLE_KINEMATIC =
0326 "computeSingleKinematic";
0327
0328 template<typename KinematicType, typename ResultType>
0329 const std::string ConvolCoeffFunctionService<KinematicType, ResultType>::CCF_SERVICE_COMPUTE_MANY_KINEMATIC =
0330 "computeManyKinematic";
0331
0332 }
0333
0334 #endif