PandaRoot
BSEmcParameterLoadingProcess.h
Go to the documentation of this file.
1 #ifndef BSEMCPARAMETERLOADINGPROCESS_HH
2 #define BSEMCPARAMETERLOADINGPROCESS_HH
3 
4 #include "TObjString.h"
5 #include "TString.h"
6 
7 #include "FairRun.h"
8 
9 #include "PndProcess.h"
10 
13 #include "BSEmcPositionPar.h"
14 #include "BSEmcStructure.h"
15 
24 template <class DataStruct>
25 class BSEmcParameterLoadingProcess : public PndProcess<DataStruct> {
26  public:
27  BSEmcParameterLoadingProcess(const TString &t_neighbouringFilename = "EmcCrystalsNeighbouringRelations.txt", const TString &t_positionparFilename = "")
28  : PndProcess<DataStruct>("BSEmcParameterLoadingProcess"), fNeighbouringFilename(t_neighbouringFilename), fPositionParFilename(t_positionparFilename){};
30  virtual void Process(){};
31  virtual void SetData(DataStruct * /*t_data*/){};
32 
33  void SetNeighbouringFilename(const TString &t_neighbouringFilename) { fNeighbouringFilename = t_neighbouringFilename; }
34  void SetPositionParFilename(const TString &t_positionFilename) { fPositionParFilename = t_positionFilename; }
35 
36  void SetDetectorName(const std::string &t_detectorName)
37  {
38  this->fDetectorName = t_detectorName;
39  this->fParameterList.push_back(BSEmcCrystalPositionPar::fgParameterName + "Barrel");
40  this->fParameterList.push_back(BSEmcCrystalPositionPar::fgParameterName + "FwEndcap");
41  this->fParameterList.push_back(BSEmcCrystalPositionPar::fgParameterName + "BwEndcap");
42  this->fParameterList.push_back(BSEmcCrystalPositionPar::fgParameterName + "Shashlik");
43 
44  this->fParameterList.push_back(BSEmcPositionPar::fgParameterName + "Barrel");
45  this->fParameterList.push_back(BSEmcPositionPar::fgParameterName + "FwEndcap");
46  this->fParameterList.push_back(BSEmcPositionPar::fgParameterName + "BwEndcap");
47  this->fParameterList.push_back(BSEmcPositionPar::fgParameterName + "Shashlik");
48 
53  }
54 
55  void SetupParameters(const PndParameterRegister *t_parameter)
56  {
57  SetupPositionParameters(t_parameter);
58  SetupNeighbouringParameters(t_parameter);
59  }
60 
61  private:
62  TString fNeighbouringFilename{"EmcCrystalsNeighbouringRelations.txt"};
63  TString fPositionParFilename{""};
64 
65  void GetPositionParameterFromCrystals(BSEmcCrystalPositionPar *t_par, Int_t t_lowerDetectorId, Int_t t_upperDetectorId)
66  {
67  if (!BSEmcStructure::Instance()->IsDetIdXtalMapSet()) {
69  }
70  for (const auto &crystal : BSEmcStructure::Instance()->GetDetIdXtalMap()) {
71  Int_t id = crystal.first;
72 
73  if (t_lowerDetectorId <= id && id < t_upperDetectorId) {
74  BSEmcXtal *xtal = crystal.second.get();
75  t_par->SetPositionDataForCrystal(id, {xtal->GetCentre(), xtal->GetFrontCentre(), xtal->GetNormalToFrontFace(), xtal->GetAxisVector()});
76  }
77  }
78  }
79 
80  void ReadPositionParParameter(BSEmcCrystalPositionPar *t_par, Int_t t_lowerDetectorId, Int_t t_upperDetectorId)
81  {
82  LOG(debug) << "BSEmcParameterLoadingProcess::ReadPositionParParameter(" << fPositionParFilename << ").";
83 
84  std::string line;
85  std::ifstream txtfile(fPositionParFilename.Data());
86  if (txtfile.is_open()) {
87  while (std::getline(txtfile, line)) {
88  TString entry{line};
89  TString tmpstr;
90  std::unique_ptr<TObjArray> tokens{entry.Tokenize(":")};
91  Int_t crystalId = 0;
92  TVector3 vectorvalues[4];
93  if (tokens->GetEntriesFast() > 1) {
94  tmpstr = (dynamic_cast<TObjString *>(tokens->At(0)))->GetString();
95  crystalId = tmpstr.Atoi();
96  if (t_lowerDetectorId <= crystalId && crystalId < t_upperDetectorId) {
97  Int_t counter = 0, veccounter = 0;
98  for (Int_t tokenId = 1; tokenId < tokens->GetEntriesFast(); ++tokenId) {
99  tmpstr = (dynamic_cast<TObjString *>(tokens->At(tokenId)))->GetString();
100  vectorvalues[veccounter][counter] = tmpstr.Atof();
101  counter = (counter + 1) % 3;
102  if (counter == 0) {
103  ++veccounter;
104  }
105  }
106  t_par->SetPositionDataForCrystal(crystalId, BSEmcCrystalPositionData{vectorvalues[0], vectorvalues[1], vectorvalues[2], vectorvalues[3]});
107  }
108  }
109  }
110  txtfile.close();
111  } else {
112  LOG(error) << "BSEmcParameterLoadingProcess::ReadPositionParParameter(" << fPositionParFilename << ") - Unable to open file!";
113  }
114  }
115 
116  void FillPositionParameter(BSEmcCrystalPositionPar *t_posPar, Int_t t_inputVersion, Int_t t_lowerDetectorId, Int_t t_upperDetectorId)
117  {
118  if (fPositionParFilename.Length() != 0) {
119  ReadPositionParParameter(t_posPar, t_lowerDetectorId, t_upperDetectorId);
120  } else {
121  GetPositionParameterFromCrystals(t_posPar, t_lowerDetectorId, t_upperDetectorId);
122  }
123  t_posPar->UpdatePositionArray();
124  t_posPar->setChanged(kTRUE);
125  t_posPar->setInputVersion(t_inputVersion, 1);
126  }
127 
128  void SetupPositionParameter(BSEmcCrystalPositionPar *t_posPar, BSEmcPositionPar *t_digiPar)
129  {
130  t_posPar->SetPositionDepth(t_digiPar->GetDigiPositionDepth());
131  t_posPar->SetPositionMethod(t_digiPar->GetDigiPositionMethod());
132  t_posPar->FillMap();
133  }
134 
135  void SetupPositionParameters(const PndParameterRegister *t_paramRegister)
136  {
137  BSEmcCrystalPositionPar *barrelPar =
138  dynamic_cast<BSEmcCrystalPositionPar *>(t_paramRegister->GetParameter(TString{BSEmcCrystalPositionPar::fgParameterName + "Barrel"}.Data()));
139  BSEmcCrystalPositionPar *fwecPar =
140  dynamic_cast<BSEmcCrystalPositionPar *>(t_paramRegister->GetParameter(TString{BSEmcCrystalPositionPar::fgParameterName + "FwEndcap"}.Data()));
141  BSEmcCrystalPositionPar *bwecPar =
142  dynamic_cast<BSEmcCrystalPositionPar *>(t_paramRegister->GetParameter(TString{BSEmcCrystalPositionPar::fgParameterName + "BwEndcap"}.Data()));
143  BSEmcCrystalPositionPar *shashlikPar =
144  dynamic_cast<BSEmcCrystalPositionPar *>(t_paramRegister->GetParameter(TString{BSEmcCrystalPositionPar::fgParameterName + "Shashlik"}.Data()));
145  BSEmcPositionPar *barrelDigiPar = dynamic_cast<BSEmcPositionPar *>(t_paramRegister->GetParameter(TString{BSEmcPositionPar::fgParameterName + "Barrel"}.Data()));
146  BSEmcPositionPar *fwecDigiPar = dynamic_cast<BSEmcPositionPar *>(t_paramRegister->GetParameter(TString{BSEmcPositionPar::fgParameterName + "FwEndcap"}.Data()));
147  BSEmcPositionPar *bwecDigiPar = dynamic_cast<BSEmcPositionPar *>(t_paramRegister->GetParameter(TString{BSEmcPositionPar::fgParameterName + "BwEndcap"}.Data()));
148  BSEmcPositionPar *shashlikDigiPar = dynamic_cast<BSEmcPositionPar *>(t_paramRegister->GetParameter(TString{BSEmcPositionPar::fgParameterName + "Shashlik"}.Data()));
149 
150  if (!barrelPar->IsSet() || !fwecPar->IsSet() || !bwecPar->IsSet() || !shashlikPar->IsSet()) {
151 
152  FairRun *run = FairRun::Instance();
153  if (run == nullptr) {
154  Fatal("SetParContainers", "No analysis run");
155  }
156  FillPositionParameter(barrelPar, run->GetRunId(), 100000000, 300000000);
157  FillPositionParameter(fwecPar, run->GetRunId(), 300000000, 400000000);
158  FillPositionParameter(bwecPar, run->GetRunId(), 400000000, 500000000);
159  FillPositionParameter(shashlikPar, run->GetRunId(), 500000000, 600000000);
160  }
161 
162  SetupPositionParameter(barrelPar, barrelDigiPar);
163  SetupPositionParameter(fwecPar, fwecDigiPar);
164  SetupPositionParameter(bwecPar, bwecDigiPar);
165  SetupPositionParameter(shashlikPar, shashlikDigiPar);
166  }
167 
168  void ReadNeighbouringParameter(BSEmcGeoNeighbouringRelationPar *t_par, Int_t t_lowerId, Int_t t_upperId)
169  {
170  LOG(debug) << "BSEmcParameterLoadingProcess::ReadNeighbouringParameter(" << fNeighbouringFilename << ").";
171 
172  std::string line;
173  std::ifstream txtfile(fNeighbouringFilename.Data());
174  if (txtfile.is_open()) {
175  while (std::getline(txtfile, line)) {
176  TString entry{line};
177  TString tmpstr;
178  TObjArray *tokens = entry.Tokenize(":");
179  Int_t crystalId = 0;
180  std::vector<Int_t> neighbourIds;
181  if (tokens->GetEntriesFast() > 1) {
182  tmpstr = (dynamic_cast<TObjString *>(tokens->At(0)))->GetString();
183  crystalId = tmpstr.Atoi();
184  for (Int_t tokenId = 1; tokenId < tokens->GetEntriesFast(); ++tokenId) {
185  tmpstr = (dynamic_cast<TObjString *>(tokens->At(tokenId)))->GetString();
186  neighbourIds.push_back(tmpstr.Atoi());
187  }
188  if (t_lowerId <= crystalId && crystalId < t_upperId) {
189  t_par->RegisterIdAndNeighbours(crystalId, neighbourIds);
190  }
191  }
192  }
193  txtfile.close();
194  } else {
195  LOG(error) << "BSEmcParameterLoadingProcess::ReadNeighbouringParameter(" << fNeighbouringFilename << ") - Unable to open file!";
196  }
197  t_par->UpdateRelationArray();
198  }
199 
200  void LoadNeighbouringParameter(BSEmcGeoNeighbouringRelationPar *t_par, Int_t t_version, Int_t t_lowerId, Int_t t_upperId)
201  {
202  ReadNeighbouringParameter(t_par, t_lowerId, t_upperId);
203  t_par->setChanged(kTRUE);
204  t_par->setInputVersion(t_version, 1);
205  }
206 
207  void SetupNeighbouringParameters(const PndParameterRegister *t_paramRegister)
208  {
210  dynamic_cast<BSEmcGeoNeighbouringRelationPar *>(t_paramRegister->GetParameter(TString{BSEmcGeoNeighbouringRelationPar::fgParameterName + "Barrel"}.Data()));
211  BSEmcGeoNeighbouringRelationPar *fwEndcapPar =
212  dynamic_cast<BSEmcGeoNeighbouringRelationPar *>(t_paramRegister->GetParameter(TString{BSEmcGeoNeighbouringRelationPar::fgParameterName + "FwEndcap"}.Data()));
213  BSEmcGeoNeighbouringRelationPar *bwEndcapPar =
214  dynamic_cast<BSEmcGeoNeighbouringRelationPar *>(t_paramRegister->GetParameter(TString{BSEmcGeoNeighbouringRelationPar::fgParameterName + "BwEndcap"}.Data()));
215  BSEmcGeoNeighbouringRelationPar *shashlikPar =
216  dynamic_cast<BSEmcGeoNeighbouringRelationPar *>(t_paramRegister->GetParameter(TString{BSEmcGeoNeighbouringRelationPar::fgParameterName + "Shashlik"}.Data()));
217 
218  if (!barrelPar->IsSet() || !fwEndcapPar->IsSet() || !bwEndcapPar->IsSet() || !shashlikPar->IsSet()) {
219  FairRun *run = FairRun::Instance();
220  if (run == nullptr) {
221  Fatal(TString{"BSEmcParameterLoadingProcess::SetupNeighbouringParameters(" + fNeighbouringFilename + ")"}, "No analysis run");
222  }
223  LoadNeighbouringParameter(barrelPar, run->GetRunId(), 100000000, 300000000);
224  LoadNeighbouringParameter(fwEndcapPar, run->GetRunId(), 300000000, 400000000);
225  LoadNeighbouringParameter(bwEndcapPar, run->GetRunId(), 400000000, 500000000);
226  LoadNeighbouringParameter(shashlikPar, run->GetRunId(), 500000000, 600000000);
227  }
228  barrelPar->FillMap();
229  fwEndcapPar->FillMap();
230  bwEndcapPar->FillMap();
231  shashlikPar->FillMap();
232  }
233 };
234 
235 #endif /*BSEMCPARAMETERLOADINGPROCESS_HH*/
void SetDetectorName(const std::string &t_detectorName)
Set the Detector name. Important, as most EmcParameter need to know for which detector they need to b...
Base Process class.
Definition: PndProcess.h:24
FairParSet * GetParameter(const std::string &t_name) const
Access the parameter via the name.
Double_t GetDigiPositionDepth() const
virtual void Process()
The actual data transformation (digitizing, clustering, etc.) should be defined here.
static const std::string fgParameterName
void SetupParameters(const PndParameterRegister *t_parameter)
Fetch all parameters from the PndParameterRegister.
void SetPositionMethod(const TString &t_positionMethod)
void SetPositionDepth(Double_t t_depth)
void SetPositionParFilename(const TString &t_positionFilename)
void SetNeighbouringFilename(const TString &t_neighbouringFilename)
const TVector3 & GetCentre() const
void SetPositionDataForCrystal(Int_t t_id, const BSEmcCrystalPositionData &t_position)
static BSEmcStructure * Instance()
static std::string fgParameterName
Struct containing crystal position data.
void CreateDetIdXtalMap()
void RegisterIdAndNeighbours(Int_t t_id, std::vector< Int_t > &t_neighbours)
Parameter for crystal positions.
represents coordinates of one crystal
Definition: BSEmcXtal.h:46
const TVector3 & GetNormalToFrontFace() const
const Text_t * GetDigiPositionMethod() const
BSEmcParameterLoadingProcess(const TString &t_neighbouringFilename="EmcCrystalsNeighbouringRelations.txt", const TString &t_positionparFilename="")
const TVector3 & GetAxisVector() const
const TVector3 & GetFrontCentre() const
std::string fDetectorName
Set Detector name this PndProcess transforms data for. Required for example by EMC Processes to fetch...
Definition: PndProcess.h:99
std::vector< std::string > fParameterList
Parameter names required by this PndProcess. Needs to be populated in derived class.
Definition: PndProcess.h:98
Helper class to indirect the Parameter fetching via the FairRuntimeDb.
Neigbouring Relations based on a list of detectorIds.
virtual void SetData(DataStruct *)
Pass the data container ptrs to the process, and store pointers in class members. ...
Mini parameter set to contain position relevant parameter.
Process to load neighbouring and position parameters from txtfiles or position values via BSEmcStruct...