BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
expdataNN.C
Go to the documentation of this file.
1/*
2 BM@N alignment routine
3 BM@N experiment at NICA complex, JINR, 2025
4
5 Department: Math & Soft Group of HEP lab
6 Author: Igor Polev, polev@jinr.int
7
8 ROOT data export macro for NN alignment of each detector element.
9 Exports hits for each detector element into individual CSV files.
10 For each track computes best-fit straight line through all its hits
11 and stores the line coefficients alongside hit data.
12
13 Line fit model (independent fits in xz and yz planes):
14 x = xA + xB * z
15 y = yA + yB * z
16 Ordinary least squares, minimizing residuals in x and y.
17*/
18
19#ifndef __CLING__
20#include "BmnRootIterator.h"
21#include "BmnRun8cgDM.h"
23#include "BmnWeightedLine.h"
24#include "TStopwatch.h"
25
26#include <fstream>
27#include <map>
28#include <string>
29#include <vector>
30#endif
31
32#define BMN_IN_FILELIST "../cfg/dst7466noa-full.txt"
33#define BMN_EXPORT_PREFIX "/home/igor/DATA/bmn/nn/data_det"
34
35int expdataNN(int argc = 0, char** argv = nullptr)
36{
37 TStopwatch timer;
38
39 // prepare ROOT file readers
40 BmnRun8cgDM detModel;
41 detModel.Init();
43 BmnRootIterator data(BMN_IN_FILELIST, detModel, &pb);
44
45 std::cout << "Exporting data defined in " << BMN_IN_FILELIST << std::endl;
46 std::cout << data.GetTotalEvents() << " total events to process" << std::endl;
47 std::cout << detModel.Alignables() << " detector elements expected" << std::endl;
48
49 // per-detectorID output files and hit counters (opened on first use)
50 std::map<Int_t, std::ofstream*> files;
51 std::map<Int_t, Int_t> detHitCnt;
52
53 auto GetFile = [&](Int_t detID) -> std::ofstream& {
54 auto it = files.find(detID);
55 if (it != files.end())
56 return *(it->second);
57 std::string name = std::string(BMN_EXPORT_PREFIX) + std::to_string(detID) + ".csv";
58 auto* f = new std::ofstream(name, std::ofstream::out);
59 *f << "x,y,z,xA,xB,yA,yB\n";
60 files[detID] = f;
61 detHitCnt[detID] = 0;
62 return *f;
63 };
64
65 // process ROOT data
66 Int_t trackID{0}, hitCnt{0};
67 struct HitRec
68 {
69 Int_t detID;
70 Double_t x, y, z;
71 };
72
73 pb.Init();
74 data.ResetAll();
75 while (!data.EndOfTracks()) {
76
77 // fit the line and collect hits in one pass; ordinary least squares: unit weights
78 BmnWeightedLine lineX, lineY;
79 std::vector<HitRec> hits;
80 hits.reserve(data.HitsInTrack());
81
82 while (!data.EndOfHits()) {
83 hits.push_back({data.HitDetectorID(), data.HitX(), data.HitY(), data.HitZ()});
84 lineX.Add(data.HitZ(), data.HitX());
85 lineY.Add(data.HitZ(), data.HitY());
86 data.NextHit();
87 }
88 Double_t xA{lineX.At0()}, yA{lineY.At0()}, xB{lineX.Slope()}, yB{lineY.Slope()};
89
90 // write each hit to its detector file
91 for (const auto& h : hits) {
92 std::ofstream& f = GetFile(h.detID);
93 f << h.x << "," << h.y << "," << h.z << "," << xA << "," << xB << "," << yA << "," << yB << "\n";
94 detHitCnt[h.detID]++;
95 hitCnt++;
96 }
97
98 trackID++;
99 data.NextTrack();
100 }
101 pb.Clear();
102
103 // close all output files
104 for (auto& kv : files) {
105 kv.second->close();
106 delete kv.second;
107 }
108
109 std::cout << "Tracks exported: " << trackID << std::endl;
110 std::cout << "Hits exported: " << hitCnt << std::endl;
111 std::cout << "Output files: " << files.size() << std::endl;
112
113 if (files.size() != detModel.Alignables())
114 std::cout << "WARNING! Files count does not match detector count:"
115 << " some data may be missing!" << std::endl;
116 for (const auto& kv : detHitCnt)
117 std::cout << " det ID " << std::setw(10) << kv.first << " : " << std::setw(10) << kv.second << " hits\n";
118
119 // no memory cleanup in ROOT macro mode
120 std::cout << "Done in " << timer.RealTime() << "s" << std::endl;
121 return 0;
122}
int Int_t
double Double_t
Bool_t EndOfTracks() const noexcept
Bool_t EndOfHits() const noexcept
Bool_t Init(const char *targetGeoFile=nullptr)
virtual Double_t HitY() const override
virtual void ResetAll() override
virtual Int_t HitDetectorID() const override
virtual Double_t HitZ() const override
virtual Double_t HitX() const override
virtual Int_t HitsInTrack() const override
Long64_t GetTotalEvents() const noexcept
virtual void NextTrack() override
virtual void NextHit() override
virtual Int_t Alignables() const noexcept override
Definition BmnRun8DM.h:139
Double_t At0() const noexcept
void Add(Double_t z, Double_t v, Double_t w=1.0) noexcept
Double_t Slope() const noexcept
Double_t RealTime()
int expdataNN(int argc=0, char **argv=nullptr)
Definition expdataNN.C:35
#define BMN_EXPORT_PREFIX
Definition expdataNN.C:33
#define BMN_IN_FILELIST
Definition expdataNN.C:32