-
Notifications
You must be signed in to change notification settings - Fork 482
Default TPC loopers in o2-sim #14851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jackal1-66
wants to merge
12
commits into
AliceO2Group:dev
Choose a base branch
from
jackal1-66:loopers
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f60eb58
First implementation of loopers inclusion in base Generator class
jackal1-66 8457c65
Merge branch 'AliceO2Group:dev' into loopers
jackal1-66 045213b
Various improvements
jackal1-66 bc55d0a
Vetoing loopers for FlatGas and \!collisioncontext
jackal1-66 883bc8e
Implemented rate and collision system dependence (default)
jackal1-66 c156a31
Set automatic interaction rate from collision context
jackal1-66 19e77a3
Fixed bug + cleaned code
jackal1-66 b773e93
Improved logging + colsys check
jackal1-66 b81a2ee
Please consider the following formatting changes
alibuild 5669efa
Merge pull request #17 from alibuild/alibot-cleanup-14851
jackal1-66 797f283
Add copyright headers
jackal1-66 ba6a1a7
Corrected include folder
jackal1-66 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // Copyright 2024-2025 CERN and copyright holders of ALICE O2. | ||
| // See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. | ||
| // All rights not expressly granted are reserved. | ||
| // | ||
| // This software is distributed under the terms of the GNU General Public | ||
| // License v3 (GPL Version 3), copied verbatim in the file "COPYING". | ||
| // | ||
| // In applying this license CERN does not waive the privileges and immunities | ||
| // granted to it by virtue of its status as an Intergovernmental Organization | ||
| // or submit itself to any jurisdiction. | ||
|
|
||
| /// \author M+Giacalone - September 2025 | ||
|
|
||
| #ifndef ALICEO2_EVENTGEN_TPCLOOPERS_H_ | ||
| #define ALICEO2_EVENTGEN_TPCLOOPERS_H_ | ||
|
|
||
| #ifdef GENERATORS_WITH_TPCLOOPERS | ||
| #include <onnxruntime_cxx_api.h> | ||
| #endif | ||
| #include <vector> | ||
| #include <rapidjson/document.h> | ||
| #include "TRandom3.h" | ||
| #include <SimulationDataFormat/DigitizationContext.h> | ||
| #include "TParticle.h" | ||
|
|
||
| #ifdef GENERATORS_WITH_TPCLOOPERS | ||
| // Static Ort::Env instance for multiple onnx model loading | ||
| extern Ort::Env global_env; | ||
|
|
||
| // This class is responsible for loading the scaler parameters from a JSON file | ||
| // and applying the inverse transformation to the generated data. | ||
| // Inferenced output is scaled (min-max normalization or robust scaling for outlier features) during training, | ||
| // so we need to revert this transformation to get physical values. | ||
| struct Scaler { | ||
| std::vector<double> normal_min; | ||
| std::vector<double> normal_max; | ||
| std::vector<double> outlier_center; | ||
| std::vector<double> outlier_scale; | ||
|
|
||
| void load(const std::string& filename); | ||
|
|
||
| std::vector<double> inverse_transform(const std::vector<double>& input); | ||
|
|
||
| private: | ||
| std::vector<double> jsonArrayToVector(const rapidjson::Value& jsonArray); | ||
| }; | ||
|
|
||
| // This class loads the ONNX model and generates samples using it. | ||
| class ONNXGenerator | ||
| { | ||
| public: | ||
| ONNXGenerator(Ort::Env& shared_env, const std::string& model_path); | ||
|
|
||
| std::vector<double> generate_sample(); | ||
|
|
||
| private: | ||
| Ort::Env& env; | ||
| Ort::Session session; | ||
| TRandom3 rand_gen; | ||
| }; | ||
| #endif // GENERATORS_WITH_TPCLOOPERS | ||
|
|
||
| namespace o2 | ||
| { | ||
| namespace eventgen | ||
| { | ||
|
|
||
| #ifdef GENERATORS_WITH_TPCLOOPERS | ||
| /** | ||
| * Generator for TPC Loopers based on pre-trained ONNX models. | ||
| * Currently it generates loopers as electron-positron pairs and Compton electrons | ||
| * according to specified distributions and parameters. | ||
| * This can be extended to other types of background processes in the future (e.g. slow neutron spallation products, saturation tail). | ||
| * Multiple configuration options are available: | ||
| * - Flat gas: loopers are generated uniformly per event taking a reference value which can be either the LHC orbit time or the average interaction time record interval from the collision context. | ||
| * ==> Current automatic setup (default) sets the interaction rate automatically from the collision context and the reference value per orbit is calculated from an external file. | ||
| * ==> Number of loopers per orbit can be adjusted via a specific parameter. | ||
| * - Poisson + Gaussian sampling: number of loopers are sampled from Poissonian (for pairs) and Gaussian (for Compton electrons) distributions based on provided parameters. | ||
| * ==> flat gas must be disabled to use this option. | ||
| * - Fixed number of loopers per event | ||
| * ==> flat gas must be disabled to use this option and Poissonian/Gaussian parameters file should be set to None | ||
| */ | ||
| class GenTPCLoopers | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a small doxygen style class description would be good |
||
| { | ||
| public: | ||
| GenTPCLoopers(std::string model_pairs = "tpcloopmodel.onnx", std::string model_compton = "tpcloopmodelcompton.onnx", | ||
| std::string poisson = "poisson.csv", std::string gauss = "gauss.csv", std::string scaler_pair = "scaler_pair.json", | ||
| std::string scaler_compton = "scaler_compton.json"); | ||
|
|
||
| Bool_t generateEvent(); | ||
|
|
||
| Bool_t generateEvent(double time_limit); | ||
|
|
||
| std::vector<TParticle> importParticles(); | ||
|
|
||
| unsigned int PoissonPairs(); | ||
|
|
||
| unsigned int GaussianElectrons(); | ||
|
|
||
| void SetNLoopers(unsigned int nsig_pair, unsigned int nsig_compton); | ||
|
|
||
| void SetMultiplier(const std::array<float, 2>& mult); | ||
|
|
||
| void setFlatGas(Bool_t flat, Int_t number = -1, Int_t nloopers_orbit = -1); | ||
|
|
||
| void setFractionPairs(float fractionPairs); | ||
|
|
||
| void SetRate(const std::string& rateFile, bool isPbPb, int intRate = 50000); | ||
|
|
||
| void SetAdjust(float adjust = 0.f); | ||
|
|
||
| unsigned int getNLoopers() const { return (mNLoopersPairs + mNLoopersCompton); } | ||
|
|
||
| private: | ||
| std::unique_ptr<ONNXGenerator> mONNX_pair = nullptr; | ||
| std::unique_ptr<ONNXGenerator> mONNX_compton = nullptr; | ||
| std::unique_ptr<Scaler> mScaler_pair = nullptr; | ||
| std::unique_ptr<Scaler> mScaler_compton = nullptr; | ||
| double mPoisson[3] = {0.0, 0.0, 0.0}; // Mu, Min and Max of Poissonian | ||
| double mGauss[4] = {0.0, 0.0, 0.0, 0.0}; // Mean, Std, Min, Max | ||
| std::vector<std::vector<double>> mGenPairs; | ||
| std::vector<std::vector<double>> mGenElectrons; | ||
| unsigned int mNLoopersPairs = -1; | ||
| unsigned int mNLoopersCompton = -1; | ||
| std::array<float, 2> mMultiplier = {1., 1.}; | ||
| bool mPoissonSet = false; | ||
| bool mGaussSet = false; | ||
| // Random number generator | ||
| TRandom3 mRandGen; | ||
| int mCurrentEvent = 0; // Current event number, used for adaptive loopers | ||
| TFile* mContextFile = nullptr; // Input collision context file | ||
| o2::steer::DigitizationContext* mCollisionContext = nullptr; // Pointer to the digitization context | ||
| std::vector<o2::InteractionTimeRecord> mInteractionTimeRecords; // Interaction time records from collision context | ||
| Bool_t mFlatGas = false; // Flag to indicate if flat gas loopers are used | ||
| Bool_t mFlatGasOrbit = false; // Flag to indicate if flat gas loopers are per orbit | ||
| Int_t mFlatGasNumber = -1; // Number of flat gas loopers per event | ||
| double mIntTimeRecMean = 1.0; // Average interaction time record used for the reference | ||
| double mTimeLimit = 0.0; // Time limit for the current event | ||
| double mTimeEnd = 0.0; // Time limit for the last event | ||
| float mLoopsFractionPairs = 0.08; // Fraction of loopers from Pairs | ||
| int mInteractionRate = 50000; // Interaction rate in Hz | ||
| }; | ||
| #endif // GENERATORS_WITH_TPCLOOPERS | ||
|
|
||
| } // namespace eventgen | ||
| } // namespace o2 | ||
|
|
||
| #endif // ALICEO2_EVENTGEN_TPCLOOPERS_H_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright 2024-2025 CERN and copyright holders of ALICE O2. | ||
| // See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. | ||
| // All rights not expressly granted are reserved. | ||
| // | ||
| // This software is distributed under the terms of the GNU General Public | ||
| // License v3 (GPL Version 3), copied verbatim in the file "COPYING". | ||
| // | ||
| // In applying this license CERN does not waive the privileges and immunities | ||
| // granted to it by virtue of its status as an Intergovernmental Organization | ||
| // or submit itself to any jurisdiction. | ||
|
|
||
| /// \author M+Giacalone - September 2025 | ||
|
|
||
| #ifndef ALICEO2_EVENTGEN_TPCLOOPERSPARAM_H_ | ||
| #define ALICEO2_EVENTGEN_TPCLOOPERSPARAM_H_ | ||
|
|
||
| #include "CommonUtils/ConfigurableParam.h" | ||
| #include "CommonUtils/ConfigurableParamHelper.h" | ||
|
|
||
| namespace o2 | ||
| { | ||
| namespace eventgen | ||
| { | ||
|
|
||
| /** | ||
| ** a parameter class/struct to keep the settings of | ||
| ** the TPC loopers event-generator and | ||
| ** allow the user to modify them | ||
| **/ | ||
| struct GenTPCLoopersParam : public o2::conf::ConfigurableParamHelper<GenTPCLoopersParam> { | ||
| bool loopersVeto = false; // if true, no loopers are generated | ||
| // Current files are set to custom user CCDB paths, TO BE CHANGED | ||
| std::string model_pairs = "ccdb://Users/m/mgiacalo/WGAN_ExtGenPair"; // ONNX model for e+e- pair production | ||
| std::string model_compton = "ccdb://Users/m/mgiacalo/WGAN_ExtGenCompton"; // ONNX model for Compton scattering | ||
| std::string poisson = "${O2_ROOT}/share/Generators/TPCLoopers/poisson_params.csv"; // file with Poissonian parameters | ||
| std::string gauss = "${O2_ROOT}/share/Generators/TPCLoopers/gaussian_params.csv"; // file with Gaussian parameters | ||
| std::string scaler_pair = "${O2_ROOT}/share/Generators/TPCLoopers/ScalerPairParams.json"; // file with scaler parameters for e+e- pair production | ||
| std::string scaler_compton = "${O2_ROOT}/share/Generators/TPCLoopers/ScalerComptonParams.json"; // file with scaler parameters for Compton scattering | ||
| std::string nclxrate = "ccdb://Users/m/mgiacalo/ClustersTrackRatio"; // file with clusters/rate information per orbit | ||
| std::string colsys = "PbPb"; // collision system (PbPb or pp) | ||
| int intrate = -1; // Automatic IR from collision context if -1, else user-defined interaction rate in Hz | ||
| bool flat_gas = true; // if true, the gas density is considered flat in the TPC volume | ||
| unsigned int nFlatGasLoopers = 500; // number of loopers to be generated per event in case of flat gas [currently unused, kept for possible future debug developments] | ||
| float fraction_pairs = 0.08; // fraction of loopers [currently unused, kept for possible future debug developments] | ||
| float multiplier[2] = {1., 1.}; // multiplier for pairs and compton loopers for Poissonian and Gaussian sampling | ||
| unsigned int fixedNLoopers[2] = {1, 1}; // fixed number of loopers coming from pairs and compton electrons - valid if flat gas is false and both Poisson and Gaussian params files are empty | ||
| float adjust_flatgas = 0.f; // adjustment for the number of flat gas loopers per orbit (in percentage, e.g. -0.1 = -10%) [-1, inf)] | ||
| O2ParamDef(GenTPCLoopersParam, "GenTPCLoopers"); | ||
| }; | ||
|
|
||
| } // end namespace eventgen | ||
| } // end namespace o2 | ||
|
|
||
| #endif // ALICEO2_EVENTGEN_TPCLOOPERSPARAM_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # TPC Loopers Generator - Parameter Files | ||
|
|
||
| This directory contains parameter files used by the TPC Loopers event generator in ALICE O2. | ||
|
|
||
| ## Overview | ||
|
|
||
| The TPC Loopers generator uses pre-trained ONNX models to generate realistic looper particles based on machine learning models trained on full GEANT4 slow neutron transport simulations. The parameter files in this directory provide: | ||
| - Example statistical distribution parameters for sampling the number of loopers per event | ||
| - **Mandatory** scaling parameters for transforming the ONNX model outputs to physical values | ||
|
|
||
| ## Files Description | ||
|
|
||
| ### Statistical Sampling Parameters | ||
|
|
||
| The files provided in the folder are examples based on the training dataset. | ||
|
|
||
| #### `gaussian_params.csv` | ||
| Parameters for Gaussian distribution used to sample the number of Compton electrons per event. | ||
|
|
||
| **Format:** Four values (one per line) | ||
| 1. Mean (μ) | ||
| 2. Standard deviation (σ) | ||
| 3. Minimum value | ||
| 4. Maximum value | ||
|
|
||
| #### `poisson_params.csv` | ||
| Parameters for Poisson distribution used to sample the number of electron-positron pairs per event. | ||
|
|
||
| **Format:** Three values (one per line) | ||
| 1. Lambda (λ) parameter | ||
| 2. Minimum value | ||
| 3. Maximum value | ||
|
|
||
| ### Scaler Parameters | ||
|
|
||
| These JSON files contain the parameters for inverse transformation of the ONNX models output. They should be kept as they are | ||
| unless a new version of the models is released. | ||
|
|
||
| #### `ScalerComptonParams.json` | ||
| Scaler parameters for Compton electron generation model. | ||
|
|
||
| **Structure:** | ||
| ```json | ||
| { | ||
| "normal": { | ||
| "min": [array of 5 min values for min-max normalization], | ||
| "max": [array of 5 max values for min-max normalization] | ||
| }, | ||
| "outlier": { | ||
| "center": [array of 2 center values for robust scaling], | ||
| "scale": [array of 2 scale values for robust scaling] | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| - **normal**: Min-max normalization parameters for standard features (`Px`, `Py`, `Pz`, `VertexCoordinatesX`, `VertexCoordinatesY`) | ||
| - **outlier**: Robust scaler parameters (center and scale) for outlier features (`VertexCoordinatesZ`,`time`) | ||
|
|
||
| #### `ScalerPairParams.json` | ||
| Scaler parameters for electron-positron pair generation model. | ||
|
|
||
| **Structure:** | ||
| ```json | ||
| { | ||
| "normal": { | ||
| "min": [array of 8 min values for min-max normalization], | ||
| "max": [array of 8 max values for min-max normalization] | ||
| }, | ||
| "outlier": { | ||
| "center": [array of 2 center values for robust scaling], | ||
| "scale": [array of 2 scale values for robust scaling] | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| - **normal**: Min-max normalization parameters for standard features (`Px_e`, `Py_e`, `Pz_e`,`Px_p`, `Py_p`, `Pz_p`, `VertexCoordinatesX`, `VertexCoordinatesY`) | ||
| - **outlier**: Robust scaler parameters (center and scale) for outlier features (`VertexCoordinatesZ`,`time`) | ||
| --- | ||
| *Author: M. Giacalone - September 2025* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
jackal1-66 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "normal": { | ||
| "min": [ | ||
| -0.0108811147511005, | ||
| -0.0098758740350604, | ||
| -0.0103233363479375, | ||
| -260.0542297363281, | ||
| -259.80059814453125 | ||
| ], | ||
| "max": [ | ||
| 0.0108060473576188, | ||
| 0.0103057539090514, | ||
| 0.0106524610891938, | ||
| 260.0343933105469, | ||
| 259.62890625 | ||
| ] | ||
| }, | ||
| "outlier": { | ||
| "center": [ | ||
| -71.39387130737305, | ||
| 96791.23828125 | ||
| ], | ||
| "scale": [ | ||
| 265.9389114379883, | ||
| 230762.30981445312 | ||
| ] | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.