forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpaceCharge.h
More file actions
1424 lines (1144 loc) · 93.4 KB
/
SpaceCharge.h
File metadata and controls
1424 lines (1144 loc) · 93.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019-2020 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.
/// \file SpaceCharge.h
/// \brief This class contains the algorithms for calculation the distortions and corrections
///
/// \author Matthias Kleiner <mkleiner@ikf.uni-frankfurt.de>
/// Rifki Sadikin <rifki.sadikin@cern.ch> (original code in AliRoot in AliTPCSpaceCharge3DCalc.h)
/// \date Aug 21, 2020
#ifndef ALICEO2_TPC_SPACECHARGE_H_
#define ALICEO2_TPC_SPACECHARGE_H_
#include "TPCSpaceCharge/TriCubic.h"
#include "TPCSpaceCharge/SpaceChargeHelpers.h"
#include "TPCSpaceCharge/PoissonSolverHelpers.h"
#include "TPCSpaceCharge/RegularGrid3D.h"
#include "TPCSpaceCharge/DataContainer3D.h"
#include "TPCSpaceCharge/SpaceChargeParameter.h"
#include "DataFormatsTPC/Defs.h"
class TH3;
class TH3D;
class TH3F;
class TH2F;
namespace o2
{
namespace parameters
{
class GRPMagField;
}
namespace utils
{
class TreeStreamRedirector;
}
namespace tpc
{
class Sector;
template <class T>
class CalDet;
/// Enumerator for setting the space-charge distortion mode
enum class SCDistortionType : int {
SCDistortionsConstant = 0, // space-charge distortions constant over time
SCDistortionsRealistic = 1 // realistic evolution of space-charge distortions over time
};
/// \class SpaceCharge
/// this class provides the algorithms for calculating the global distortions and corrections from the space charge density.
/// The calculations can be done by a realistic space charge histogram as an input or by an analytical formula.
/// An example of of the usage can be found in 'macro/calculateDistortionsCorrections.C'
/// \tparam DataT the data type which is used during the calculations
/// \tparam Nz number of vertices in z direction
/// \tparam Nr number of vertices in r direction
/// \tparam Nphi number of vertices in phi direction
template <typename DataT = double>
class SpaceCharge
{
using RegularGrid = RegularGrid3D<DataT>;
using DataContainer = DataContainer3D<DataT>;
using GridProp = GridProperties<DataT>;
using TriCubic = TriCubicInterpolator<DataT>;
using TH3DataT = std::conditional_t<std::is_same<DataT, double>::value, TH3D, TH3F>; // datatype for TH3 (TH3F for DataT==float and TH3D for DataT==double)
public:
/// constructor
/// grid granularity has to set before constructing an object using the static function setGrid(nZVertices, nRVertices, nPhiVertices)!
/// \param bfield magnetic field (-5, -2, 0, 2, 5)
/// \param nZVertices grid vertices in z direction
/// \param nRVertices grid vertices in r direction
/// \param nPhiVertices grid vertices in phi direction
/// \param initBuffers initialize all buffers
SpaceCharge(const int bfield, const unsigned short nZVertices = 129, const unsigned short nRVertices = 129, const unsigned short nPhiVertices = 360, const bool initBuffers = false);
/// constructor for empty object. Can be used when buffers are loaded from file
SpaceCharge();
/// default move constructor
SpaceCharge(SpaceCharge&&) = default;
/// move assignment
SpaceCharge& operator=(SpaceCharge&&) = default;
/// numerical integration strategys
enum class IntegrationStrategy { Trapezoidal = 0, ///< trapezoidal integration (https://en.wikipedia.org/wiki/Trapezoidal_rule). straight electron drift line assumed: z0->z1, r0->r0, phi0->phi0
Simpson = 1, ///< simpon integration. see: https://en.wikipedia.org/wiki/Simpson%27s_rule. straight electron drift line assumed: z0->z1, r0->r0, phi0->phi0
Root = 2, ///< Root integration. straight electron drift line assumed: z0->z1, r0->r0, phi0->phi0
SimpsonIterative = 3 ///< simpon integration, but using an iterative method to approximate the drift path. No straight electron drift line assumed: z0->z1, r0->r1, phi0->phi1
};
enum class Type {
Distortions = 0, ///< distortions
Corrections = 1 ///< corrections
};
enum class GlobalDistType {
Standard = 0, ///< classical method (start calculation of global distortion at each voxel in the tpc and follow electron drift to readout -slow-)
Fast = 1, ///< interpolation of global corrections (use the global corrections to apply an iterative approach to obtain the global distortions -fast-)
None = 2 ///< dont calculate global distortions
};
enum class GlobalDistCorrMethod {
LocalDistCorr, ///< using local dis/corr interpolator for calculation of global distortions/corrections
ElectricalField ///< using electric field for calculation of global distortions/corrections
};
enum class MisalignmentType {
ShiftedClip, ///< shifted copper rod clip
RotatedClip, ///< rotated mylar strips from FC
RodShift ///< shifted copper rod
};
enum class FCType {
IFC, ///< inner field cage
OFC ///< outer field cage
};
/// step 0: set the charge density from TH3 histogram containing the space charge density
/// \param hisSCDensity3D histogram for the space charge density
void fillChargeDensityFromHisto(const TH3& hisSCDensity3D);
/// step 0: set the space-charge density from two TH3 histograms containing the space-charge density for A and C side seperately
/// \param hisSCDensity3D_A histogram for the space charge density for A-side
/// \param hisSCDensity3D_C histogram for the space charge density for C-side
void fillChargeDensityFromHisto(const TH3& hisSCDensity3D_A, const TH3& hisSCDensity3D_C);
/// step 0: set the space-charge density from two TH3 histograms containing the space charge density for A and C side separately which are stored in a ROOT file
/// \param file path to root file containing the space-charge density
/// \param nameA name of the space-charge density histogram for the A-side
/// \param nameC name of the space-charge density histogram for the C-side
void fillChargeDensityFromHisto(const char* file, const char* nameA, const char* nameC);
/// step 0: set the space charge density from std::vector<CalDet> containing the space charge density. Each entry in the object corresponds to one z slice
/// \param calSCDensity3D pad-by-pad CalDet for the space charge density
void fillChargeDensityFromCalDet(const std::vector<CalDet<float>>& calSCDensity3D);
/// Convert the IDCs to the number of ions for the ion backflow (primary ionization is not considered)
/// \param idcZero map containing the IDCs values which will be converted to the space-charge density
/// \param mapIBF map contains the pad-by-pad IBF in %
/// \param ionDriftTimeMS ion drift time in ms
/// \param normToPadArea normalize IDCs to pad area (should always be true as the normalization is performed in IDCFactorization::calcIDCZero
static void convertIDCsToCharge(std::vector<CalDet<float>>& idcZero, const CalDet<float>& mapIBF, const float ionDriftTimeMS = 200, const bool normToPadArea = true);
/// Convert the IDCs to the number of ions for the ion backflow (primary ionization is not considered)
/// \param idcZero map containing the IDCs which will be converted to the space-charge density
/// \param mapIBF map contains the pad-by-pad IBF in %
/// \param ionDriftTimeMS ion drift time in ms
/// \param normToPadArea normalize IDCs to pad area (should always be true as the normalization is performed in IDCFactorization::calcIDCZero
void fillChargeFromIDCs(std::vector<CalDet<float>>& idcZero, const CalDet<float>& mapIBF, const float ionDriftTimeMS = 200, const bool normToPadArea = true);
/// step 0: set the charge (number of ions) from std::vector<CalDet> containing the charge. Each entry in the object corresponds to one z slice.
/// Normalization to the space charge is also done automatically
/// \param calCharge3D histogram for the charge
void fillChargeFromCalDet(const std::vector<CalDet<float>>& calCharge3D);
/// step 0: set the charge density from TH3 histogram containing the space charge density
/// \param fInp input file containing a histogram for the space charge density
/// \param name the name of the space charge density histogram in the file
void fillChargeDensityFromFile(TFile& fInp, const char* name);
/// \param side side of the TPC
/// \param calcVectors set to calculate also the local distortion and local correction vectors
void calculateDistortionsCorrections(const o2::tpc::Side side, const bool calcVectors = false);
/// step 0: this function fills the internal storage for the charge density using an analytical formula
/// \param formulaStruct struct containing a method to evaluate the density
void setChargeDensityFromFormula(const AnalyticalFields<DataT>& formulaStruct);
/// step 0: this function fills the boundary of the potential using an analytical formula. The boundary is used in the PoissonSolver.
/// \param formulaStruct struct containing a method to evaluate the potential
void setPotentialBoundaryFromFormula(const AnalyticalFields<DataT>& formulaStruct);
/// adding the boundary potential from other sc object which same number of vertices!
/// \param other other SC object which boundary potential witll be added
/// \param scaling sclaing factor which used to scale the others boundary potential
void addBoundaryPotential(const SpaceCharge<DataT>& other, const Side side, const float scaling = 1);
/// setting the boundary potential to 0 for z<zMin and z>zMax
void resetBoundaryPotentialToZeroInRangeZ(float zMin, float zMax, const Side side);
/// step 0: this function fills the potential using an analytical formula
/// \param formulaStruct struct containing a method to evaluate the potential
void setPotentialFromFormula(const AnalyticalFields<DataT>& formulaStruct);
/// mirror potential from one side to the other side
/// \param sideRef side which contains the reference potential
/// \param sideMirrored side where the potential will be set from sideRef
void mirrorPotential(const Side sideRef, const Side sideMirrored);
/// simulate only one sector instead of 18 per side. This makes currently only sense for the static distortions (ToDo: simplify usage)
/// phi max will be restricted to 2Pi/18 for this instance and for global instance of poisson solver
void setSimOneSector() { setSimNSector(1); }
/// simulate N sectors
void setSimNSector(const int nSectors);
/// unsetting simulation of one sector
static void unsetSimNSector();
/// setting default potential (same potential for all GEM frames. The default value of 1000V are matched to distortions observed in laser data without X-Ray etc.
/// \param side side of the TPC where the potential will be set
/// \param deltaPotential delta potential which will be set at the GEM frames
void setDefaultStaticDistortionsGEMFrameChargeUp(const Side side, const DataT deltaPotential = 1000);
/// setting the boundary potential of the GEM stack along the radius
/// \param potentialFunc potential funtion as a function of the radius
/// \param Side of the TPC
void setPotentialBoundaryGEMFrameAlongR(const std::function<DataT(DataT)>& potentialFunc, const Side side);
/// setting the boundary potential of the IROC on the bottom along phi
/// \param potentialFunc potential funtion as a function of global phi
/// \param Side of the TPC
void setPotentialBoundaryGEMFrameIROCBottomAlongPhi(const std::function<DataT(DataT)>& potentialFunc, const Side side) { setPotentialBoundaryGEMFrameAlongPhi(potentialFunc, GEMstack::IROCgem, true, side); }
/// setting the boundary potential of the IROC on the top along phi
/// \param potentialFunc potential funtion as a function of global phi
/// \param Side of the TPC
void setPotentialBoundaryGEMFrameIROCTopAlongPhi(const std::function<DataT(DataT)>& potentialFunc, const Side side) { setPotentialBoundaryGEMFrameAlongPhi(potentialFunc, GEMstack::IROCgem, false, side); }
/// setting the boundary potential of the OROC1 on the bottom along phi
/// \param potentialFunc potential funtion as a function of global phi
/// \param Side of the TPC
void setPotentialBoundaryGEMFrameOROC1BottomAlongPhi(const std::function<DataT(DataT)>& potentialFunc, const Side side) { setPotentialBoundaryGEMFrameAlongPhi(potentialFunc, GEMstack::OROC1gem, true, side); }
/// setting the boundary potential of the OROC1 on the top along phi
/// \param potentialFunc potential funtion as a function of global phi
/// \param Side of the TPC
void setPotentialBoundaryGEMFrameOROC1TopAlongPhi(const std::function<DataT(DataT)>& potentialFunc, const Side side) { setPotentialBoundaryGEMFrameAlongPhi(potentialFunc, GEMstack::OROC1gem, false, side); }
/// setting the boundary potential of the OROC2 on the bottom along phi
/// \param potentialFunc potential funtion as a function of global phi
/// \param Side of the TPC
void setPotentialBoundaryGEMFrameOROC2BottomAlongPhi(const std::function<DataT(DataT)>& potentialFunc, const Side side) { setPotentialBoundaryGEMFrameAlongPhi(potentialFunc, GEMstack::OROC2gem, true, side); }
/// setting the boundary potential of the OROC2 on the top along phi
/// \param potentialFunc potential funtion as a function of global phi
/// \param Side of the TPC
void setPotentialBoundaryGEMFrameOROC2TopAlongPhi(const std::function<DataT(DataT)>& potentialFunc, const Side side) { setPotentialBoundaryGEMFrameAlongPhi(potentialFunc, GEMstack::OROC2gem, false, side); }
/// setting the boundary potential of the OROC3 on the bottom along phi
/// \param potentialFunc potential funtion as a function of global phi
/// \param Side of the TPC
void setPotentialBoundaryGEMFrameOROC3BottomAlongPhi(const std::function<DataT(DataT)>& potentialFunc, const Side side) { setPotentialBoundaryGEMFrameAlongPhi(potentialFunc, GEMstack::OROC3gem, true, side); }
/// setting the boundary potential of the OROC3 on the top along phi
/// \param potentialFunc potential funtion as a function of global phi
/// \param Side of the TPC
void setPotentialBoundaryGEMFrameOROC3TopAlongPhi(const std::function<DataT(DataT)>& potentialFunc, const Side side) { setPotentialBoundaryGEMFrameAlongPhi(potentialFunc, GEMstack::OROC3gem, false, side); }
/// setting the boundary potential of the OROC3 on the top along phi
/// \param potentialFunc potential funtion as a function of global phi
/// \param Side of the TPC
void setPotentialBoundaryGEMFrameOROC3ToOFCPhi(const std::function<DataT(DataT)>& potentialFunc, const Side side) { setPotentialBoundaryGEMFrameAlongPhi(potentialFunc, GEMstack::OROC3gem, false, side, true); }
/// setting the potential from the IROC to the inner field cage
void setPotentialBoundaryGEMFrameIROCToIFCPhi(const std::function<DataT(DataT)>& potentialFunc, const Side side) { setPotentialBoundaryGEMFrameAlongPhi(potentialFunc, GEMstack::IROCgem, true, side, true); }
/// setting the boundary potential for the inner TPC radius along r
/// \param potentialFunc potential funtion as a function of z
/// \param Side of the TPC
void setPotentialBoundaryInnerRadius(const std::function<DataT(DataT)>& potentialFunc, const Side side);
/// setting the boundary potential for the outer TPC radius along r
/// \param potentialFunc potential funtion as a function of z
/// \param Side of the TPC
void setPotentialBoundaryOuterRadius(const std::function<DataT(DataT)>& potentialFunc, const Side side);
/// step 1: use the O2TPCPoissonSolver class to numerically calculate the potential with set space charge density and boundary conditions from potential
/// \param side side of the TPC
/// \param stoppingConvergence stopping criterion used in the poisson solver
/// \param symmetry use symmetry or not in the poisson solver
void poissonSolver(const Side side, const DataT stoppingConvergence = 1e-6, const int symmetry = 0);
/// step 1: use the O2TPCPoissonSolver class to numerically calculate the potential with set space charge density and boundary conditions from potential for A and C side in parallel
/// \param stoppingConvergence stopping criterion used in the poisson solver
/// \param symmetry use symmetry or not in the poisson solver
void poissonSolver(const DataT stoppingConvergence = 1e-6, const int symmetry = 0);
/// step 2: calculate numerically the electric field from the potential
/// \param side side of the TPC
void calcEField(const Side side);
/// step 2a: set the electric field from an analytical formula
/// \param formulaStruct struct containing a method to evaluate the electric fields
void setEFieldFromFormula(const AnalyticalFields<DataT>& formulaStruct);
/// scale the space charge density by a scaling factor: sc_new = sc * scalingFactor
/// \param scalingFactor factor to scale the space-charge density
/// \param side side for which the space-charge density will be scaled
void scaleChargeDensity(const DataT scalingFactor, const Side side) { mDensity[side] *= scalingFactor; }
/// scale the space-charge for one sector: space-charge density *= scalingFactor;
/// \param scalingFactor scaling factor for the space-charge density
void scaleChargeDensitySector(const float scalingFactor, const Sector sector);
/// scaling the space-charge density for given stack
void scaleChargeDensityStack(const float scalingFactor, const Sector sector, const GEMstack stack);
/// scale the potential by a scaling factor
/// \param scalingFactor factor to scale the potential
/// \param side side for which the potential will be scaled
void scalePotential(const DataT scalingFactor, const Side side) { mPotential[side] *= scalingFactor; }
/// add space charge density from other object (this.mDensity = this.mDensity + other.mDensity)
/// \param otherSC other space-charge object, which charge will be added to current object
void addChargeDensity(const SpaceCharge<DataT>& otherSC);
/// add global corrections from other space charge object
void addGlobalCorrections(const SpaceCharge<DataT>& otherSC, const Side side);
/// convert space-charge object to new definition of number of vertices
/// \param nZNew new number of vertices in z direction
/// \param nRNew new number of vertices in r direction
/// \param nPhiNew new number of vertices in phi direction
void downSampleObject(const int nZNew, const int nRNew, const int nPhiNew);
/// step 3: calculate the local distortions and corrections with an electric field
/// \param type calculate local corrections or local distortions: type = o2::tpc::SpaceCharge<>::Type::Distortions or o2::tpc::SpaceCharge<>::Type::Corrections
/// \param formulaStruct struct containing a method to evaluate the electric field Er, Ez, Ephi (analytical formula or by TriCubic interpolator)
template <typename ElectricFields = AnalyticalFields<DataT>>
void calcLocalDistortionsCorrections(const Type type, const ElectricFields& formulaStruct);
/// step 3b: calculate the local distortion and correction vectors with an electric field
/// \param formulaStruct struct containing a method to evaluate the electric field Er, Ez, Ephi (analytical formula or by TriCubic interpolator)
template <typename ElectricFields = AnalyticalFields<DataT>>
void calcLocalDistortionCorrectionVector(const ElectricFields& formulaStruct);
/// step 3b: calculate the local distortions and corrections with the local distortion/correction vectors using Runge Kutta 4.
/// calcLocalDistortionCorrectionVector() has to be called before this function
/// \param type calculate local corrections or local distortions: type = o2::tpc::SpaceCharge<>::Type::Distortions or o2::tpc::SpaceCharge<>::Type::Corrections
/// \param side side of the TPC
template <typename ElectricFields = AnalyticalFields<DataT>>
void calcLocalDistortionsCorrectionsRK4(const Type type, const Side side);
/// step 4: calculate global corrections by using the electric field or the local corrections
/// \param formulaStruct struct containing a method to evaluate the electric field Er, Ez, Ephi or the local corrections
/// \param type how to treat the corrections at regions where the corrected value is out of the TPC volume: type=0: use last valid correction value, type=1 do linear extrapolation, type=2 do parabolic extrapolation, type=3 do NOT abort when reaching the CE or the IFC to get a smooth estimate of the corrections
template <typename Fields = AnalyticalFields<DataT>>
void calcGlobalCorrections(const Fields& formulaStruct, const int type = 3);
/// calculate the global corrections using the electric fields (interface for python)
void calcGlobalCorrectionsEField(const Side side, const int type = 3) { calcGlobalCorrections(getElectricFieldsInterpolator(side), type); }
/// step 5: calculate global distortions by using the electric field or the local distortions (SLOW)
/// \param formulaStruct struct containing a method to evaluate the electric field Er, Ez, Ephi or the local distortions
/// \param maxIterations maximum steps which are are performed to reach the central electrode (in general this is not necessary, but in case of problems this value aborts the calculation)
template <typename Fields = AnalyticalFields<DataT>>
void calcGlobalDistortions(const Fields& formulaStruct, const int maxIterations = 3 * sSteps * 129);
void init();
/// step 5: calculate global distortions using the global corrections (FAST)
/// \param globCorr interpolator for global corrections
/// \param maxIter maximum iterations per global distortion
/// \param approachZ when the difference between the desired z coordinate and the position of the global correction is deltaZ, approach the desired z coordinate by deltaZ * \p approachZ.
/// \param approachR when the difference between the desired r coordinate and the position of the global correction is deltaR, approach the desired r coordinate by deltaR * \p approachR.
/// \param approachPhi when the difference between the desired phi coordinate and the position of the global correction is deltaPhi, approach the desired phi coordinate by deltaPhi * \p approachPhi.
/// \param diffCorr if the absolute differences from the interpolated values for the global corrections from the last iteration compared to the current iteration is smaller than this value, set converged to true for current global distortion
/// \param type whether to calculate distortions or corrections
void calcGlobalDistWithGlobalCorrIterative(const DistCorrInterpolator<DataT>& globCorr, const int maxIter = 100, const DataT approachZ = 1, const DataT approachR = 1, const DataT approachPhi = 1, const DataT diffCorr = 50e-6, const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0);
/// step 5: calculate global distortions using the global corrections (FAST)
/// \param scSCale possible second sc object
/// \param scale scaling for second sc object
void calcGlobalDistWithGlobalCorrIterative(const Side side, const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachZ = 1, const DataT approachR = 1, const DataT approachPhi = 1, const DataT diffCorr = 50e-6);
void calcGlobalDistWithGlobalCorrIterative(const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachZ = 1, const DataT approachR = 1, const DataT approachPhi = 1, const DataT diffCorr = 50e-6);
/// calculate global corrections from global distortions
/// \param scSCale possible second sc object
/// \param scale scaling for second sc object
void calcGlobalCorrWithGlobalDistIterative(const Side side, const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachZ = 1, const DataT approachR = 1, const DataT approachPhi = 1, const DataT diffCorr = 50e-6);
void calcGlobalCorrWithGlobalDistIterative(const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachZ = 1, const DataT approachR = 1, const DataT approachPhi = 1, const DataT diffCorr = 50e-6);
/// Calculate global distortions using the global corrections by scaling with second distortion object, which will be consistent with scaled corrections in cartesian coordinates (as done in the tracking)
/// \param side Side of the TPC
/// \param scSCale possible second sc object
/// \param scale scaling for second sc object
void calcGlobalDistWithGlobalCorrIterativeLinearCartesian(const Side side, const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachX = 1, const DataT approachY = 1, const DataT approachZ = 1, const DataT diffCorr = 50e-6);
void calcGlobalDistWithGlobalCorrIterativeLinearCartesian(const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachX = 1, const DataT approachY = 1, const DataT approachZ = 1, const DataT diffCorr = 50e-6);
/// Calculate global corrections using the global distortions by scaling with second distortion object, which will be consistent with scaled corrections in cartesian coordinates
/// \param side Side of the TPC
/// \param scSCale possible second sc object
/// \param scale scaling for second sc object
void calcGlobalCorrWithGlobalDistIterativeLinearCartesian(const Side side, const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachX = 1, const DataT approachY = 1, const DataT approachZ = 1, const DataT diffCorr = 50e-6);
void calcGlobalCorrWithGlobalDistIterativeLinearCartesian(const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachX = 1, const DataT approachY = 1, const DataT approachZ = 1, const DataT diffCorr = 50e-6);
/// \return returns number of vertices in z direction
unsigned short getNZVertices() const { return mParamGrid.NZVertices; }
/// \return returns number of vertices in r direction
unsigned short getNRVertices() const { return mParamGrid.NRVertices; }
/// \return returns number of vertices in phi direction
unsigned short getNPhiVertices() const { return mParamGrid.NPhiVertices; }
/// \return returns parameter C0
auto getC0() const { return mC0; }
/// \return returns parameter C1
auto getC1() const { return mC1; }
/// \return returns parameter C2
auto getC2() const { return mC2; }
/// \return returns BField in kG
int getBField() const { return mBField.getBField(); }
const auto& getPotential(const Side side) const& { return mPotential[side]; }
/// setting the potential directly for given vertex
void setPotential(int iz, int ir, int iphi, Side side, float val);
/// get the space charge density for given coordinate
/// \param z global z coordinate
/// \param r global r coordinate
/// \param phi global phi coordinate
DataT getDensityCyl(const DataT z, const DataT r, const DataT phi, const Side side) const;
/// get the potential for list of given coordinate
std::vector<float> getDensityCyl(const std::vector<DataT>& z, const std::vector<DataT>& r, const std::vector<DataT>& phi, const Side side) const;
/// get the potential for given coordinate
/// \param z global z coordinate
/// \param r global r coordinate
/// \param phi global phi coordinate
DataT getPotentialCyl(const DataT z, const DataT r, const DataT phi, const Side side) const;
/// get the potential for list of given coordinate
std::vector<float> getPotentialCyl(const std::vector<DataT>& z, const std::vector<DataT>& r, const std::vector<DataT>& phi, const Side side) const;
/// get the electric field for given coordinate
/// \param z global z coordinate
/// \param r global r coordinate
/// \param phi global phi coordinate
/// \param eZ returns correction in z direction
/// \param eR returns correction in r direction
/// \param ePhi returns correction in phi direction
void getElectricFieldsCyl(const DataT z, const DataT r, const DataT phi, const Side side, DataT& eZ, DataT& eR, DataT& ePhi) const;
/// get the local correction for given coordinate
/// \param z global z coordinate
/// \param r global r coordinate
/// \param phi global phi coordinate
/// \param lcorrZ returns local correction in z direction
/// \param lcorrR returns local correction in r direction
/// \param lcorrRPhi returns local correction in rphi direction
void getLocalCorrectionsCyl(const DataT z, const DataT r, const DataT phi, const Side side, DataT& lcorrZ, DataT& lcorrR, DataT& lcorrRPhi) const;
/// get the local correction for given coordinates
/// \param z global z coordinates
/// \param r global r coordinates
/// \param phi global phi coordinates
/// \param lcorrZ returns local corrections in z direction
/// \param lcorrR returns local corrections in r direction
/// \param lcorrRPhi returns local corrections in rphi direction
void getLocalCorrectionsCyl(const std::vector<DataT>& z, const std::vector<DataT>& r, const std::vector<DataT>& phi, const Side side, std::vector<DataT>& lcorrZ, std::vector<DataT>& lcorrR, std::vector<DataT>& lcorrRPhi) const;
/// get the global correction for given coordinate
/// \param z global z coordinate
/// \param r global r coordinate
/// \param phi global phi coordinate
/// \param corrZ returns correction in z direction
/// \param corrR returns correction in r direction
/// \param corrRPhi returns correction in rphi direction
void getCorrectionsCyl(const DataT z, const DataT r, const DataT phi, const Side side, DataT& corrZ, DataT& corrR, DataT& corrRPhi) const;
/// get the global correction for given coordinates
/// \param z global z coordinates
/// \param r global r coordinates
/// \param phi global phi coordinates
/// \param corrZ returns corrections in z direction
/// \param corrR returns corrections in r direction
/// \param corrRPhi returns corrections in rphi direction
void getCorrectionsCyl(const std::vector<DataT>& z, const std::vector<DataT>& r, const std::vector<DataT>& phi, const Side side, std::vector<DataT>& corrZ, std::vector<DataT>& corrR, std::vector<DataT>& corrRPhi) const;
/// get the global corrections for given coordinate
/// \param x global x coordinate
/// \param y global y coordinate
/// \param z global z coordinate
/// \param corrX returns corrections in x direction
/// \param corrY returns corrections in y direction
/// \param corrZ returns corrections in z direction
void getCorrections(const DataT x, const DataT y, const DataT z, const Side side, DataT& corrX, DataT& corrY, DataT& corrZ) const;
/// get the analytical global corrections for given coordinate
/// \param x global x coordinate
/// \param y global y coordinate
/// \param z global z coordinate
/// \param corrX returns corrections in x direction
/// \param corrY returns corrections in y direction
/// \param corrZ returns corrections in z direction
void getCorrectionsAnalytical(const DataT x, const DataT y, const DataT z, const Side side, DataT& corrX, DataT& corrY, DataT& corrZ) const { getDistortionsCorrectionsAnalytical(x, y, z, side, corrX, corrY, corrZ, false); }
/// get the local distortions for given coordinate
/// \param z global z coordinate
/// \param r global r coordinate
/// \param phi global phi coordinate
/// \param ldistZ returns local distortion in z direction
/// \param ldistR returns local distortion in r direction
/// \param ldistRPhi returns local distortion in rphi direction
void getLocalDistortionsCyl(const DataT z, const DataT r, const DataT phi, const Side side, DataT& ldistZ, DataT& ldistR, DataT& ldistRPhi) const;
/// get the local distortions for given coordinates
/// \param z global z coordinates
/// \param r global r coordinates
/// \param phi global phi coordinates
/// \param ldistZ returns local distortions in z direction
/// \param ldistR returns local distortions in r direction
/// \param ldistRPhi returns local distortions in rphi direction
void getLocalDistortionsCyl(const std::vector<DataT>& z, const std::vector<DataT>& r, const std::vector<DataT>& phi, const Side side, std::vector<DataT>& ldistZ, std::vector<DataT>& ldistR, std::vector<DataT>& ldistRPhi) const;
/// get the local distortion vector for given coordinates
/// \param z global z coordinate
/// \param r global r coordinate
/// \param phi global phi coordinate
/// \param lvecdistZ returns local distortion vector in z direction
/// \param lvecdistR returns local distortion vector in r direction
/// \param lvecdistRPhi returns local distortion vector in rphi direction
void getLocalDistortionVectorCyl(const DataT z, const DataT r, const DataT phi, const Side side, DataT& lvecdistZ, DataT& lvecdistR, DataT& lvecdistRPhi) const;
/// get the local distortion vector for given coordinate
/// \param z global z coordinates
/// \param r global r coordinates
/// \param phi global phi coordinates
/// \param lvecdistZ returns local distortion vectors in z direction
/// \param lvecdistR returns local distortion vectors in r direction
/// \param lvecdistRPhi returns local distortion vectors in rphi direction
void getLocalDistortionVectorCyl(const std::vector<DataT>& z, const std::vector<DataT>& r, const std::vector<DataT>& phi, const Side side, std::vector<DataT>& lvecdistZ, std::vector<DataT>& lvecdistR, std::vector<DataT>& lvecdistRPhi) const;
/// get the local correction vector for given coordinate
/// \param z global z coordinate
/// \param r global r coordinate
/// \param phi global phi coordinate
/// \param ldistZ returns local correction vector in z direction
/// \param ldistR returns local correction vector in r direction
/// \param ldistRPhi returns local correction vector in rphi direction
void getLocalCorrectionVectorCyl(const DataT z, const DataT r, const DataT phi, const Side side, DataT& lveccorrZ, DataT& lveccorrR, DataT& lveccorrRPhi) const;
/// get the local correction vector for given coordinate
/// \param z global z coordinates
/// \param r global r coordinates
/// \param phi global phi coordinates
/// \param ldistZ returns local correction vectors in z direction
/// \param ldistR returns local correction vectors in r direction
/// \param ldistRPhi returns local correction vectors in rphi direction
void getLocalCorrectionVectorCyl(const std::vector<DataT>& z, const std::vector<DataT>& r, const std::vector<DataT>& phi, const Side side, std::vector<DataT>& lveccorrZ, std::vector<DataT>& lveccorrR, std::vector<DataT>& lveccorrRPhi) const;
/// get the global distortions for given coordinate
/// \param z global z coordinate
/// \param r global r coordinate
/// \param phi global phi coordinate
/// \param distZ returns distortion in z direction
/// \param distR returns distortion in r direction
/// \param distRPhi returns distortion in rphi direction
void getDistortionsCyl(const DataT z, const DataT r, const DataT phi, const Side side, DataT& distZ, DataT& distR, DataT& distRPhi) const;
/// get the global distortions for given coordinate
/// \param z global z coordinates
/// \param r global r coordinates
/// \param phi global phi coordinates
/// \param distZ returns distortions in z direction
/// \param distR returns distortions in r direction
/// \param distRPhi returns distortions in rphi direction
void getDistortionsCyl(const std::vector<DataT>& z, const std::vector<DataT>& r, const std::vector<DataT>& phi, const Side side, std::vector<DataT>& distZ, std::vector<DataT>& distR, std::vector<DataT>& distRPhi) const;
/// get the global distortions for given coordinate
/// \param x global x coordinate
/// \param y global y coordinate
/// \param z global z coordinate
/// \param distX returns distortion in x direction
/// \param distY returns distortion in y direction
/// \param distZ returns distortion in z direction
void getDistortions(const DataT x, const DataT y, const DataT z, const Side side, DataT& distX, DataT& distY, DataT& distZ) const;
/// get the global distortions for given coordinate for a possible analytical formula if it was specified
/// \param x global x coordinate
/// \param y global y coordinate
/// \param z global z coordinate
/// \param distX returns distortion in x direction
/// \param distY returns distortion in y direction
/// \param distZ returns distortion in z direction
void getDistortionsAnalytical(const DataT x, const DataT y, const DataT z, const Side side, DataT& distX, DataT& distY, DataT& distZ) const { getDistortionsCorrectionsAnalytical(x, y, z, side, distX, distY, distZ, true); }
/// set distortions and corrections by an analytical formula
void setDistortionsCorrectionsAnalytical(const AnalyticalDistCorr<DataT>& formula) { mAnaDistCorr = formula; }
/// \return returns analytical distortions/corrections
const auto& getDistortionsCorrectionsAnalytical() const& { return mAnaDistCorr; }
/// setting usage of the analytical formula for the distortions and corrections
void setUseAnalyticalDistCorr(const bool useAna) { mUseAnaDistCorr = useAna; }
/// \return returns if the analytical formula will be used in the distortElectron() and getCorrections() function
bool getUseAnalyticalDistCorr() const { return mUseAnaDistCorr; }
/// convert x and y coordinates from cartesian to the radius in polar coordinates
static DataT getRadiusFromCartesian(const DataT x, const DataT y) { return std::sqrt(x * x + y * y); }
/// convert x and y coordinates from cartesian to phi in polar coordinates
static DataT getPhiFromCartesian(const DataT x, const DataT y) { return std::atan2(y, x); }
/// convert radius and phi coordinates from polar coordinates to x cartesian coordinates
static DataT getXFromPolar(const DataT r, const DataT phi) { return r * std::cos(phi); }
/// convert radius and phi coordinates from polar coordinates to y cartesian coordinate
static DataT getYFromPolar(const DataT r, const DataT phi) { return r * std::sin(phi); }
/// Correct electron position using correction lookup tables
/// \param point 3D coordinates of the electron
void correctElectron(GlobalPosition3D& point);
/// Distort electron position using distortion lookup tables
/// \param point 3D coordinates of the electron
/// \param scSCale other sc object which is used for scaling of the distortions
/// \param scale scaling value
void distortElectron(GlobalPosition3D& point, const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0) const;
/// set the distortions directly from a look up table
/// \param distdZ distortions in z direction
/// \param distdR distortions in r direction
/// \param distdRPhi distortions in rphi direction
/// \param side side of the TPC
void setDistortionLookupTables(const DataContainer& distdZ, const DataContainer& distdR, const DataContainer& distdRPhi, const Side side);
/// set the density, potential, electric fields, local distortions/corrections, global distortions/corrections from a file. Missing objects in the file are ignored.
/// \param file output file where the electrical fields will be written to
/// \param side of the TPC
void setFromFile(std::string_view file, const Side side);
/// set the density, potential, electric fields, local distortions/corrections, global distortions/corrections from a file for both sides. Missing objects in the file are ignored.
/// \param file output file where the electrical fields will be written to
void setFromFile(std::string_view file);
/// Get grid spacing in r direction
DataT getGridSpacingR(const Side side) const { return mGrid3D[side].getSpacingR(); }
/// Get grid spacing in z direction
DataT getGridSpacingZ(const Side side) const { return mGrid3D[side].getSpacingZ(); }
/// Get grid spacing in phi direction
DataT getGridSpacingPhi(const Side side) const { return mGrid3D[side].getSpacingPhi(); }
/// Get constant electric field
static constexpr DataT getEzField(const Side side) { return getSign(side) * (TPCParameters<DataT>::cathodev - TPCParameters<DataT>::vg1t) / TPCParameters<DataT>::TPCZ0; }
/// Get inner radius of tpc
DataT getRMin(const Side side) const { return mGrid3D[side].getGridMinR(); }
/// Get min z position which is used during the calaculations
DataT getZMin(const Side side) const { return mGrid3D[side].getGridMinZ(); }
/// Get min phi
DataT getPhiMin(const Side side) const { return mGrid3D[side].getGridMinPhi(); }
/// Get max r
DataT getRMax(const Side side) const { return mGrid3D[side].getGridMaxR(); };
/// Get max z
DataT getZMax(const Side side) const { return mGrid3D[side].getGridMaxZ(); }
/// Get max phi
DataT getPhiMax(const Side side) const { return mGrid3D[side].getGridMaxPhi(); }
// get side of TPC for z coordinate TODO rewrite this
static Side getSide(const DataT z) { return ((z >= 0) ? Side::A : Side::C); }
/// Get the grid object
const RegularGrid& getGrid3D(const Side side) const { return mGrid3D[side]; }
/// Get struct containing interpolators for the electrical fields
/// \param side side of the TPC
NumericalFields<DataT> getElectricFieldsInterpolator(const Side side) const;
/// Get struct containing interpolators for local distortions dR, dZ, dPhi
/// \param side side of the TPC
DistCorrInterpolator<DataT> getLocalDistInterpolator(const Side side) const;
/// Get struct containing interpolators for local corrections dR, dZ, dPhi
/// \param side side of the TPC
DistCorrInterpolator<DataT> getLocalCorrInterpolator(const Side side) const;
/// Get struct containing interpolators for global distortions dR, dZ, dPhi
/// \param side side of the TPC
DistCorrInterpolator<DataT> getGlobalDistInterpolator(const Side side) const;
/// Get struct containing interpolators for global corrections dR, dZ, dPhi
/// \param side side of the TPC
DistCorrInterpolator<DataT> getGlobalCorrInterpolator(const Side side) const;
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local distortion dR for given vertex
DataT getLocalDistR(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mLocalDistdR[side](iz, ir, iphi); }
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local distortion dZ for given vertex
DataT getLocalDistZ(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mLocalDistdZ[side](iz, ir, iphi); }
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local distortion dRPhi for given vertex
DataT getLocalDistRPhi(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mLocalDistdRPhi[side](iz, ir, iphi); }
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local distortion vector dR for given vertex
DataT getLocalVecDistR(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mLocalVecDistdR[side](iz, ir, iphi); }
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local distortion vector dZ for given vertex
DataT getLocalVecDistZ(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mLocalVecDistdZ[side](iz, ir, iphi); }
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local distortion vector dRPhi for given vertex
DataT getLocalVecDistRPhi(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mLocalVecDistdRPhi[side](iz, ir, iphi); }
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local correction dR for given vertex
DataT getLocalCorrR(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mLocalCorrdR[side](iz, ir, iphi); }
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local correction dZ for given vertex
DataT getLocalCorrZ(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mLocalCorrdZ[side](iz, ir, iphi); }
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local correction dRPhi for given vertex
DataT getLocalCorrRPhi(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mLocalCorrdRPhi[side](iz, ir, iphi); }
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local correction vector dR for given vertex
DataT getLocalVecCorrR(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return -mLocalVecDistdR[side](iz, ir, iphi); }
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local correction vector dZ for given vertex
DataT getLocalVecCorrZ(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return -mLocalVecDistdZ[side](iz, ir, iphi); }
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
/// \return returns local correction vector dRPhi for given vertex
DataT getLocalVecCorrRPhi(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return -mLocalVecDistdRPhi[side](iz, ir, iphi); }
/// Get global distortion dR for vertex
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
DataT getGlobalDistR(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mGlobalDistdR[side](iz, ir, iphi); }
/// Get global distortion dZ for vertex
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
DataT getGlobalDistZ(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mGlobalDistdZ[side](iz, ir, iphi); }
/// Get global distortion dRPhi for vertex
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
DataT getGlobalDistRPhi(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mGlobalDistdRPhi[side](iz, ir, iphi); }
/// Get global correction dR for vertex
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
DataT getGlobalCorrR(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mGlobalCorrdR[side](iz, ir, iphi); }
/// Get global correction dZ for vertex
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
DataT getGlobalCorrZ(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mGlobalCorrdZ[side](iz, ir, iphi); }
/// Get global correction dRPhi for vertex
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
DataT getGlobalCorrRPhi(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mGlobalCorrdRPhi[side](iz, ir, iphi); }
/// Get global electric Field Er for vertex
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
DataT getEr(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mElectricFieldEr[side](iz, ir, iphi); }
/// Get global electric Field Ez for vertex
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
DataT getEz(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mElectricFieldEz[side](iz, ir, iphi); }
/// Get global electric Field Ephi for vertex
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
DataT getEphi(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mElectricFieldEphi[side](iz, ir, iphi); }
/// Get density for vertex
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
DataT getDensity(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mDensity[side](iz, ir, iphi); }
/// Get potential for vertex
/// \param iz vertex in z dimension
/// \param ir vertex in r dimension
/// \param iphi vertex in phi dimension
/// \param side side of the TPC
DataT getPotential(const size_t iz, const size_t ir, const size_t iphi, const Side side) const { return mPotential[side](iz, ir, iphi); }
/// Get the step width which is used for the calculation of the correction/distortions in units of the z-bin
static int getStepWidth() { return 1 / sSteps; }
/// Get phi vertex position for index in phi direction
/// \param indexPhi index in phi direction
DataT getPhiVertex(const size_t indexPhi, const Side side) const { return mGrid3D[side].getPhiVertex(indexPhi); }
/// Get r vertex position for index in r direction
/// \param indexR index in r direction
DataT getRVertex(const size_t indexR, const Side side) const { return mGrid3D[side].getRVertex(indexR); }
/// Get z vertex position for index in z direction
/// \param indexZ index in z direction
DataT getZVertex(const size_t indexZ, const Side side) const { return mGrid3D[side].getZVertex(indexZ); }
/// \param omegaTau \omega \tau value
/// \param t1 value for t1 see: ???
/// \param t2 value for t2 see: ???
void setOmegaTauT1T2(const DataT omegaTau = 0.32f, const DataT t1 = 1, const DataT t2 = 1);
/// \param c0 coefficient C0 (compare Jim Thomas's notes for definitions)
/// \param c1 coefficient C1 (compare Jim Thomas's notes for definitions)
void setC0C1(const DataT c0, const DataT c1)
{
mC0 = c0;
mC1 = c1;
}
/// \param c2 coefficient C2
void setC2(const DataT c2) { mC0 = c2; }
/// set magnetic field which can be used for ExB misalignment distortions
/// \param field magnetic field (-5, -2, 0, 2, 5)
void initBField(const int field);
/// enable/disable calculation of distortions due to ExB misalignment
void setSimExBMisalignment(const bool simExBMisalignment) { mSimExBMisalignment = simExBMisalignment; }
/// \return returns if ExB misalignment will be simulated during distortion calculations
bool getSimExBMisalignment() const { return mSimExBMisalignment; }
/// calculate distortions due to electric fields (space charge, boundary potential...)
void setSimEDistortions(const bool simEDistortions) { mSimEDistortions = simEDistortions; }
/// \return returns if distortions due to electric fields will be simulated during distortion calculations
bool getSimEDistortions() const { return mSimEDistortions; }
/// set number of steps used for calculation of distortions/corrections per z bin
/// \param nSteps number of steps per z bin
static void setNStep(const int nSteps) { sSteps = nSteps; }
static int getNStep() { return sSteps; }
/// get the number of threads used for some of the calculations
static int getNThreads() { return sNThreads; }
/// set the number of threads used for some of the calculations
static void setNThreads(const int nThreads) { sNThreads = nThreads; }
/// set which kind of numerical integration is used for calcution of the integrals int Er/Ez dz, int Ephi/Ez dz, int Ez dz
/// \param strategy numerical integration strategy. see enum IntegrationStrategy for the different types
static void setNumericalIntegrationStrategy(const IntegrationStrategy strategy) { sNumericalIntegrationStrategy = strategy; }
static IntegrationStrategy getNumericalIntegrationStrategy() { return sNumericalIntegrationStrategy; }
static void setGlobalDistType(const GlobalDistType globalDistType) { sGlobalDistType = globalDistType; }
static GlobalDistType getGlobalDistType() { return sGlobalDistType; }
static void setGlobalDistCorrMethod(const GlobalDistCorrMethod globalDistCorrMethod) { sGlobalDistCorrCalcMethod = globalDistCorrMethod; }
static GlobalDistCorrMethod getGlobalDistCorrMethod() { return sGlobalDistCorrCalcMethod; }
static void setSimpsonNIteratives(const int nIter) { sSimpsonNIteratives = nIter; }
static int getSimpsonNIteratives() { return sSimpsonNIteratives; }
/// Set the space-charge distortions model
/// \param distortionType distortion type (constant or realistic)
static void setSCDistortionType(SCDistortionType distortionType) { sSCDistortionType = distortionType; }
/// Get the space-charge distortions model
static SCDistortionType getSCDistortionType() { return sSCDistortionType; }
void setUseInitialSCDensity(const bool useInitialSCDensity) { mUseInitialSCDensity = useInitialSCDensity; }
/// write all fields etc to a file
/// \param file output file where the electrical fields will be written to
/// \param side of the TPC
/// \param option "RECREATE" or "UPDATE"
void dumpToFile(std::string_view file, const Side side, std::string_view option) const;
/// write all fields etc to a file for both sides
/// \param file output file where the electrical fields will be written to
void dumpToFile(std::string_view file) const;
/// dump meta data to file (mC0, mC1, mC2, RegularGrid)
/// \param file output file
/// \param option "RECREATE" or "UPDATE"
/// \param overwriteExisting overwrite existing meta data in file
void dumpMetaData(std::string_view file, std::string_view option, const bool overwriteExisting = false) const;
/// set meta data from file (mC0, mC1, mC2, RegularGrid)
/// \param file input file to read from
void readMetaData(std::string_view file);
/// dump sc density, potential, electric fields, global distortions/corrections to tree
/// \param outFileName name of the output file
/// \param side of the TPC
/// \param nZPoints number of vertices of the output in z
/// \param nRPoints number of vertices of the output in r
/// \param nPhiPoints number of vertices of the output in phi
/// \param randomize randomize points
///
/// Adding other TTree as friend:
/// TFile f1("file_1.root");
/// TTree* tree1 = (TTree*)f1.Get("tree");
/// tree1->BuildIndex("globalIndex")
///
/// TFile f2("file_2.root");
/// TTree* tree2 = (TTree*)f2.Get("tree");
/// tree2->BuildIndex("globalIndex");
/// tree1->AddFriend(tree2, "t2");
void dumpToTree(const char* outFileName, const Side side, const int nZPoints = 50, const int nRPoints = 150, const int nPhiPoints = 180, const bool randomize = false) const;
/// dump to tree evaluated on the pads for given sector
/// \param outFileName name of the output file
/// \param sector TPC sector for which the TTree is created
/// \param nZPoints number of points in z to consider
void dumpToTree(const char* outFileName, const o2::tpc::Sector& sector, const int nZPoints = 50) const;
/// write electric fields to file using RDataFrame
/// \param file output file where the electrical fields will be written to
/// \param side of the TPC
/// \param option "RECREATE" or "UPDATE"
int dumpElectricFields(std::string_view file, const Side side, std::string_view option) const;
/// set electric field from root file using RDataFrame
/// \param file output file where the electrical fields will be written to