Skip to content

Commit 82339d1

Browse files
committed
Utilities for MagField object regeneration
1 parent 14ed93d commit 82339d1

4 files changed

Lines changed: 108 additions & 2 deletions

File tree

Common/Field/macro/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Macros for magnetic field manipulations
2+
3+
```o2::field::MagneticWrapperChebyshev``` class allows to dump the field data to text file and recreate it back as a ROOT object from this file. This is useful if one needs to change the name or the namespace of this class or persistent classes it usese internally (e.g. ``MathUtils/Chebyshev3D.h``, ``MathUtils/Chebyshev3DCalc.h``)
4+
5+
* macro ``extractMapsAsText.C``
6+
7+
Converts all magnetics field objects of MapClass in the inpFileName file to text files named as <prefix><map_name> in current directory, e.g.
8+
``root -b -q 'extractMapsAsText.C+("$O2_ROOT/share/Common/maps/mfchebKGI_sym.root")'``
9+
10+
* macro ``createMapsFromText.C``
11+
12+
Converts all text files with name pattern ``<path>/<prefix>*`` to magnetic field object of MapClass and stores them in the outFileName root file, e.g.
13+
``root -b -q 'createMapsFromText.C+'``
14+
will create local ``mfchebKGI_sym.root``, which can substitute ``O2/Common/maps/mfchebKGI_sym.root`` file.
15+
16+
Currently the MapClass is aliased to ``o2::field::MagneticWrapperChebyshev`` in both cases. If after ``extractMapsAsText.C`` macro the name of the underlying MapClass changes, this has to be reflected in the ``createMapsFromText.C``
17+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#if !defined(__CLING__) || defined(__ROOTCLING__)
2+
#include "Field/MagneticWrapperChebyshev.h"
3+
#include <TSystem.h>
4+
#include <TFile.h>
5+
#include <TKey.h>
6+
#include <TList.h>
7+
#include <TIterator.h>
8+
#include <string>
9+
#include <iostream>
10+
#endif
11+
12+
using MapClass = o2::field::MagneticWrapperChebyshev;
13+
14+
// This macro converts all text files with name pattern <path>/<prefix>* to
15+
// mag.field object of MapClass and stores them in the outFileName root file
16+
17+
int createMapsFromText(const std::string path = "./", // where to look for the text files
18+
const std::string outFileName = "mfchebKGI_sym.root",
19+
const std::string prefix = "_alice_mag_map_")
20+
{
21+
int nMaps = 0;
22+
TFile outFile(outFileName.c_str(), "recreate");
23+
if (!outFile.IsOpen() || outFile.IsZombie()) {
24+
std::cout << "Failed to open file " << outFileName << " to store the maps\n";
25+
return nMaps;
26+
}
27+
auto dir = gSystem->OpenDirectory(path.c_str());
28+
if (!dir) {
29+
std::cout << "Failed to check the path " << path << "\n";
30+
}
31+
const char* np = nullptr;
32+
while ((np = gSystem->GetDirEntry(dir))) {
33+
std::string inpName = np;
34+
35+
if (inpName.rfind(prefix, 0) == 0) { // this is a mag. map text file
36+
std::cout << "Creating map from " << inpName << "\n";
37+
MapClass magmap;
38+
magmap.loadData(inpName.c_str());
39+
std::cout << "Saving new map " << magmap.GetName() << "\n";
40+
magmap.Write();
41+
nMaps++;
42+
}
43+
}
44+
outFile.Close();
45+
gSystem->FreeDirectory(dir);
46+
47+
return nMaps;
48+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#if !defined(__CLING__) || defined(__ROOTCLING__)
2+
#include "Field/MagneticWrapperChebyshev.h"
3+
#include <TSystem.h>
4+
#include <TFile.h>
5+
#include <TKey.h>
6+
#include <TList.h>
7+
#include <TIterator.h>
8+
#include <string>
9+
#include <iostream>
10+
#endif
11+
12+
using MapClass = o2::field::MagneticWrapperChebyshev;
13+
14+
// This macro converts all mag.field objects in the inpFileName file to text files named as <prefix><map_name>
15+
// in current directory
16+
17+
int extractMapsAsText(const std::string inpFileName = "$O2_ROOT/share/Common/maps/mfchebKGI_sym.root",
18+
const std::string prefix = "_alice_mag_map_")
19+
{
20+
int nMaps = 0;
21+
TFile inpFile(inpFileName.c_str());
22+
if (!inpFile.IsOpen() || inpFile.IsZombie()) {
23+
std::cout << "Failed to open file " << inpFileName << " with maps\n";
24+
return nMaps;
25+
}
26+
27+
TIter nextKey(inpFile.GetListOfKeys());
28+
TObject* obj = nullptr;
29+
while ((obj = nextKey())) {
30+
if (obj->IsA() == TKey::Class()) {
31+
auto magmap = dynamic_cast<MapClass*>(inpFile.Get(obj->GetName()));
32+
if (!magmap) {
33+
std::cout << "Failed to load map " << obj->GetName();
34+
return -nMaps;
35+
}
36+
std::string outName = prefix + obj->GetName() + ".txt";
37+
std::cout << "Dumping map " << obj->GetName() << " as a text to " << outName << "\n";
38+
magmap->saveData(outName.c_str());
39+
nMaps++;
40+
}
41+
}
42+
return nMaps;
43+
}

Common/Field/src/MagneticWrapperChebyshev.cxx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,14 +1402,12 @@ void MagneticWrapperChebyshev::saveData(const char* outfile) const
14021402
fprintf(stream, "#\nEND SOLENOID\n");
14031403

14041404
// TPCIntegral part
1405-
fprintf(stream, "# Set of Chebyshev parameterizations for ALICE magnetic field\nSTART %s\n", GetName());
14061405
fprintf(stream, "START TPCINT\n#Number of pieces\n%d\n", mNumberOfParameterizationTPC);
14071406
for (int ip = 0; ip < mNumberOfParameterizationTPC; ip++)
14081407
getParameterTPCIntegral(ip)->saveData(stream);
14091408
fprintf(stream, "#\nEND TPCINT\n");
14101409

14111410
// TPCRatIntegral part
1412-
fprintf(stream, "# Set of Chebyshev parameterizations for ALICE magnetic field\nSTART %s\n", GetName());
14131411
fprintf(stream, "START TPCRatINT\n#Number of pieces\n%d\n", mNumberOfParameterizationTPCRat);
14141412
for (int ip = 0; ip < mNumberOfParameterizationTPCRat; ip++) {
14151413
getParameterTPCRatIntegral(ip)->saveData(stream);

0 commit comments

Comments
 (0)