Project
Loading...
Searching...
No Matches
testMagneticField.cxx
Go to the documentation of this file.
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#define BOOST_TEST_MODULE Test MagneticField
13#define BOOST_TEST_MAIN
14#define BOOST_TEST_DYN_LINK
15#include <boost/test/unit_test.hpp>
16#include <iostream>
17#include "Field/MagneticField.h"
18#include "Field/MagFieldFast.h"
19#include <memory>
20#include <fairlogger/Logger.h> // for FairLogger
21#include <TStopwatch.h>
22#include <TRandom.h>
23
24using namespace o2::field;
25
26BOOST_AUTO_TEST_CASE(MagneticField_test)
27{
28 // create magnetic field
29 std::unique_ptr<MagneticField> fld = std::make_unique<MagneticField>("Maps", "Maps", 1., 1., o2::field::MagFieldParam::k5kG);
30 double bz0 = fld->solenoidField();
31 LOG(info) << "Created default magnetic field for " << bz0 << "kG";
32 const double nomBz = 5.00685;
33 BOOST_CHECK_CLOSE(bz0, nomBz, 0.1);
34
35 const int ntst = 10000;
36 float rnd[3];
37 double xyz[ntst][3] = {}, bxyz[ntst][3] = {}, dummyAcc = 0.;
38 // fill input
39 for (int it = ntst; it--;) {
40 gRandom->RndmArray(3, rnd);
41 xyz[it][0] = rnd[0] * 400. * TMath::Cos(rnd[1] * TMath::Pi() * 2);
42 xyz[it][1] = rnd[1] * 400. * TMath::Sin(rnd[1] * TMath::Pi() * 2);
43 xyz[it][2] = (rnd[0] - 0.5) * 250;
44 }
45
46 const int repFactor = 50;
47 // timing: slow field
48 TStopwatch swSlow;
49 swSlow.Start();
50 for (int ii = repFactor; ii--;) {
51 for (int it = ntst; it--;) {
52 fld->Field(xyz[it], bxyz[it]);
53 }
54 }
55 swSlow.Stop();
56
57 // init fast field
58 fld->AllowFastField(true);
59
60 // timing: fast field
61 TStopwatch swFast;
62 swFast.Start();
63 double bfast[3];
64 for (int ii = repFactor; ii--;) {
65 for (int it = ntst; it--;) {
66 fld->Field(xyz[it], bfast);
67 }
68 }
69 swFast.Stop();
70 //
71 double sS = swSlow.CpuTime() / (ntst * repFactor);
72 double sF = swFast.CpuTime() / (ntst * repFactor);
73 double rat = sF > 0. ? sS / sF : -1;
74 LOG(info) << "Timing: Exact param: " << sS << " Fast param: " << sF
75 << "s/call -> factor " << rat;
76
77 // compare slow/fast param precision
78 double mean[3] = {0.}, rms[3] = {0.};
79 const char comp[] = "XYZ";
80 LOG(info) << "Relative precision of fast field wrt exact field";
81 for (int it = ntst; it--;) {
82 fld->Field(xyz[it], bfast);
83 for (int i = 0; i < 3; i++) {
84 double df = bxyz[it][i] - bfast[i];
85 mean[i] += df;
86 rms[i] += df * df;
87 }
88 }
89 for (int i = 0; i < 3; i++) {
90 mean[i] /= ntst;
91 rms[i] /= ntst;
92 rms[i] -= mean[i] * mean[i];
93 rms[i] = TMath::Sqrt(rms[i]);
94 LOG(info) << "deltaB" << comp[i] << ": "
95 << " mean=" << mean[i] << "(" << mean[i] / nomBz * 100. << "%)"
96 << " RMS =" << rms[i] << "(" << rms[i] / nomBz * 100. << "%)";
97 BOOST_CHECK(TMath::Abs(mean[i] / nomBz) < 1.e-3);
98 BOOST_CHECK(TMath::Abs(rms[i] / nomBz) < 1.e-3);
99 }
100}
int32_t i
Definition of the fast magnetic field parametrization MagFieldFast.
Definition of the MagF class.
BOOST_AUTO_TEST_CASE(MagneticField_test)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
BOOST_CHECK(tree)