Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:58:39

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 
0027 // Author: Ivana Hrivnacova, 04/07/2012  (ivana@ipno.in2p3.fr)
0028 
0029 #ifndef G4MergeMode_h
0030 #define G4MergeMode_h 1
0031 
0032 #include "globals.hh"
0033 #include "G4Exception.hh"
0034 
0035 #include <functional>
0036 #include <cmath>
0037 
0038 // Enumeration for definition available binning schemes
0039 
0040 enum class G4MergeMode {
0041   kAddition,        // "Or" if boolean type
0042   kMultiplication,  // "And" if boolean type
0043   kMaximum,         // "Or" if boolean type
0044   kMinimum          // "And" if boolean type
0045 };
0046 
0047 // Generic merge function
0048 template <typename T>
0049 using G4MergeFunction = std::function<T(const T&, const T&)>;
0050 
0051 // Utility functions
0052 namespace  G4Accumulables {
0053 
0054 G4MergeMode GetMergeMode(const G4String& mergeModeName);
0055 
0056 template <typename T>
0057 G4MergeFunction<T> GetMergeFunction(G4MergeMode mergeMode)
0058 {
0059   switch ( mergeMode ) {
0060     case G4MergeMode::kAddition:
0061       // return std::bind([](const T& x, const T& y) { return x + y; });
0062       return [](const T& x, const T& y) { return x + y; };
0063 
0064     case G4MergeMode::kMultiplication:
0065       return [](const T& x, const T& y) { return x * y; };
0066 
0067     case G4MergeMode::kMaximum:
0068       // return std::bind([](const T& x, const T& y) { return std::max(x,y);});
0069       return [](const T& x, const T& y) { return std::max(x,y); };
0070 
0071     case G4MergeMode::kMinimum:
0072       // return std::bind([](const T& x, const T& y) { return std::min(x,y);});
0073       return [](const T& x, const T& y) { return std::min(x,y); };
0074   }
0075 
0076   // Must never get here, but we need a return to satisfy MSVC
0077   G4Exception("G4Accumulables::GetMergeFunction<T>",
0078             "Analysis_F001", FatalException,
0079             "Undefined merge mode");
0080   return [](const T&, const T&) { return T(); };
0081 }
0082 
0083 template <G4bool>
0084 G4MergeFunction<G4bool> GetMergeFunction(G4MergeMode mergeMode)
0085 {
0086   switch ( mergeMode ) {
0087     case G4MergeMode::kAddition:
0088     case G4MergeMode::kMaximum:
0089       // return std::bind([](const T& x, const T& y) { return x + y; });
0090       return [](const G4bool& x, const G4bool& y) { return x || y; };
0091 
0092     case G4MergeMode::kMultiplication:
0093     case G4MergeMode::kMinimum:
0094       return [](const G4bool& x, const G4bool& y) { return x && y; };
0095   }
0096 }
0097 
0098 }
0099 
0100 #endif