FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairParamList.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 //*-- AUTHOR : Ilse Koenig
9 //*-- Last modified : 28/01/2009 by Ilse Koenig
10 
11 #include "FairParamList.h"
12 
13 #include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN
14 
15 #include <TArrayD.h> // for TArrayD
16 #include <TArrayF.h> // for TArrayF
17 #include <TArrayI.h> // for TArrayI
18 #include <TBuffer.h> // for TBuffer, etc
19 #include <TBufferFile.h> // for TBufferFile
20 #include <TClass.h> // for TClass
21 #include <TCollection.h> // for TIter
22 #include <TStreamerInfo.h> // for TStreamerInfo
23 #include <iostream> // for operator<<, ostream, cout, etc
24 #include <string.h> // for memcpy, strcmp, strlen
25 
26 //_HADES_CLASS_DESCRIPTION
28 //
29 // FairParamObj
30 //
31 // Class for parameters stored in binary format in Oracle and used as list
32 // elements in FairParamList::paramList.
33 //
34 // The overloaded constructors and fill-functions accept single values or arrays of type
35 // UChar_t, Int_t, Float_t, Double_t, char_t, Text_t.
36 // The arguments for arrays are
37 // the name of the parameter
38 // the pointer to the array
39 // the length of the array
40 // The data are automatically converted to an UChar_t array.
41 // For classes also the class version is stored, for ROOT classes also the TStreamerInfo.
42 //
43 // -------------------------------------------------------------------------------------
44 //
45 // FairParamList
46 //
47 // Class for the generic Oracle and ASCII interface for parameter containers
48 // derived from HParCond
49 //
50 // The class contains a list to stores objects of type FairParamObj
51 // The list objects store the name, the value, the parameter type and some
52 // additional information depending for the object type.
53 //
54 // All add/addObject functions add an initialized parameter to the list for
55 // writing. The functions create a new list element and copy the data into
56 // this object.
57 // Add functions:
58 // 1. accepted basic types: Int_t, Float_t, Double_t, UChar_t
59 // 2. accepted ROOT arrays: TArrayI, TArrayF, TArrayD, TArrayC
60 // 3. accepted string: Text_t*
61 // This can be any text, for example also numbers in hexadecimal or
62 // scientific format. The number of characters must be specified (default 1).
63 // AddObject function:
64 // Accepts classes derived from TObject.
65 // The persistent data elements are streamed into an UChar_t array using the
66 // class streamer. For ROOT classes, for example histograms, the ROOT streamer
67 // info is stored in an additional binary array.
68 //
69 // All fill/fillObject functions convert the data in the list element back
70 // to the type of the parameter and copies them into the data element in the
71 // initialization process.
72 // 1. Single parameters of basic type:
73 // The functions return kFALSE, if the parameter is not in the list.
74 // 2. Arrays:
75 // a) If the array size is specified (return code Bool_t), the functions return
76 // kFALSE, if the number of data elements in the list objects is not the same.
77 // b) If the array size is not specified (return code Int_t), the array is
78 // recreated with the size of the number of data elements in the list object.
79 // The functions return the number of data elements or 0, if the parameter
80 // was not found.
81 // 3. Classes:
82 // The class version is checked and a warning printed, if it is not identical
83 // with the current version (typically class version of list object higher than
84 // version in the actual parameter container). The class streamer takes care
85 // for backward compatibility. A warning is printed, if the ROOT version is
86 // different from the current version.
87 // The function returns the number of bytes in the list object or 0, if the
88 // parameter was not found in the list.
89 //
91 
94 
95 FairParamObj::FairParamObj(const Text_t* name)
96  : TNamed(name, "")
97  , paramValue(nullptr)
98  , arraySize(0)
99  , paramType("UChar_t")
100  , basicType(kFALSE)
101  , bytesPerValue(1)
102  , classVersion(-1)
103  , streamerInfo(nullptr)
104  , streamerInfoSize(0)
105 {}
106 
108  : TNamed(o)
109  , paramValue(nullptr)
110  , arraySize(o.arraySize)
111  , paramType(o.paramType)
112  , basicType(o.basicType)
113  , bytesPerValue(o.bytesPerValue)
114  , classVersion(o.classVersion)
115  , streamerInfo(nullptr)
116  , streamerInfoSize(o.streamerInfoSize)
117 {
118  paramValue = new UChar_t[arraySize];
119  memcpy(paramValue, o.getParamValue(), arraySize);
120  if (streamerInfoSize > 0) {
121  streamerInfo = new UChar_t[streamerInfoSize];
123  }
124 }
125 
126 FairParamObj::FairParamObj(const Text_t* name, Int_t value)
127  : TNamed(name, "")
128  , paramValue(nullptr)
129  , arraySize(sizeof(Int_t))
130  , paramType("Int_t")
131  , basicType(kTRUE)
132  , bytesPerValue(sizeof(Int_t))
133  , classVersion(-1)
134  , streamerInfo(nullptr)
135  , streamerInfoSize(0)
136 {
137  // Constructor for a Int_t value
138  paramValue = new UChar_t[arraySize];
139  memcpy(paramValue, &value, arraySize);
140 }
141 
142 FairParamObj::FairParamObj(const Text_t* name, Bool_t value)
143  : TNamed(name, "")
144  , paramValue(nullptr)
145  , arraySize(sizeof(Bool_t))
146  , paramType("Bool_t")
147  , basicType(kTRUE)
148  , bytesPerValue(sizeof(Bool_t))
149  , classVersion(-1)
150  , streamerInfo(nullptr)
151  , streamerInfoSize(0)
152 {
153  // Constructor for a Int_t value
154  paramValue = new UChar_t[arraySize];
155  memcpy(paramValue, &value, arraySize);
156 }
157 
158 FairParamObj::FairParamObj(const Text_t* name, UInt_t value)
159  : TNamed(name, "")
160  , paramValue(nullptr)
161  , arraySize(sizeof(UInt_t))
162  , paramType("UInt_t")
163  , basicType(kTRUE)
164  , bytesPerValue(sizeof(UInt_t))
165  , classVersion(-1)
166  , streamerInfo(nullptr)
167  , streamerInfoSize(0)
168 {
169  // Constructor for a Int_t value
170  paramValue = new UChar_t[arraySize];
171  memcpy(paramValue, &value, arraySize);
172 }
173 
174 FairParamObj::FairParamObj(const Text_t* name, Float_t value)
175  : TNamed(name, "")
176  , paramValue(nullptr)
177  , arraySize(sizeof(Float_t))
178  , paramType("Float_t")
179  , basicType(kTRUE)
180  , bytesPerValue(sizeof(Float_t))
181  , classVersion(-1)
182  , streamerInfo(nullptr)
183  , streamerInfoSize(0)
184 {
185  // Constructor for a Float_t value
186  paramValue = new UChar_t[arraySize];
187  memcpy(paramValue, &value, arraySize);
188 }
189 
190 FairParamObj::FairParamObj(const Text_t* name, Double_t value)
191  : TNamed(name, "")
192  , paramValue(nullptr)
193  , arraySize(sizeof(Double_t))
194  , paramType("Double_t")
195  , basicType(kTRUE)
196  , bytesPerValue(sizeof(Double_t))
197  , classVersion(-1)
198  , streamerInfo(nullptr)
199  , streamerInfoSize(0)
200 {
201  // Constructor for a Double_t value
202  paramValue = new UChar_t[arraySize];
203  memcpy(paramValue, &value, arraySize);
204 }
205 
206 FairParamObj::FairParamObj(const Text_t* name, const Int_t* value, const Int_t nValues)
207  : TNamed(name, "")
208  , paramValue(nullptr)
209  , arraySize(sizeof(Int_t) * nValues)
210  , paramType("Int_t")
211  , basicType(kTRUE)
212  , bytesPerValue(sizeof(Int_t))
213  , classVersion(-1)
214  , streamerInfo(nullptr)
215  , streamerInfoSize(0)
216 {
217  // Constructor for an array with nValues elements of type Int_t
218  paramValue = new UChar_t[arraySize];
219  memcpy(paramValue, value, arraySize);
220 }
221 
222 FairParamObj::FairParamObj(const Text_t* name, const UInt_t* value, const Int_t nValues)
223  : TNamed(name, "")
224  , paramValue(nullptr)
225  , arraySize(sizeof(UInt_t) * nValues)
226  , paramType("UInt_t")
227  , basicType(kTRUE)
228  , bytesPerValue(sizeof(UInt_t))
229  , classVersion(-1)
230  , streamerInfo(nullptr)
231  , streamerInfoSize(0)
232 {
233  // Constructor for an array with nValues elements of type UInt_t
234  paramValue = new UChar_t[arraySize];
235  memcpy(paramValue, value, arraySize);
236 }
237 
238 FairParamObj::FairParamObj(const Text_t* name, const Float_t* value, const Int_t nValues)
239  : TNamed(name, "")
240  , paramValue(nullptr)
241  , arraySize(sizeof(Float_t) * nValues)
242  , paramType("Float_t")
243  , basicType(kTRUE)
244  , bytesPerValue(sizeof(Float_t))
245  , classVersion(-1)
246  , streamerInfo(nullptr)
247  , streamerInfoSize(0)
248 {
249  // Constructor for an array with nValues elements of type Float_t
250  paramValue = new UChar_t[arraySize];
251  memcpy(paramValue, value, arraySize);
252 }
253 
254 FairParamObj::FairParamObj(const Text_t* name, const Double_t* value, const Int_t nValues)
255  : TNamed(name, "")
256  , paramValue(nullptr)
257  , arraySize(sizeof(Double_t) * nValues)
258  , paramType("Double_t")
259  , basicType(kTRUE)
260  , bytesPerValue(sizeof(Double_t))
261  , classVersion(-1)
262  , streamerInfo(nullptr)
263  , streamerInfoSize(0)
264 {
265  // Constructor for an array with nValues elements of type Double_t
266  paramValue = new UChar_t[arraySize];
267  memcpy(paramValue, value, arraySize);
268 }
269 
270 FairParamObj::FairParamObj(const Text_t* name, const Text_t* value)
271  : TNamed(name, "")
272  , paramValue(nullptr)
273  , arraySize(strlen(value))
274  , paramType("Text_t")
275  , basicType(kTRUE)
276  , bytesPerValue(sizeof(Char_t))
277  , classVersion(-1)
278  , streamerInfo(nullptr)
279  , streamerInfoSize(0)
280 {
281  // Constructor for a string value
282  paramValue = new UChar_t[arraySize + 1];
283  memcpy(paramValue, value, arraySize);
284 }
285 
286 FairParamObj::FairParamObj(const Text_t* name, const Char_t* value, const Int_t nValues)
287  : TNamed(name, "")
288  , paramValue(nullptr)
289  , arraySize(sizeof(Char_t) * nValues)
290  , paramType("Char_t")
291  , basicType(kTRUE)
292  , bytesPerValue(sizeof(Char_t))
293  , classVersion(-1)
294  , streamerInfo(nullptr)
295  , streamerInfoSize(0)
296 {
297  // Constructor for an array with nValues elements of type Char_t
298  paramValue = new UChar_t[arraySize];
299  memcpy(paramValue, value, arraySize);
300 }
301 
302 FairParamObj::FairParamObj(const Text_t* name, const UChar_t* value, const Int_t nValues)
303  : TNamed(name, "")
304  , paramValue(nullptr)
305  , arraySize(sizeof(UChar_t) * nValues)
306  , paramType("UChar_t")
307  , basicType(kTRUE)
308  , bytesPerValue(sizeof(UChar_t))
309  , classVersion(-1)
310  , streamerInfo(nullptr)
311  , streamerInfoSize(0)
312 {
313  paramValue = new UChar_t[arraySize];
314  memcpy(paramValue, value, arraySize);
315 }
316 
318 {
319  // Destructor
320  delete[] paramValue;
321  paramValue = 0;
322  delete[] streamerInfo;
323  streamerInfo = 0;
324 }
325 
326 void FairParamObj::setParamType(const Text_t* t)
327 {
328  // Sets the parameter type. Accepted type names are:
329  // UChar_t (default)
330  // Char_t
331  // Int_t
332  // Float_t
333  // Double_t
334  // Text_t
335  // class name
336  paramType = t;
337  if (strcmp(t, "Char_t") == 0) {
338  basicType = kTRUE;
339  bytesPerValue = sizeof(Char_t);
340  } else if (strcmp(t, "Int_t") == 0) {
341  basicType = kTRUE;
342  bytesPerValue = sizeof(Int_t);
343  } else if (strcmp(t, "Float_t") == 0) {
344  basicType = kTRUE;
345  bytesPerValue = sizeof(Float_t);
346  } else if (strcmp(t, "Double_t") == 0) {
347  basicType = kTRUE;
348  bytesPerValue = sizeof(Double_t);
349  } else if (strcmp(t, "Text_t") == 0) {
350  basicType = kTRUE;
351  bytesPerValue = sizeof(Char_t);
352  } else {
353  basicType = kFALSE;
354  bytesPerValue = 1;
355  }
356  if (basicType == kTRUE) {
357  classVersion = -1;
358  streamerInfoSize = 0;
359  streamerInfo = 0;
360  }
361 }
362 
363 UChar_t* FairParamObj::setLength(Int_t l)
364 {
365  // Sets the length of the binary array
366  delete[] paramValue;
367  arraySize = l;
368  if (l > 0) {
369  paramValue = new UChar_t[arraySize];
370  } else {
371  paramValue = 0;
372  }
373  return paramValue;
374 }
375 
376 void FairParamObj::setParamValue(UChar_t* value, const Int_t length)
377 {
378  // Sets the parameter value (the array is not copied!)
379  delete[] paramValue;
380  arraySize = length;
381  paramValue = value;
382 }
383 
385 {
386  // Sets the length of the streamer info
387  delete[] streamerInfo;
388  streamerInfoSize = l;
389  if (l > 0) {
390  streamerInfo = new UChar_t[streamerInfoSize];
391  } else {
392  streamerInfo = 0;
393  }
394  return streamerInfo;
395 }
396 
397 void FairParamObj::setStreamerInfo(UChar_t* array, const Int_t length)
398 {
399  // Sets the streamer info of ROOT classes (the array is not copied!)
400  delete[] streamerInfo;
401  streamerInfoSize = length;
402  streamerInfo = array;
403 }
404 
406 {
407  // Returns the number of values in the array, respectively 1 for classes and strings
408  Int_t n = 1;
409  if (basicType) {
410  n = arraySize / bytesPerValue;
411  }
412  return n;
413 }
414 
416 {
417  // Prints the name and type of the parameters, respectively class name and version.
418  // Prints also the numbers for an array of type Int_t, Float_t, Double_t.
419  std::cout << GetName() << ": ";
420  if (classVersion >= 0) {
421  std::cout << "\n Class Type: " << paramType.Data() << "\n Class Version: " << classVersion << std::endl;
422  } else if (strcmp(paramType, "Text_t") == 0) {
423  TString val(reinterpret_cast<Char_t*>(paramValue), arraySize);
424  val.ReplaceAll("\n", "\n ");
425  std::cout << paramType << "\n " << val.Data() << std::endl;
426  } else {
427  Int_t nParams = getNumParams();
428  if (nParams == 1) {
429  std::cout << paramType << " ";
430  } else {
431  std::cout << paramType << " array, nValues: " << nParams << "\n ";
432  }
433  if (strcmp(paramType, "Char_t") == 0) {
434  Char_t* val = reinterpret_cast<Char_t*>(paramValue);
435  printData(val, nParams);
436  } else if (strcmp(paramType, "Int_t") == 0) {
437  Int_t* val = reinterpret_cast<Int_t*>(paramValue);
438  printData(val, nParams);
439  } else if (strcmp(paramType, "Float_t") == 0) {
440  Float_t* val = reinterpret_cast<Float_t*>(paramValue);
441  printData(val, nParams);
442  } else if (strcmp(paramType, "Double_t") == 0) {
443  Double_t* val = reinterpret_cast<Double_t*>(paramValue);
444  printData(val, nParams);
445  } else {
446  std::cout << "Type: " << paramType << " Array length: " << arraySize << std::endl;
447  }
448  }
449 }
450 
451 template<class type>
452 void FairParamObj::printData(type* val, Int_t nParams)
453 {
454  Int_t i = 0, k = 0;
455  while (k < nParams) {
456  if (i == 10) {
457  std::cout << "\n ";
458  i = 0;
459  }
460  std::cout << val[k] << " ";
461  i++;
462  k++;
463  if (k > 50) {
464  std::cout << "...";
465  break;
466  }
467  }
468  std::cout << std::endl;
469 }
470 
471 //-----------------------------------------------------------------------------------
472 
474  : TObject()
475 {}
476 
478 {
479  // Destructor
480 }
481 
483 {
484  // Adds a FairParamObj object to the list
485  paramList.Add(new FairParamObj(p));
486 }
487 
488 void FairParamList::add(const Text_t* name, const Text_t* value)
489 {
490  // Adds a string parameter to the list
491  // name = name of the parameter
492  // value = string value
493  paramList.Add(new FairParamObj(name, value));
494 }
495 
496 void FairParamList::add(const Text_t* name, const Int_t value)
497 {
498  // Adds a parameter of type Int_t to the list
499  paramList.Add(new FairParamObj(name, value));
500 }
501 
502 void FairParamList::add(const Text_t* name, const Bool_t value)
503 {
504  // Adds a parameter of type Int_t to the list
505  paramList.Add(new FairParamObj(name, value));
506 }
507 
508 void FairParamList::add(const Text_t* name, const UInt_t value)
509 {
510  // Adds a parameter of type UInt_t to the list
511  paramList.Add(new FairParamObj(name, value));
512 }
513 
514 void FairParamList::add(const Text_t* name, const Float_t value)
515 {
516  // Adds a parameter of type Float_t to the list
517  paramList.Add(new FairParamObj(name, value));
518 }
519 
520 void FairParamList::add(const Text_t* name, const Double_t value)
521 {
522  // Adds a parameter of type Double_t to the list
523  paramList.Add(new FairParamObj(name, value));
524 }
525 
526 void FairParamList::add(const Text_t* name, TArrayI& value)
527 {
528  // Adds a parameter of type TArrayI to the list
529  paramList.Add(new FairParamObj(name, value.GetArray(), value.GetSize()));
530 }
531 
532 void FairParamList::add(const Text_t* name, TArrayC& value)
533 {
534  // Adds a parameter of type TArrayC to the list
535  paramList.Add(new FairParamObj(name, value.GetArray(), value.GetSize()));
536 }
537 
538 void FairParamList::add(const Text_t* name, TArrayF& value)
539 {
540  // Adds a parameter of type TArrayF to the list
541  paramList.Add(new FairParamObj(name, value.GetArray(), value.GetSize()));
542 }
543 
544 void FairParamList::add(const Text_t* name, TArrayD& value)
545 {
546  // Adds a parameter of type TArrayD to the list
547  paramList.Add(new FairParamObj(name, value.GetArray(), value.GetSize()));
548 }
549 
550 void FairParamList::add(const Text_t* name, const UChar_t* values, const Int_t nValues)
551 {
552  // Adds a binary array of size nValues to the list
553  paramList.Add(new FairParamObj(name, values, nValues));
554 }
555 
556 void FairParamList::add(const Text_t* name, const Int_t* values, const Int_t nValues)
557 {
558  // Adds an array of type Int_t and of size nValues as binary to the list
559  paramList.Add(new FairParamObj(name, values, nValues));
560 }
561 
562 void FairParamList::add(const Text_t* name, const Float_t* values, const Int_t nValues)
563 {
564  // Adds an array of type Float_t and of size nValues as binary to the list
565  paramList.Add(new FairParamObj(name, values, nValues));
566 }
567 
568 void FairParamList::add(const Text_t* name, const Double_t* values, const Int_t nValues)
569 {
570  // Adds an array of type Double_t and of size nValues as binary to the list
571  paramList.Add(new FairParamObj(name, values, nValues));
572 }
573 
574 void FairParamList::addObject(const Text_t* name, TObject* obj)
575 {
576  // Adds a TObject to the list, sets the class version and the streamer info for
577  // ROOT classes
578  if (!obj) {
579  return;
580  }
581  FairParamObj* o = new FairParamObj(name);
582  o->setParamType(obj->IsA()->GetName());
583  o->setClassVersion(obj->IsA()->GetClassVersion());
584  TFile* filesave = gFile;
585  FairParamTFile* paramFile = new FairParamTFile();
586  gFile = paramFile;
587  const Int_t bufsize = 10000;
588 
589  TBufferFile* buffer = new TBufferFile(TBuffer::kWrite, bufsize);
590 
591  buffer->SetParent(paramFile);
592  buffer->MapObject(obj);
593  obj->Streamer(*buffer);
594  Int_t len = buffer->Length();
595  Char_t* buf = new char[len];
596  memcpy(buf, buffer->Buffer(), len);
597  o->setParamValue(reinterpret_cast<UChar_t*>(buf), len);
598  TArrayC* fClassIndex = paramFile->GetClassIndex();
599  if (fClassIndex && fClassIndex->fArray[0] != 0) {
600  TIter next(gROOT->GetListOfStreamerInfo());
601  TStreamerInfo* info;
602  TList list;
603  while ((info = static_cast<TStreamerInfo*>(next()))) {
604  Int_t uid = info->GetNumber();
605  if (fClassIndex->fArray[uid]) {
606  list.Add(info);
607  }
608  }
609  if (list.GetSize() > 0) {
610  list.Sort();
611  fClassIndex->fArray[0] = 2; // to prevent adding classes in TStreamerInfo::TagFile
612 
613  TBufferFile* infoBuffer = new TBufferFile(TBuffer::kWrite, bufsize);
614 
615  infoBuffer->MapObject(&list);
616  list.Streamer(*infoBuffer);
617  Int_t infolen = infoBuffer->Length();
618  Char_t* infobuf = new char[infolen];
619  memcpy(infobuf, infoBuffer->Buffer(), infolen);
620  o->setStreamerInfo(reinterpret_cast<UChar_t*>(infobuf), infolen);
621  delete infoBuffer;
622  } else {
623  o->setStreamerInfo(0, 0);
624  }
625  }
626  fClassIndex->fArray[0] = 0;
627  delete paramFile;
628  paramList.Add(o);
629  delete buffer;
630  gFile = filesave;
631 }
632 
634 {
635  // Prints the parameter list including values
636  TIter next(&paramList);
637  FairParamObj* o;
638  while ((o = static_cast<FairParamObj*>(next())) != 0) {
639  o->print();
640  }
641 }
642 
643 Bool_t FairParamList::fill(const Text_t* name, Text_t* value, const Int_t length)
644 {
645  // Copies the data from the list object into the parameter value of type string
646  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
647  if (value == 0) {
648  return kFALSE;
649  }
650  if (o != 0 && strcmp(o->getParamType(), "Text_t") == 0) {
651  Int_t l = o->getLength();
652  if (l < length - 1) {
653  memcpy(value, reinterpret_cast<Char_t*>(o->getParamValue()), l);
654  value[l] = '\0';
655  return kTRUE;
656  } else {
657  LOG(error) << "char array too small";
658  // Error("FairParamList::fill(const Text_t*,Text_t*)","char array too small");
659  }
660  }
661  LOG(error) << "Could not find parameter " << name;
662  // Error("FairParamList::fill \nNot found: %s",name);
663  return kFALSE;
664 }
665 
666 Bool_t FairParamList::fill(const Text_t* name, UChar_t* values, const Int_t nValues)
667 {
668  // Copies the data from the list object into the parameter array of type UChar_t of size nValues.
669  // The function returns an error, if the array size of the list object is not equal
670  // to nValues.
671  if (values == 0) {
672  return kFALSE;
673  }
674  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
675  if (o != 0 && strcmp(o->getParamType(), "UChar_t") == 0) {
676  Int_t n = o->getLength();
677  if (n == nValues) {
678  memcpy(values, o->getParamValue(), n);
679  return kTRUE;
680  } else {
681  LOG(error) << "Different array sizes for parameter " << name;
682  // Error("FairParamList::fill \nDifferent array sizes for parameter %s",name);
683  return kFALSE;
684  }
685  }
686  LOG(error) << "Could not find parameter " << name;
687  // Error("FairParamList::fill \nNot found: %s",name);
688  return kFALSE;
689 }
690 
691 Bool_t FairParamList::fill(const Text_t* name, Int_t* values, const Int_t nValues)
692 {
693  // Copies the data from the list object into the parameter array of type Int_t.
694  // The function returns an error, if the array size of the list object is not equal
695  // to nValues.
696  if (values == 0) {
697  return kFALSE;
698  }
699  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
700  if (o != 0 && strcmp(o->getParamType(), "Int_t") == 0) {
701  Int_t l = o->getLength();
702  Int_t n = o->getNumParams();
703  if (n == nValues) {
704  memcpy(values, o->getParamValue(), l);
705  return kTRUE;
706  } else {
707  LOG(error) << "Different array sizes for parameter " << name;
708  // Error("FairParamList::fill \nDifferent array sizes for parameter %s",name);
709  return kFALSE;
710  }
711  }
712  LOG(error) << "Could not find parameter " << name;
713  // Error("FairParamList::fill \nNot found: %s",name);
714  return kFALSE;
715 }
716 
717 Bool_t FairParamList::fill(const Text_t* name, Bool_t* values, const Int_t nValues)
718 {
719  // Copies the data from the list object into the parameter array of type Int_t.
720  // The function returns an error, if the array size of the list object is not equal
721  // to nValues.
722  if (values == 0) {
723  return kFALSE;
724  }
725  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
726  if (o != 0 && strcmp(o->getParamType(), "Bool_t") == 0) {
727  Int_t l = o->getLength();
728  Int_t n = o->getNumParams();
729  if (n == nValues) {
730  memcpy(values, o->getParamValue(), l);
731  return kTRUE;
732  } else {
733  LOG(error) << "Different array sizes for parameter " << name;
734  // Error("FairParamList::fill \nDifferent array sizes for parameter %s",name);
735  return kFALSE;
736  }
737  }
738  LOG(error) << "Could not find parameter " << name;
739  // Error("FairParamList::fill \nNot found: %s",name);
740  return kFALSE;
741 }
742 
743 Bool_t FairParamList::fill(const Text_t* name, UInt_t* values, const Int_t nValues)
744 {
745  // Copies the data from the list object into the parameter array of type UInt_t.
746  // The function returns an error, if the array size of the list object is not equal
747  // to nValues.
748  if (values == 0) {
749  return kFALSE;
750  }
751  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
752  if (o != 0 && strcmp(o->getParamType(), "Int_t") == 0) {
753  Int_t l = o->getLength();
754  Int_t n = o->getNumParams();
755  if (n == nValues) {
756  memcpy(values, o->getParamValue(), l);
757  return kTRUE;
758  } else {
759  LOG(error) << "Different array sizes for parameter " << name;
760  // Error("FairParamList::fill \nDifferent array sizes for parameter %s",name);
761  return kFALSE;
762  }
763  }
764  LOG(error) << "Could not find parameter " << name;
765  // Error("FairParamList::fill \nNot found: %s",name);
766  return kFALSE;
767 }
768 
769 Bool_t FairParamList::fill(const Text_t* name, Float_t* values, const Int_t nValues)
770 {
771  // Copies the data from the list object into the parameter array of type Float_t.
772  // The function returns an error, if the array size of the list object is not equal
773  // to nValues.
774  if (values == 0) {
775  return kFALSE;
776  }
777  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
778  if (o != 0 && strcmp(o->getParamType(), "Float_t") == 0) {
779  Int_t l = o->getLength();
780  Int_t n = o->getNumParams();
781  if (n == nValues) {
782  memcpy(values, o->getParamValue(), l);
783  return kTRUE;
784  } else {
785  LOG(error) << "Different array sizes for parameter " << name;
786  // Error("FairParamList::fill \nDifferent array sizes for parameter %s",name);
787  return kFALSE;
788  }
789  }
790  LOG(error) << "Could not find parameter " << name;
791  // Error("FairParamList::fill \nNot found: %s",name);
792  return kFALSE;
793 }
794 
795 Bool_t FairParamList::fill(const Text_t* name, Double_t* values, const Int_t nValues)
796 {
797  // Copies the data from the list object into the parameter array of type Double_t.
798  // The function returns an error, if the array size of the list object is not equal
799  // to nValues.
800  if (values == 0) {
801  return kFALSE;
802  }
803  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
804  if (o != 0 && strcmp(o->getParamType(), "Double_t") == 0) {
805  Int_t l = o->getLength();
806  Int_t n = o->getNumParams();
807  if (n == nValues) {
808  memcpy(values, o->getParamValue(), l);
809  return kTRUE;
810  } else {
811  LOG(error) << "Different array sizes for parameter " << name;
812  // Error("FairParamList::fill \nDifferent array sizes for parameter %s",name);
813  return kFALSE;
814  }
815  }
816  LOG(error) << "Could not find parameter " << name;
817  // Error("FairParamList::fill \nNot found: %s",name);
818  return kFALSE;
819 }
820 
821 Bool_t FairParamList::fill(const Text_t* name, TArrayI* value)
822 {
823  // Copies the data from the list object into the parameter value of type TArrayI
824  // The array is resized, if the number of data is different.
825  if (value == 0) {
826  return kFALSE;
827  }
828  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
829  if (o != 0 && strcmp(o->getParamType(), "Int_t") == 0) {
830  Int_t l = o->getLength();
831  Int_t n = o->getNumParams();
832  if (value->GetSize() != n) {
833  value->Set(n);
834  }
835  memcpy(value->GetArray(), o->getParamValue(), l);
836  return kTRUE;
837  }
838  LOG(error) << "Could not find parameter " << name;
839  // Error("FairParamList::fill \nNot found: %s",name);
840  return kFALSE;
841 }
842 
843 Bool_t FairParamList::fill(const Text_t* name, TArrayC* value)
844 {
845  // Copies the data from the list object into the parameter value of type TArrayC
846  // The array is resized, if the number of data is different.
847  if (value == 0) {
848  return kFALSE;
849  }
850  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
851  if (o != 0 && strcmp(o->getParamType(), "Char_t") == 0) {
852  Int_t l = o->getLength();
853  if (value->GetSize() != l) {
854  value->Set(l);
855  }
856  memcpy(value->GetArray(), o->getParamValue(), l);
857  return kTRUE;
858  }
859  LOG(error) << "Could not find parameter " << name;
860  // Error("FairParamList::fill \nNot found: %s",name);
861  return kFALSE;
862 }
863 
864 Bool_t FairParamList::fill(const Text_t* name, TArrayF* value)
865 {
866  // Copies the data from the list object into the parameter value of type TArrayF
867  // The array is resized, if the number of data is different.
868  if (value == 0) {
869  return kFALSE;
870  }
871  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
872  if (o != 0 && strcmp(o->getParamType(), "Float_t") == 0) {
873  Int_t l = o->getLength();
874  Int_t n = o->getNumParams();
875  if (value->GetSize() != n) {
876  value->Set(n);
877  }
878  memcpy(value->GetArray(), o->getParamValue(), l);
879  return kTRUE;
880  }
881  LOG(error) << "Could not find parameter " << name;
882  // Error("FairParamList::fill \nNot found: %s",name);
883  return kFALSE;
884 }
885 
886 Bool_t FairParamList::fill(const Text_t* name, TArrayD* value)
887 {
888  // Copies the data from the list object into the parameter value of type TArrayD
889  // The array is resized, if the number of data is different.
890  if (value == 0) {
891  return kFALSE;
892  }
893  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
894  if (o != 0 && strcmp(o->getParamType(), "Double_t") == 0) {
895  Int_t l = o->getLength();
896  Int_t n = o->getNumParams();
897  if (value->GetSize() != n) {
898  value->Set(n);
899  }
900  memcpy(value->GetArray(), o->getParamValue(), l);
901  return kTRUE;
902  }
903  LOG(error) << "Could not find parameter " << name;
904  // Error("FairParamList::fill \nNot found: %s",name);
905  return kFALSE;
906 }
907 
908 Bool_t FairParamList::fillObject(const Text_t* name, TObject* obj)
909 {
910  // Fills the object obj (must exist!) via the Streamer and returns the class version.
911  // Prints a warning if the class version in the list objects differs from the actual
912  // class version.
913  if (!obj) {
914  return 0;
915  }
916  FairParamObj* o = static_cast<FairParamObj*>(paramList.FindObject(name));
917  if (o != 0 && strcmp(o->getParamType(), obj->IsA()->GetName()) == 0) {
918  if (o->getClassVersion() != obj->IsA()->GetClassVersion()) {
919  LOG(warn) << "Read Class Version = " << o->getClassVersion()
920  << " does not match actual version = " << obj->IsA()->GetClassVersion();
921  }
922  // Warning("FairParamList::fill",
923  // "\n Read Class Version = %i does not match actual version = %i",
924  // o->getClassVersion(),obj->IsA()->GetClassVersion());
925  TFile* filesave = gFile;
926  gFile = 0;
927  TBufferFile* buf = 0;
928 
929  Int_t len = o->getStreamerInfoSize();
930  if (len > 0 && o->getStreamerInfo() != 0) {
931  buf = new TBufferFile(TBuffer::kRead, len);
932  memcpy(buf->Buffer(), reinterpret_cast<Char_t*>(o->getStreamerInfo()), len);
933  buf->SetBufferOffset(0);
934  TList list;
935  buf->MapObject(&list);
936  list.Streamer(*buf);
937  delete buf;
938  TStreamerInfo* info;
939  TIter next(&list);
940  while ((info = static_cast<TStreamerInfo*>(next()))) {
941  if (info->IsA() != TStreamerInfo::Class()) {
942  LOG(warn) << "Not a TStreamerInfo object";
943  // Warning("FairParamList::fill","not a TStreamerInfo object");
944  continue;
945  }
946  info->BuildCheck();
947  }
948  list.Clear(); // this will delete all TStreamerInfo objects with kCanDelete
949  }
950  len = o->getLength();
951  buf = new TBufferFile(TBuffer::kRead, len);
952  memcpy(buf->Buffer(), reinterpret_cast<Char_t*>(o->getParamValue()), len);
953  buf->SetBufferOffset(0);
954  buf->MapObject(obj);
955  obj->Streamer(*buf);
956  delete buf;
957  gFile = filesave;
958  return len;
959  }
960  LOG(error) << "Could not find parameter " << name;
961  // Error("FairParamList::fill \nNot found: %s",name);
962  return 0;
963 }
Int_t bytesPerValue
Definition: FairParamList.h:32
void printData(type *, Int_t)
const char * getParamType()
Definition: FairParamList.h:61
UChar_t * paramValue
Definition: FairParamList.h:28
Int_t classVersion
Definition: FairParamList.h:33
void addObject(const Text_t *, TObject *)
ClassImp(FairEventBuilder)
Bool_t fillObject(const Text_t *, TObject *)
void setParamValue(UChar_t *, const Int_t)
THashTable paramList
Definition: FairParamList.h:84
FairParamObj(const Text_t *name="")
UChar_t * setLength(Int_t l)
TString paramType
Definition: FairParamList.h:30
UChar_t * getParamValue()
Definition: FairParamList.h:59
Int_t getClassVersion()
Definition: FairParamList.h:63
UChar_t * setStreamerInfoSize(Int_t)
Bool_t basicType
Definition: FairParamList.h:31
Int_t streamerInfoSize
Definition: FairParamList.h:35
Bool_t fill(const Text_t *, Text_t *, const Int_t)
Int_t getLength()
Definition: FairParamList.h:64
Int_t getNumParams()
void setParamType(const Text_t *t)
UChar_t * getStreamerInfo()
Definition: FairParamList.h:66
void setClassVersion(const Int_t v)
Definition: FairParamList.h:56
Int_t getStreamerInfoSize()
Definition: FairParamList.h:67
void setStreamerInfo(UChar_t *, const Int_t)
UChar_t * streamerInfo
Definition: FairParamList.h:34
void add(FairParamObj &)