Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
THaApparatus.cxx
Go to the documentation of this file.
1//*-- Author : Ole Hansen 13-May-00
2
4//
5// THaApparatus
6//
7// ABC for a generic apparatus (collection of detectors).
8// Defines a standard Decode() method that loops over all detectors
9// defined for this object.
10//
11// A Reconstruct() method has to be implemented by the derived classes.
12//
14
15#include "THaApparatus.h"
16#include "THaDetector.h"
17#include "TClass.h"
18#include "TList.h"
19
20#include <cstring>
21#ifdef WITH_DEBUG
22#include <iostream>
23#endif
24
25using namespace std;
26
27//_____________________________________________________________________________
28THaApparatus::THaApparatus( const char* name, const char* description ) :
29 THaAnalysisObject(name,description),
30 fDetectors{new TList}
31{
32 // Constructor
33
34}
35
36//_____________________________________________________________________________
37THaApparatus::THaApparatus( ) : fDetectors(nullptr)
38{
39 // only for ROOT I/O
40}
41
42//_____________________________________________________________________________
44{
45 // Destructor. Delete all detectors currently associated with this
46 // apparatus, including any that were added via AddDetector.
47
48 if (fDetectors) {
50 delete fDetectors; fDetectors=nullptr;
51 }
52}
53
54//_____________________________________________________________________________
56{
57 // Add a detector to this apparatus. This is the standard way to
58 // configure an apparatus for data analysis.
59 //
60 // The name of each detector is important as it defines the names
61 // of all related global variables, output variables, cuts, database
62 // file names and entries, etc. Consequently, duplicate detector
63 // names are not allowed. Note that some apparatuses require
64 // specific names for certain standard detectors, e.g. "s1" for the
65 // first scintillator plane in the HRS, etc.
66 //
67 // The detector object must be allocated by the caller, but will be
68 // deleted by the apparatus.
69
70 if( !pdet )
71 return -1;
72
73 if( fDetectors->FindObject(pdet->GetName()) ) {
74 if( !quiet )
75 Error("THaApparatus", "Detector with name %s already exists for this"
76 " apparatus. Not added.", pdet->GetName() );
77 // If not adding, delete the detector since it was given to us
78 // with the assumption that we will manage it
79 delete pdet;
80 return -1;
81 }
82 pdet->SetApparatus(this);
83 if( first )
84 fDetectors->AddFirst( pdet );
85 else
86 fDetectors->AddLast( pdet );
87
88 return 0;
89}
90
91//_____________________________________________________________________________
93{
94 // Default Begin() for an apparatus: Begin() all our detectors
95
96 TIter next(fDetectors);
97 while( auto* obj = static_cast<THaAnalysisObject*>(next()) ) {
98 obj->Begin(run);
99 }
100 return 0;
101}
102
103//_____________________________________________________________________________
105{
106 // Call the Clear() method for all detectors defined for this apparatus.
107
108 // No point in doing this during Init. Our own Init will call the detectors'
109 // Init() anyway, which will call the detectors' Clear() in turn
110 if( !strchr(opt,'I') ) {
111 TIter next(fDetectors);
112 while( auto* theDetector = static_cast<THaDetector*>( next() )) {
113#ifdef WITH_DEBUG
114 if( fDebug>1 ) cout << "Clearing " << theDetector->GetName()
115 << "... " << flush;
116#endif
117 theDetector->Clear(opt);
118#ifdef WITH_DEBUG
119 if( fDebug>1 ) cout << "done.\n" << flush;
120#endif
121 }
122 }
123}
124
125//_____________________________________________________________________________
127{
128 // Call the Decode() method for all detectors defined for this apparatus.
129
130 TIter next(fDetectors);
131 while( auto* theDetector = static_cast<THaDetector*>( next() )) {
132#ifdef WITH_DEBUG
133 if( fDebug>1 ) cout << "Decoding " << theDetector->GetName()
134 << "... " << flush;
135#endif
136 theDetector->Decode( evdata );
137#ifdef WITH_DEBUG
138 if( fDebug>1 ) cout << "done.\n" << flush;
139#endif
140 }
141 return 0;
142}
143
144//_____________________________________________________________________________
146{
147 // Default End() for an apparatus: End() all our detectors
148
149 TIter next(fDetectors);
150 while( auto* obj = static_cast<THaAnalysisObject*>(next()) ) {
151 obj->End(run);
152 }
153 return 0;
154}
155
156//_____________________________________________________________________________
158{
159 // Find the named detector and return a pointer to it.
160 // This is useful for specialized processing.
161
162 return dynamic_cast<THaDetector*>( fDetectors->FindObject( name ));
163}
164
165//_____________________________________________________________________________
167{
168 // Return number of detectors of the apparatus
169
170 return fDetectors->GetSize();
171}
172
173//_____________________________________________________________________________
175{
176 // Default method for initializing an apparatus.
177 // First, call default THaAnalysisObject::Init(), which
178 // - Creates the fPrefix for this apparatus.
179 // - Opens database file and reads database for this apparatus, if custom
180 // ReadDatabase() defined.
181 // - Sets up global variables for this apparatus via DefineVariables().
182 // Then, call Init() for all defined detectors in turn. Pass run_time argument.
183 // Return kOK if all detectors initialized correctly, or kInitError
184 // if any detector failed to initialize. Init() will be called for
185 // all detectors, even if one or more detectors fail.
186
187 if( THaAnalysisObject::Init(run_time) )
188 return fStatus;
189
190 TIter next(fDetectors);
191 while( TObject* obj = next() ) {
192 auto* theDetector = dynamic_cast<THaDetector*>( obj );
193 if( !theDetector ) {
194 Error( Here("Init()"), "Detector %s (\"%s\") is not a THaDetector. "
195 "Initialization of apparatus %s (\"%s\") failed.",
196 obj->GetName(), obj->GetTitle(), GetName(), GetTitle());
198 } else {
199#ifdef WITH_DEBUG
200 if( fDebug>0 ) cout << "Initializing "
201 << theDetector->GetName() << "... "
202 << flush;
203#endif
204 theDetector->Init( run_time );
205#ifdef WITH_DEBUG
206 if( fDebug>0 ) cout << "done.\n" << flush;
207#endif
208 if( !theDetector->IsOK() ) {
209 Error( Here("Init()"), "While initializing apparatus %s (\"%s\") "
210 "got error %d from detector %s (\"%s\")",
211 GetName(), GetTitle(), theDetector->Status(),
212 theDetector->GetName(), theDetector->GetTitle());
214 }
215 }
216 }
217
218 return fStatus;
219}
220
221//_____________________________________________________________________________
223{
224 // Print info about the apparatus and all its detectors
225
227 TString sopt(opt);
228 sopt.ToUpper();
229 if( sopt.Contains("DET") )
230 fDetectors->Print(opt);
231}
232
233//_____________________________________________________________________________
235{
236 // Set debug level of this apparatus as well as all its detectors
237
238 SetDebug(level);
239
240 TIter next(fDetectors);
241 while( auto* theDetector = static_cast<THaDetector*>( next() )) {
242 theDetector->SetDebug( level );
243 }
244}
245
246//_____________________________________________________________________________
int Int_t
bool Bool_t
const char Option_t
char name[80]
virtual void Print(Option_t *option, const char *wildcard, Int_t recurse=1) const
virtual Int_t GetSize() const
virtual void SetDebug(Int_t level)
virtual const char * Here(const char *) const
virtual void Print(Option_t *opt="") const
virtual void Print(Option_t *opt="") const
virtual THaDetector * GetDetector(const char *name)
virtual Int_t AddDetector(THaDetector *det, Bool_t quiet=false, Bool_t first=false)
virtual Int_t End(THaRunBase *r=nullptr)
virtual void SetDebugAll(Int_t level)
virtual ~THaApparatus()
virtual void Clear(Option_t *opt="")
Int_t GetNumDets() const
virtual Int_t Begin(THaRunBase *r=nullptr)
virtual Int_t Decode(const THaEvData &)
TList * fDetectors
virtual void SetApparatus(THaApparatus *)
TObject * FindObject(const char *name) const override
void AddLast(TObject *obj) override
void Delete(Option_t *option="") override
void AddFirst(TObject *obj) override
const char * GetName() const override
const char * GetTitle() const override
virtual void Error(const char *method, const char *msgfmt,...) const
void ToUpper()
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
STL namespace.
ClassImp(TPyArg)