File indexing completed on 2024-06-26 07:05:37
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "eicsmear/smear/Device.h"
0011
0012 #include <algorithm>
0013 #include <functional>
0014 #include <iterator>
0015 #include <stdexcept>
0016 #include <string>
0017 #include <vector>
0018
0019 #include <TUUID.h>
0020 #include <TDatabasePDG.h>
0021
0022 #include "eicsmear/smear/FormulaString.h"
0023 #include "eicsmear/smear/ParticleMCS.h"
0024
0025 using std::cout;
0026 using std::cerr;
0027 using std::endl;
0028
0029 namespace Smear {
0030
0031
0032
0033
0034
0035
0036
0037
0038 bool Device::Init(const TString& kinematicFunction,
0039 const TString& resolutionFunction, int genre) {
0040 Accept.SetGenre(genre);
0041
0042
0043
0044 FormulaString f(kinematicFunction.Data());
0045
0046 mSmeared = f.Variables().front();
0047
0048 mKinematicFunction = new TF1(TUUID().AsString(),
0049 f.GetString().c_str(), -1e15, 1.e16);
0050
0051 mFormula = new FormulaString(resolutionFunction.Data());
0052
0053 return mFormula && mKinematicFunction;
0054 }
0055
0056 Device::Device(KinType type, const TString& formula, EGenre genre)
0057 : mSmeared(type)
0058 , mKinematicFunction(NULL)
0059 , mFormula(NULL) {
0060 Accept.SetGenre(genre);
0061 Init(FormulaString::GetKinName(type), formula, genre);
0062 }
0063
0064 Device::Device(const TString& variable, const TString& resolution,
0065 EGenre genre)
0066 : mSmeared(kInvalidKinType)
0067 , mKinematicFunction(NULL)
0068 , mFormula(NULL) {
0069 Init(variable, resolution, genre);
0070 }
0071
0072 Device::Device(const Device& that)
0073 : Smearer(that)
0074 , mSmeared(that.mSmeared)
0075 , mKinematicFunction(NULL)
0076 , mFormula(NULL)
0077 , mDimensions(that.mDimensions) {
0078 if (that.mKinematicFunction) {
0079 mKinematicFunction = static_cast<TF1*>(
0080 that.mKinematicFunction->Clone(TUUID().AsString()));
0081 }
0082 if (that.mFormula) {
0083 mFormula = static_cast<FormulaString*>(that.mFormula->Clone());
0084 }
0085 }
0086
0087 Device::~Device() {
0088 if (mFormula) {
0089 delete mFormula;
0090 mFormula = NULL;
0091 }
0092 if (mKinematicFunction) {
0093 delete mKinematicFunction;
0094 mKinematicFunction = NULL;
0095 }
0096 }
0097
0098 void Device::Smear(const erhic::VirtualParticle &prt, ParticleMCS &out) {
0099
0100 if (!Accept.Is(prt)) {
0101 return;
0102 }
0103
0104 const std::vector<KinType> vars = mFormula->Variables();
0105 std::vector<double> args;
0106 for (unsigned i(0); i < vars.size(); ++i) {
0107 args.push_back(GetVariable(prt, vars.at(i)));
0108 }
0109
0110
0111 double unsmeared = mKinematicFunction->Eval(GetVariable(prt, mSmeared));
0112 double resolution = mFormula->Eval(args);
0113 double smeared = mDistribution.Generate(unsmeared, resolution);
0114
0115 if ( false && abs(prt.Id())==11){
0116
0117 std::cout << " ====================== mSmeared = " << mSmeared << std::endl;
0118
0119 std::cout << " orig eta = " << prt.GetEta()<< std::endl;
0120 std::cout << "unsmeared " << unsmeared << std::endl;
0121 std::cout << "resolution " << resolution << std::endl;
0122 std::cout << "smeared " << smeared << std::endl;
0123 std::cout << "mSmeared " << mSmeared << std::endl;
0124 }
0125
0126 if ( mKinematicFunction->GetFormula()->GetExpFormula() != "x" ){
0127 std::cerr << "Formula = " << mKinematicFunction->GetFormula()->GetExpFormula() << std::endl;
0128 throw -1;
0129 }
0130
0131 out.SetVariable(mKinematicFunction->GetX(smeared), mSmeared);
0132
0133 if (kTheta == mSmeared) {
0134 out.SetTheta ( FixTheta(out.GetTheta() ), false);
0135 } else if (kPhi == mSmeared) {
0136 out.SetPhi ( FixPhi(out.GetPhi() ), false);
0137 }
0138
0139 out.HandleBogusValues(mSmeared);
0140 if ( std::isnan( GetVariable (out, mSmeared ) ) ) cout << "Problem. smeared is " << smeared << " but propagated nan in " << mSmeared<< endl;
0141
0142 }
0143
0144 Device* Device::Clone(const char* ) const {
0145 return new Device(*this);
0146 }
0147
0148 void Device::Print(Option_t* ) const {
0149 const std::string name = FormulaString::GetKinName(mSmeared);
0150 std::cout << "Device smearing " << name << " with sigma(" << name <<
0151 ") = " << mFormula->GetInputString() << std::endl;
0152 }
0153
0154 }