ROOTANA
TDataContainer.hxx
Go to the documentation of this file.
1 #ifndef TDataContainer_hxx_seen
2 #define TDataContainer_hxx_seen
3 
4 #include <stdio.h>
5 #include <vector>
6 #include <exception>
7 
8 #include "TMidasEvent.h"
9 #include "TGenericData.hxx"
10 #include <typeinfo>
11 
12 ///
13 class failed_midas_bank_cast: public std::exception
14 {
15  virtual const char* what() const throw()
16  {
17  return "Incorrect bank cast";
18  }
19 };
20 
21 
22 /// This class is what will get passed back to users for each midas event.
23 /// It will contain a pointer to the midas event, which the user can access.
24 /// It will also contain routines that the user can call to access decoded
25 /// versions of the midas banks.
26 /// Generally the decoded information will be cleaned out at the end of each
27 /// event; but there will be options to allow this information to persist.
29 {
30 
31 
32 public:
33 
36  {}
37 
38  TDataContainer(const TDataContainer &dataContainer);
39 
41 
42  /// Get the MIDAS data for this event, in TMidasEvent format
43  TMidasEvent& GetMidasData() const;
44  TMidasEvent& GetMidasEvent() const{ return GetMidasData(); }
45 
46 
47  /// Add a templated function that returns event data in the format that we want.
48  template <typename T> T* GetEventData(const char* name){
49 
50  // Try to find a cached version of this bank
51  for(unsigned int ibank = 0; ibank < fEventDataList.size(); ibank++){
52  if(fEventDataList[ibank]->GetName().compare(name) == 0){
53  // Found bank. Now check it has correct type.
54  T* cast_bank = dynamic_cast<T*>(fEventDataList[ibank]);
55  // Throw exception if cached bank is of different type.
56  if(!cast_bank){
57  std::cout << "TMidasEvent::GetMidasBank: ERROR: you requested bank with name=" << name << std::endl
58  << "A cached version of this bank (of type " << typeid(fEventDataList[ibank]).name()
59  << ") already exists in event; cannot create bank class of new type "
60  << typeid(T).name()<<std::endl;
61  throw failed_midas_bank_cast();
62  }
63  return cast_bank;
64  }
65 
66  }
67 
68  void *ptr;
69  int bklen,bktype;
70  int status = GetMidasData().FindBank(name, &bklen, &bktype, &ptr);
71 
72  /// If we couldn't find bank, return null.
73  if(status == 0) return 0;
74 
75  T *bank = new T(bklen,bktype,name, ptr);
76 
77  // Cache a version of this bank...
78  fEventDataList.push_back(bank);
79 
80  return bank;
81 
82  }
83 
84 
85  /// Method to clean up the current events list of event data.
86  /// Generally this will delete all the existing information,
87  /// but this behaviour may be modified in some cases.
88  void CleanupEvent();
89 
90  /// This is the ugly function where we de-reference to get pointer for a TMidasEvent (ugly!).
91  /// In this case TDataContainer does not own the memory referenced by fMidasEventPointer.
92  void SetMidasEventPointer(TMidasEvent& event);
93 
94 private:
95 
96  /// Pointer to the TMidasEvent;
97  /// In some cases we own the memory referenced by pointer; other cases not
99 
100  /// Do we own the memory pointed to by TMidasEvent pointer?
102 
103 
104  /// For the moment make empty assign operator
106 
107  /// This is the list of banks associated with this event.
108  /// The event owns these banks and needs to take care of
109  /// deleting them.
110  std::vector<TGenericData*> fEventDataList;
111 
112 
113 };
114 
115 
116 #endif
TMidasEvent * fMidasEventPointer
void SetMidasEventPointer(TMidasEvent &event)
TDataContainer & operator=(const TDataContainer &event)
For the moment make empty assign operator.
T * GetEventData(const char *name)
Add a templated function that returns event data in the format that we want.
std::vector< TGenericData * > fEventDataList
TMidasEvent & GetMidasEvent() const
bool fOwnMidasEventMemory
Do we own the memory pointed to by TMidasEvent pointer?
TMidasEvent & GetMidasData() const
Get the MIDAS data for this event, in TMidasEvent format.
MIDAS event.
Definition: TMidasEvent.h:22
int FindBank(const char *bankName, int *bankLength, int *bankType, void **bankPtr) const
virtual const char * what() const