Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
BankData.cxx
Go to the documentation of this file.
1//*-- Author : Bob Michaels, April 2018
2// This is based on Ole Hansen's example UserModule
3
5// //
6// BankData //
7// //
8// //
9// //
11
12#include "BankData.h"
13#include "THaVar.h"
14#include "THaVarList.h"
15#include "CodaDecoder.h"
16#include "THaGlobals.h"
17#include "Textvars.h" // vsplit
18#include <sstream>
19#include <iterator>
20#include <cctype> // isspace
21
22using namespace std;
23using namespace Decoder;
24using Podd::vsplit;
25
26//_____________________________________________________________________________
27class BankLoc { // Utility class used by BankData
28public:
29 BankLoc(std::string svar, Int_t iroc, Int_t ibank, Int_t ioff, Int_t inum)
30 : svarname(std::move(svar))
31 , roc(iroc)
32 , bank(ibank)
33 , offset(ioff)
34 , numwords(inum)
35 {}
36 std::string svarname;
38};
39
40//_____________________________________________________________________________
41BankData::BankData( const char* name, const char* description)
42 : THaPhysicsModule(name,description)
43 , Nvars(0)
44 , dvars(nullptr)
45 , vardata(nullptr)
46{
47 // Normal constructor.
48}
49
50//_____________________________________________________________________________
52{
53 // Destructor
55 delete[] dvars;
56 delete[] vardata;
57}
58
59//_____________________________________________________________________________
61{
62
63 if( !IsOK() ) return -1;
64
65 const auto* codaevdata = dynamic_cast<const CodaDecoder*>(&evdata);
66 if( !codaevdata )
67 return -1;
68
69 Int_t k=0;
70 for( const auto& bankloc : banklocs ) {
71 Int_t ret = codaevdata->FillBankData(vardata, bankloc->roc, bankloc->bank,
72 bankloc->offset, bankloc->numwords);
73 if( ret == THaEvData::HED_OK ) {
74 for( Int_t j = 0; j < bankloc->numwords; j++, k++ ) {
75 dvars[k] = vardata[j];
76 }
77 }
78 }
79
80 fDataValid = true;
81 return 0;
82}
83
84//_____________________________________________________________________________
86{
87 // Read the parameters of this module from the run database
88
89// const char* const here = "ReadDatabase";
90
91 // Read database
92
93 FILE* fi = OpenFile( date );
94 if( !fi ) return kFileError;
95
97
98 const int LEN = 200;
99 char cbuf[LEN];
100 const char comment = '#';
101
102 while( fgets(cbuf, LEN, fi) ) {
103 if( fDebug )
104 cout << "database line = " << cbuf << endl;
105 const vector<string> dbline = vsplit(cbuf);
106 if( dbline.size() <= 2 )
107 continue;
108 assert(!dbline.front().empty() && !isspace(dbline.front()[0])); // else bug in vsplit
109 if( dbline.front()[0] == comment )
110 continue;
111 const string& svar = dbline[0];
112 Int_t iroc = atoi(dbline[1].c_str());
113 Int_t ibank = atoi(dbline[2].c_str());
114 Int_t ioff = 0, inum = 1;
115 if (dbline.size()>3) {
116 ioff = atoi(dbline[3].c_str());
117 if (dbline.size()>4) {
118 inum = atoi(dbline[4].c_str());
119 }
120 }
121 assert(!svar.empty());
122 if (fDebug) cout << "svar " << svar << " len " << svar.length()
123 << " iroc " << iroc << " ibank " << ibank
124 << " offset " << ioff << " num " << inum << endl;
125 banklocs.emplace_back(new BankLoc(svar, iroc, ibank, ioff, inum));
126 }
127
128 Int_t maxwords = 1, ibank = 0;
129 Nvars = 0;
130 for( const auto& bankloc : banklocs) {
131 if (fDebug) cout << "bankloc " << ibank++ << " " << bankloc->roc << " "
132 << bankloc->bank << " " << bankloc->offset << " "
133 << bankloc->numwords << endl;
134 if( bankloc->numwords > maxwords) maxwords = bankloc->numwords;
135 Nvars += bankloc->numwords;
136 }
137 // maxwords is the biggest number of words for all banklocs.
138 // dvars must be of length Nvars in order to fit all the data
139 // for this class; it's the global data.
140 // Check that neither of these are "too_big" for some reason.
141 const Int_t too_big = 1000000;
142 if (maxwords > too_big || Nvars > too_big) {
143 cerr << "BankData::ERROR: very huge number of words "
144 <<maxwords<<" "<<Nvars<<endl;
145 return kInitError;
146 }
147
148 delete [] vardata;
149 delete [] dvars; dvars = nullptr;
150 vardata = new UInt_t[maxwords];
151 if( Nvars > 0 ) {
152 dvars = new Double_t[Nvars];
153 //DEBUG?
154 for( Int_t i = 0; i < Nvars; i++ )
155 dvars[i] = 40 + i;
156 }
157
158 // warning to myself: do NOT call DefineVariables() here, it will lead to
159 // stale global data. Let the analyzer mechanisms call it (in the
160 // right sequence).
161
162 fStatus = kOK;
163
164 fclose(fi); // NOLINT(cert-err33-c) // read-only file, should always succeed
165 return fStatus;
166}
167
168//_____________________________________________________________________________
170{
171 // Define/delete global variables.
172
173 if (!gHaVars) {
174 cerr << "BankData::ERROR: No gHaVars ?! Well, that's a problem !!"<<endl;
175 return kInitError;
176 }
177
178 if (fDebug) cout << "BankData:: DefVars " << fName << " " << Nvars << endl;
179 Int_t k=0;
180 for( const auto& bankloc : banklocs ) {
181 string svarname = std::string(fName.Data()) + "." + bankloc->svarname;
182 string cdesc = "Bank Data " + bankloc->svarname;
183 if (bankloc->numwords == 1) {
184 if (fDebug) cout << "numwords = 1, svarname = " << svarname << endl;
185 if( mode == kDefine )
186 gHaVars->Define(svarname.c_str(), cdesc.c_str(), dvars[k]);
187 else
188 gHaVars->RemoveName(svarname.c_str());
189 k++;
190 } else {
191 for (Int_t j=0; j<bankloc->numwords; j++) {
192 ostringstream os;
193 os << svarname << j;
194 if (fDebug) cout << "numwords > 1, svarname = " << os.str() << endl;
195 if( mode == kDefine )
196 gHaVars->Define(os.str().c_str(), cdesc.c_str(), dvars[k]);
197 else
198 gHaVars->RemoveName(os.str().c_str());
199 k++;
200 }
201 }
202 }
203 fIsSetup = ( mode == kDefine );
204 return kOK;
205}
206
207//_____________________________________________________________________________
int Int_t
unsigned int UInt_t
double Double_t
Option_t Option_t TPoint TPoint const char mode
char name[80]
R__EXTERN class THaVarList * gHaVars
Definition THaGlobals.h:11
Double_t * dvars
Definition BankData.h:35
BankData(const char *name, const char *description)
Definition BankData.cxx:41
virtual Int_t Process(const THaEvData &evdata)
Definition BankData.cxx:60
std::vector< std::unique_ptr< BankLoc > > banklocs
Definition BankData.h:38
UInt_t * vardata
Definition BankData.h:36
virtual Int_t ReadDatabase(const TDatime &date)
Definition BankData.cxx:85
virtual Int_t DefineVariables(EMode mode=kDefine)
Definition BankData.cxx:169
Int_t Nvars
Definition BankData.h:34
virtual ~BankData()
Definition BankData.cxx:51
std::string svarname
Definition BankData.cxx:36
Int_t offset
Definition BankData.cxx:37
Int_t numwords
Definition BankData.cxx:37
Int_t roc
Definition BankData.cxx:37
Int_t bank
Definition BankData.cxx:37
BankLoc(std::string svar, Int_t iroc, Int_t ibank, Int_t ioff, Int_t inum)
Definition BankData.cxx:29
virtual Int_t FillBankData(UInt_t *rdat, UInt_t roc, Int_t bank, UInt_t offset=0, UInt_t num=1) const
Bool_t IsOK() const
virtual FILE * OpenFile(const TDatime &date)
virtual Int_t RemoveName(const char *name)
THaVar * Define(const char *name, const char *descript, const Double_t &var, const Int_t *count=nullptr)
Definition THaVarList.h:22
TString fName
const char * Data() const
STL namespace.
ClassImp(TPyArg)