BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnFunctionAna.cxx
Go to the documentation of this file.
1#include "BmnFunctionAna.h"
2
3#include "BmnFieldMapSym3.h"
4#include "BmnGlobalTrack.h"
5#include "BmnMCInfoDst.h"
6#include "BmnNewFieldMap.h"
7#include "CbmMCTrack.h"
8#include "CbmTrackMatch.h"
9#include "CbmVertex.h"
10#include "DstRunHeader.h"
11#include "FairLogger.h"
12#include "FairTrackParam.h"
13#include "TChain.h"
14#include "TClonesArray.h"
15#include "TFile.h"
16#include "TMath.h"
17#include "TStopwatch.h"
18#include "TSystem.h"
19
20#include <ROOT/RDFHelpers.hxx>
21#include <ROOT/RDataFrame.hxx>
22#include <fstream>
23#include <iostream>
24#include <list>
25#include <string>
26using namespace std;
27
28// get magnetic field and run information from a specified DST file
29// fileName: DST file path [in]
30// fieldScale: magnetic field scale [out]; fieldType: magnetic field type [out]
31// beam, target: names of the beam and target, e.g. 'Xe' and 'CsI (2%)' [out, optional]
32// beam_energy: beam kinetic energy per nucleon in AGeV [out, optional]
33// beam_a, beam_z, target_a, target_z: mass and charge numbers of the beam and target nuclei [out, optional]
34// NB: the names are empty and A and Z are equal to -1 if the nucleus was not defined at the reconstruction stage,
35// target A is equal to 0 if the empty target
36// returns magnetic field, nullptr - if some errors occured
38 Double_t& fieldScale,
39 Int_t& fieldType,
40 TString* beam,
41 TString* target,
42 Double_t* beam_energy,
43 Int_t* beam_a,
44 Int_t* beam_z,
45 Int_t* target_a,
46 Int_t* target_z)
47{
48 gSystem->ExpandPathName(dstFileName);
49 if (gSystem->AccessPathName(dstFileName.Data()) == true) {
50 LOG(error) << "GetDSTField: no specified file: " << dstFileName;
51 return nullptr;
52 }
53
54 TFile* dst_file = TFile::Open(dstFileName.Data());
55 if (dst_file->IsZombie()) {
56 LOG(error) << "GetDSTField: opening the input file failed: " << dstFileName;
57 return nullptr;
58 }
59
60 // Get a pointer to "DstRunHeader"
61 DstRunHeader* pRunHeader = dynamic_cast<DstRunHeader*>(dst_file->Get("DstRunHeader"));
62 if (!pRunHeader) {
63 LOG(error) << "GetDSTField: No 'DstRunHeader' object found in the input file: " << dstFileName;
64 dst_file->Close();
65 return nullptr;
66 }
67
68 // get the beam and target information from the 'DstRunHeader' object
69 if (beam)
70 *beam = pRunHeader->GetBeam();
71 if (target)
72 *target = pRunHeader->GetTarget();
73 if (beam_a)
74 *beam_a = pRunHeader->GetBeamA();
75 if (beam_z)
76 *beam_z = pRunHeader->GetBeamZ();
77 if (beam_energy)
78 *beam_energy = pRunHeader->GetBeamEnergy();
79 if (target_a)
80 *target_a = pRunHeader->GetTargetA();
81 if (target_z)
82 *target_z = pRunHeader->GetTargetZ();
83
84 // get field type and scale from the 'DstRunHeader' object
85 fieldType = pRunHeader->GetFieldType();
86 fieldScale = pRunHeader->GetFieldScale();
87 TString fieldName = pRunHeader->GetFieldName();
88 FairField* magField = nullptr;
89 switch (fieldType) {
90 case 1: {
91 magField = new BmnNewFieldMap(fieldName.Data());
92 (dynamic_cast<BmnNewFieldMap*>(magField))->SetScale(fieldScale);
93 magField->Init();
94 break;
95 }
96 case 3: {
97 magField = new BmnFieldMapSym3(fieldName.Data());
98 (dynamic_cast<BmnFieldMapSym3*>(magField))->SetScale(fieldScale);
99 magField->Init();
100 break;
101 }
102 default:
103 LOG(error) << "GetDSTField: This type of the magnetic field is not supported: " << fieldType;
104 }
105
106 dst_file->Close();
107 return magField;
108}
109
110// write the reconstructed global tracks from the simulated DST files to a text file
111int BmnFunctionAna::ConvertRecoToTxt(TString input_list_file, TString output_file, TString configFileName)
112{
113 TStopwatch timer;
114 timer.Start();
115
116 gSystem->ExpandPathName(input_list_file);
117
118 list<string> file_list;
119 ifstream list_file(input_list_file.Data());
120 string s;
121 while (getline(list_file, s))
122 file_list.push_back(s);
123 if (file_list.empty()) {
124 cout << "WARNING: There are no files in the list of the input text file: " << input_list_file << endl;
125 return 0;
126 }
127
128 TChain chain("bmndata");
129 for (auto const& cur_file_path : file_list) {
130 TString strFilePath(cur_file_path);
131 gSystem->ExpandPathName(strFilePath);
132
133 chain.Add(strFilePath);
134 }
135
136 /*
137 // read configuration variables
138 configFileName = gSystem->ExpandPathName("$VMCWORKDIR/config/" + configFileName);
139 ifstream configFile(configFileName.Data());
140 if (!configFile.is_open()) {
141 cout << "[ERROR] Cannot open config file " << configFileName << endl;
142 return -2;
143 }
144 json config;
145 try {
146 configFile >> config;
147 }
148 catch (const json::parse_error& ex) {
149 cout << "[ERROR] Wrong config file format: " << ex.what() << endl;
150 return -3;
151 }
152 Int_t minNTracks = config.value("minNTracks", 2);
153 Double_t minZ = config.value("minZ", -TMath::Infinity());
154 Double_t maxZ = config.value("maxZ", TMath::Infinity());
155 vector<string> excludedBranches = config.value("excludedBranches", vector<string>());
156
157 if (outFormat != "root") {
158 cout << "[ERROR] Only \"root\" format is supported at the moment" << endl;
159 return -4;
160 }
161 */
162 /*
163 TFile* inFile = TFile::Open(inFilePath.Data());
164 if (inFile == nullptr) {
165 cout << "[ERROR] Cannot open input DST file " << inFilePath << endl;
166 return -5;
167 }
168 TTree* inTree = (TTree*) inFile->Get("bmndata");
169 if (!inTree) {
170 cout << "[ERROR] Input file " << inFilePath << " does not contain \"bmndata\" tree" << endl;
171 return -6;
172 }
173 *//*
174 // exclude branches
175 vector<string> branches;
176 for (TObject* branch: *inTree->GetListOfBranches()) {
177 string branchName = ((TBranch*)branch)->GetName();
178 if (find(excludedBranches.begin(), excludedBranches.end(), branchName) == excludedBranches.end())
179 branches.push_back(branchName);
180 }
181*/
182 ofstream file(output_file.Data());
183 file << "event# firstVx firstVy firstVz lastVx lastVy lastVz firstTx firstTy lastTx lastTy firstQp lastQp "
184 "firstCov[0] firstCov[1] firstCov[2] firstCov[3] firstCov[4] firstCov[5] firstCov[6] firstCov[7] "
185 "firstCov[8] firstCov[9] firstCov[10] firstCov[11] firstCov[12] firstCov[13] firstCov[14] "
186 "lastCov[0] lastCov[1] lastCov[2] lastCov[3] lastCov[4] lastCov[5] lastCov[6] lastCov[7] "
187 "lastCov[8] lastCov[9] lastCov[10] lastCov[11] lastCov[12] lastCov[13] lastCov[14] "
188 "Chi2 NDF Length Nhits Beta400 Beta700 fDCAInVertex PDGCode"
189 << endl;
190
191 // velocity measured by ToF-400 (tof_id = 1) or ToF-700 (tof_id = 2). The global track does not store it since the
192 // change of the BmnGlobalTrack format, but it stores the squared mass computed from it by BmnGlobalTracking as
193 // m^2 = p^2 * (1/beta^2 - 1) with p = 1/(q/p) at the first track point, so the velocity is restored from the mass.
194 // -1000 is returned if the track has no ToF measurement, as the former BmnGlobalTrack::GetBeta() did
195 auto get_beta = [](BmnGlobalTrack* track, Int_t tof_id) -> Double_t {
196 Float_t mass2 = track->GetMass2(tof_id);
197 Double_t qp = track->GetParamFirst()->GetQp();
198 if ((mass2 == -1000.0) || (qp == 0))
199 return -1000.0;
200 Double_t denominator = 1. + mass2 * qp * qp;
201 return (denominator > 0) ? 1. / TMath::Sqrt(denominator) : -1000.0;
202 };
203
204 auto save_to_txt = [&file, &get_beta](const TClonesArray& global_tracks, const TClonesArray& sts_tracks,
205 const TClonesArray& sts_match, const BmnMCInfoDst& mc_info, int col_iter) {
206 for (int i = 0; i < global_tracks.GetEntries(); i++) {
207 BmnGlobalTrack* cur_global_track = static_cast<BmnGlobalTrack*>(global_tracks.UncheckedAt(i));
208
209 // check that there is information on Beta from TOF-400 or TOF-700
210 // (an invalid Beta is defined by the range check, consistently with BmnGlobalTrack::GetMass2)
211 Double_t beta400 = get_beta(cur_global_track, 1), beta700 = get_beta(cur_global_track, 2);
212 if ((beta400 < -999.0) and (beta700 < -999.0))
213 continue;
214
215 // get PDG code
216 CbmTrackMatch* sts_match_track =
217 static_cast<CbmTrackMatch*>(sts_match.UncheckedAt(cur_global_track->GetGemTrackIndex()));
218 if (sts_match_track->GetMCTrackId() < 0)
219 continue;
220 CbmMCTrack* mc_track =
221 static_cast<CbmMCTrack*>(mc_info.GetMCTracks()->UncheckedAt(sts_match_track->GetMCTrackId()));
222 // cout<<"PDG code = "<<mc_track->GetPdgCode()<<endl;
223
224 FairTrackParam* param_first = cur_global_track->GetParamFirst();
225 FairTrackParam* param_last = cur_global_track->GetParamLast();
226
227 file << col_iter << " " << param_first->GetX() << " " << param_first->GetY() << " " << param_first->GetZ()
228 << " " << param_last->GetX() << " " << param_last->GetY() << " " << param_last->GetZ() << " "
229 << param_first->GetTx() << " " << param_first->GetTy() << " " << param_last->GetTx() << " "
230 << param_last->GetTy() << " " << param_first->GetQp() << " " << param_last->GetQp() << " ";
231 Double_t cov_matrix1[15];
232 param_first->CovMatrix(cov_matrix1);
233 for (int j = 0; j < 15; j++)
234 file << cov_matrix1[j] << " ";
235 // the covariance matrix at the last point of the track ('lastCov' columns)
236 Double_t cov_matrix2[15];
237 param_last->CovMatrix(cov_matrix2);
238 for (int j = 0; j < 15; j++)
239 file << cov_matrix2[j] << " ";
240 file << cur_global_track->GetChi2() << " " << cur_global_track->GetNDF() << " "
241 << cur_global_track->GetLength() << " " << cur_global_track->GetNHits() << " " << beta400 << " "
242 << beta700 << " " << cur_global_track->GetDCAInVertex() << " " << mc_track->GetPdgCode() << endl;
243 }
244 return;
245 };
246
247 // function to cut events without vertex
248 auto cutVertex = [](const CbmVertex& vtx) { return (vtx.GetNTracks() > 1); };
249
250 int col_iter = -1;
251 ROOT::RDataFrame data(chain);
252 auto w_iterator =
253 data.Filter(cutVertex, {"PrimaryVertex."})
254 //.Define("pdg_code", definePDG, {"BmnGlobalTrack", "StsVector", "StsTrackMatch", "BmnMCInfo."})
255 .Define("col_iter", [&col_iter] { return ++col_iter; });
256
258 w_iterator.Foreach(save_to_txt, {"BmnGlobalTrack", "StsVector", "StsTrackMatch", "BmnMCInfo.", "col_iter"});
259
260 file.close();
261 timer.Stop();
262 Double_t rtime = timer.RealTime(), ctime = timer.CpuTime();
263 printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime);
264 return 0;
265}
int i
Definition P4_F32vec4.h:22
int Int_t
float Float_t
double Double_t
R__EXTERN TSystem * gSystem
BmnFieldMapSym3.h.
static FairField * GetDSTField(TString fileName, Double_t &fieldScale, Int_t &fieldType, TString *beam=nullptr, TString *target=nullptr, Double_t *beam_energy=nullptr, Int_t *beam_a=nullptr, Int_t *beam_z=nullptr, Int_t *target_a=nullptr, Int_t *target_z=nullptr)
static int ConvertRecoToTxt(TString input_list_file, TString output_file="reco_tracks.txt", TString configFileName="recotxt_config.json")
BmnGlobalTrack.h.
Int_t GetGemTrackIndex() const
Accessors.
Float_t GetDCAInVertex()
Float_t GetMass2(Int_t tofID) const
Field Map does not have any symmetries w.r.t coordinate axises.
Int_t GetNDF() const
Definition BmnTrack.h:57
Float_t GetLength() const
Definition BmnTrack.h:61
FairTrackParam * GetParamFirst()
Definition BmnTrack.h:63
Float_t GetChi2() const
Definition BmnTrack.h:55
Int_t GetNHits() const
Accessors.
Definition BmnTrack.h:49
FairTrackParam * GetParamLast()
Definition BmnTrack.h:65
CbmMCTrack.h.
Definition CbmMCTrack.h:26
Int_t GetPdgCode() const
Accessors.
Definition CbmMCTrack.h:56
CbmStsTrackMatch.h.
Int_t GetMCTrackId() const
Index of matched MC track.
CbmVertex.h.
Definition CbmVertex.h:21
TString GetTarget()
Int_t GetTargetA()
TString GetFieldName()
Double_t GetFieldScale()
TString GetBeam()
Int_t GetFieldType()
Int_t GetTargetZ()
Int_t GetBeamA()
Int_t GetBeamZ()
Float_t GetBeamEnergy()
RInterface< RDFDetail::RFilter< F, Proxied >, DS_t > Filter(F f, const ColumnNames_t &columns={}, std::string_view name="")
virtual Int_t Add(const char *name, Long64_t nentries=TTree::kMaxEntries)
T * Get(const char *namecycle)
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
void Close(Option_t *option="") override
R__ALWAYS_INLINE Bool_t IsZombie() const
Double_t RealTime()
void Start(Bool_t reset=kTRUE)
Double_t CpuTime()
void Stop()
const char * Data() const
void AddProgressBar(ROOT::RDataFrame df)
Double_t Sqrt(Double_t x)
Definition json.hpp:4446