BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnMille.cxx
Go to the documentation of this file.
1#include "BmnMille.h"
2//___________________________________________________________________________
3
4/// Opens outFileName (by default as binary file).
5
6/**
7 * \param[in] outFileName file name
8 * \param[in] asBinary flag for binary
9 * \param[in] writeZero flag for keeping of zeros
10 */
11BmnMille::BmnMille(const Char_t* outFileName, Bool_t asBinary, Bool_t writeZero)
12 : myOutFile(outFileName, (asBinary ? (std::ios::binary | std::ios::out) : std::ios::out))
13 , myAsBinary(asBinary)
14 , myWriteZero(writeZero)
15 , myBufferPos(-1)
17{
18 // Instead myBufferPos(-1), myHasSpecial(false) and the following two lines
19 // we could call newSet() and kill()...
20 myBufferInt[0] = 0;
21 myBufferFloat[0] = 0.;
22
23 if (!myOutFile.is_open()) {
24 cerr << "Mille::Mille: Could not open " << outFileName << " as output file." << endl;
25 }
26}
27
28//___________________________________________________________________________
29/// Closes file.
30
32{
33 myOutFile.close();
34}
35
36//___________________________________________________________________________
37/// Add measurement to buffer.
38
39/**
40 * \param[in] NLC number of local derivatives
41 * \param[in] derLc local derivatives
42 * \param[in] NGL number of global derivatives
43 * \param[in] derGl global derivatives
44 * \param[in] label global labels
45 * \param[in] rMeas measurement (residuum)
46 * \param[in] sigma error
47 */
49 const Double_t* derLc,
50 Int_t NGL,
51 const Double_t* derGl,
52 const Int_t* label,
53 Double_t rMeas,
54 Double_t sigma)
55{
56 if (sigma <= 0.)
57 return;
58 if (myBufferPos == -1)
59 this->newSet(); // start, e.g. new track
60 if (!this->checkBufferSize(NLC, NGL))
61 return;
62
63 // first store measurement
67
68 // store local derivatives and local 'lables' 1,...,NLC
69 for (Int_t i = 0; i < NLC; ++i) {
70 if (derLc[i] || myWriteZero) { // by default store only non-zero derivatives
72 myBufferFloat[myBufferPos] = derLc[i]; // local derivatives
73 myBufferInt[myBufferPos] = i + 1; // index of local parameter
74 }
75 }
76
77 // store uncertainty of measurement in between locals and globals
81
82 // store global derivatives and their labels
83 for (Int_t i = 0; i < NGL; ++i) {
84 if (derGl[i] || myWriteZero) { // by default store only non-zero derivatives
85 if ((label[i] > 0 || myWriteZero) && label[i] <= myMaxLabel) { // and for valid labels
87 myBufferFloat[myBufferPos] = derGl[i]; // global derivatives
88 myBufferInt[myBufferPos] = label[i]; // index of global parameter
89 } else {
90 cerr << "Mille::mille: Invalid label " << label[i] << " <= 0 or > " << myMaxLabel << endl;
91 }
92 }
93 }
94}
95
96//___________________________________________________________________________
97/// Add special data to buffer.
98
99/**
100 * \param[in] nSpecial number of floats/ints
101 * \param[in] floatings floats
102 * \param[in] integers ints
103 */
104void BmnMille::special(Int_t nSpecial, const Double_t* floatings, const Int_t* integers)
105{
106 if (nSpecial == 0)
107 return;
108 if (myBufferPos == -1)
109 this->newSet(); // start, e.g. new track
110 if (myHasSpecial) {
111 cerr << "Mille::special: Special values already stored for this record." << endl;
112 return;
113 }
114 if (!this->checkBufferSize(nSpecial, 0))
115 return;
116 myHasSpecial = kTRUE; // after newSet() (Note: MILLSP sets to buffer position...)
117
118 // myBufferFloat[.] | myBufferInt[.]
119 // ------------------------------------
120 // 0.0 | 0
121 // -float(nSpecial) | 0
122 // The above indicates special data, following are nSpecial floating and nSpecial integer data.
123
124 ++myBufferPos; // zero pair
127
128 ++myBufferPos; // nSpecial and zero
129 myBufferFloat[myBufferPos] = -nSpecial; // automatic conversion to float
131
132 for (Int_t i = 0; i < nSpecial; ++i) {
133 ++myBufferPos;
134 myBufferFloat[myBufferPos] = floatings[i];
135 myBufferInt[myBufferPos] = integers[i];
136 }
137}
138
139//___________________________________________________________________________
140/// Reset buffers, i.e. kill derivatives accumulated for current set.
141
143{
144 myBufferPos = -1;
145}
146
147//___________________________________________________________________________
148/// Write buffer (set of derivatives with same local parameters) to file.
149
151{
152 if (myBufferPos > 0) { // only if anything stored...
153 const Int_t numWordsToWrite = (myBufferPos + 1) * 2;
154
155 if (myAsBinary) {
156 myOutFile.write(reinterpret_cast<const char*>(&numWordsToWrite), sizeof(numWordsToWrite));
157 myOutFile.write(reinterpret_cast<char*>(myBufferFloat), (myBufferPos + 1) * sizeof(myBufferFloat[0]));
158 myOutFile.write(reinterpret_cast<char*>(myBufferInt), (myBufferPos + 1) * sizeof(myBufferInt[0]));
159 } else {
160 myOutFile << numWordsToWrite << "\n";
161 for (Int_t i = 0; i < myBufferPos + 1; ++i) {
162 myOutFile << myBufferFloat[i] << " ";
163 }
164 myOutFile << "\n";
165
166 for (Int_t i = 0; i < myBufferPos + 1; ++i) {
167 myOutFile << myBufferInt[i] << " ";
168 }
169 myOutFile << "\n";
170 }
171 }
172 myBufferPos = -1; // reset buffer for next set of derivatives
173}
174
175//___________________________________________________________________________
176/// Initialize for new set of locals, e.g. new track.
177
179{
180 myBufferPos = 0;
182 myBufferFloat[0] = 0.0;
183 myBufferInt[0] = 0; // position 0 used as error counter
184}
185
186//___________________________________________________________________________
187/// Enough space for next nLocal + nGlobal derivatives incl. measurement?
188
189/**
190 * \param[in] nLocal number of local derivatives
191 * \param[in] nGlobal number of global derivatives
192 * \return true if sufficient space available (else false)
193 */
195{
196 if (myBufferPos + nLocal + nGlobal + 2 >= myBufferSize) {
197 ++(myBufferInt[0]); // increase error count
198 std::cerr << "Mille::checkBufferSize: Buffer too short (" << myBufferSize << "),"
199 << "\n need space for nLocal (" << nLocal << ")"
200 << "/nGlobal (" << nGlobal << ") local/global derivatives, " << myBufferPos + 1 << " already stored!"
201 << std::endl;
202 return kFALSE;
203 } else {
204 return kTRUE;
205 }
206}
int i
Definition P4_F32vec4.h:22
bool Bool_t
int Int_t
char Char_t
constexpr Bool_t kFALSE
double Double_t
constexpr Bool_t kTRUE
void end()
Write buffer (set of derivatives with same local parameters) to file.
Definition BmnMille.cxx:150
Int_t myBufferPos
position in buffer
Definition BmnMille.h:43
@ myBufferSize
Definition BmnMille.h:39
Float_t myBufferFloat[myBufferSize]
to collect derivatives etc.
Definition BmnMille.h:42
Bool_t myHasSpecial
if true, special(..) already called for this record
Definition BmnMille.h:44
Bool_t myWriteZero
if true also write out derivatives/labels ==0
Definition BmnMille.h:34
void mille(Int_t NLC, const Double_t *derLc, Int_t NGL, const Double_t *derGl, const Int_t *label, Double_t rMeas, Double_t sigma)
Add measurement to buffer.
Definition BmnMille.cxx:48
~BmnMille()
Closes file.
Definition BmnMille.cxx:31
BmnMille(const Char_t *outFileName, Bool_t asBinary=kTRUE, Bool_t writeZero=kFALSE)
Opens outFileName (by default as binary file).
Definition BmnMille.cxx:11
void kill()
Reset buffers, i.e. kill derivatives accumulated for current set.
Definition BmnMille.cxx:142
std::ofstream myOutFile
C-binary for output.
Definition BmnMille.h:32
Bool_t myAsBinary
if false output as text
Definition BmnMille.h:33
Int_t myBufferInt[myBufferSize]
to collect labels etc.
Definition BmnMille.h:41
@ myMaxLabel
Definition BmnMille.h:49
void special(Int_t nSpecial, const Double_t *floatings, const Int_t *integers)
Add special data to buffer.
Definition BmnMille.cxx:104
void newSet()
Initialize for new set of locals, e.g. new track.
Definition BmnMille.cxx:178
Bool_t checkBufferSize(Int_t nLocal, Int_t nGlobal)
Enough space for next nLocal + nGlobal derivatives incl. measurement?
Definition BmnMille.cxx:194
Definition json.hpp:4446