source: trunk/MagicSoft/Mars/mdata/MDataArray.cc@ 6963

Last change on this file since 6963 was 5692, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 6.2 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 08/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2005
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MDataArray
28//
29// An Array of MData derived classes.
30// It can be used, eg, in MHMatrix for description of the columns.
31//
32/////////////////////////////////////////////////////////////////////////////
33#include "MDataArray.h"
34
35#include <fstream>
36
37#include <TVector.h>
38
39#include "MLog.h"
40#include "MLogManip.h"
41
42#include "MDataChain.h"
43
44ClassImp(MDataArray);
45
46using namespace std;
47
48static const TString gsDefName = "MDataArray";
49static const TString gsDefTitle = "Array to store MData cntainers";
50
51// --------------------------------------------------------------------------
52//
53// Constructor
54//
55MDataArray::MDataArray(const char *name, const char *title)
56{
57 fName = name ? name : gsDefName.Data();
58 fTitle = title ? title : gsDefTitle.Data();
59
60 gROOT->GetListOfCleanups()->Add(&fList);
61 fList.SetBit(kMustCleanup);
62}
63
64// --------------------------------------------------------------------------
65//
66// Add a new data rule as a new entry (MDataChain)
67//
68void MDataArray::AddEntry(const TString rule)
69{
70 TObject *obj = new MDataChain(rule);
71 obj->SetBit(kCanDelete);
72 fList.Add(obj);
73}
74
75// --------------------------------------------------------------------------
76//
77// Add a new data as a new entry (MData). If the destructor of MDataArray
78// should delete the object set its bit kCanDelete
79//
80void MDataArray::AddEntry(MData *data)
81{
82 data->SetBit(kMustCleanup);
83 fList.Add(data);
84}
85
86// --------------------------------------------------------------------------
87//
88// Try to find a MData which has the same rule (GetRule()).
89// Be carefull: This may already fail if the rule is A*B and you are
90// searching for B*A - FIXME: A more intelligent comaparton (based on
91// IsEqual()) should be developed.
92//
93// Returns the index in the array, -1 if rule was not found.
94//
95Int_t MDataArray::FindRule(const char *rule) const
96{
97 const MDataChain data(rule);
98 if (!data.IsValid())
99 return -1;
100
101 const TString r(data.GetRule());
102
103 TIter Next(&fList);
104 const MData *d = NULL;
105 while ((d=(MData*)Next()))
106 if (d->GetRule()==r)
107 return fList.IndexOf(d);
108
109 return -1;
110}
111
112// --------------------------------------------------------------------------
113//
114// Return the i-th entry
115//
116MData &MDataArray::operator[](Int_t i) const
117{
118 return (MData&)*((TObjArray)fList)[i];
119}
120
121// --------------------------------------------------------------------------
122//
123// Return the data value of the i-th entry
124//
125Double_t MDataArray::operator()(Int_t i) const
126{
127 return (*this)[i].GetValue();
128}
129
130// --------------------------------------------------------------------------
131//
132// PreProcesses all members in the list
133//
134Bool_t MDataArray::PreProcess(const MParList *plist)
135{
136 if (fList.GetSize()==0)
137 {
138 *fLog << err << "Error - No Column specified... aborting." << endl;
139 return kFALSE;
140 }
141
142 TIter Next(&fList);
143 MData *data = NULL;
144 while ((data=(MData*)Next()))
145 if (!data->PreProcess(plist))
146 return kFALSE;
147
148 return kTRUE;
149}
150
151// --------------------------------------------------------------------------
152//
153// Print the rules for all entries of the array
154//
155void MDataArray::Print(Option_t *opt) const
156{
157 Int_t n=0;
158
159 const Int_t w = 1+(Int_t)TMath::Log10(fList.GetEntries());
160
161 TIter Next(&fList);
162 MData *data = NULL;
163 while ((data=(MData*)Next()))
164 {
165 *fLog << all << " " << fName << "[" << setw(w) << n++ << "] = " << flush;
166 data->Print();
167 *fLog << endl;
168 }
169}
170
171Bool_t MDataArray::AsciiWrite(ostream &out) const
172{
173 ((TObjArray)fList).ForEach(MParContainer, AsciiWrite)(out);
174 return kTRUE;
175}
176
177// --------------------------------------------------------------------------
178//
179// Implementation of SavePrimitive. Used to write the call to a constructor
180// to a macro. In the original root implementation it is used to write
181// gui elements to a macro-file.
182//
183void MDataArray::StreamPrimitive(ofstream &out) const
184{
185 out << " MDataArray " << GetUniqueName();
186
187 if (fName!=gsDefName)
188 {
189 out << "(\"" << fName << "\"";
190 if (fTitle!=gsDefTitle)
191 out << ", \"" << fTitle << "\")";
192 }
193 out << ";" << endl;
194
195 TIter Next(&fList);
196 MData *data = NULL;
197 while ((data=(MData*)Next()))
198 out << " " << GetUniqueName() << ".AddEntry(\"" << data->GetRule() << "\");" << endl;
199}
200
201TString MDataArray::GetRule(int i) const
202{
203 return (*this)[i].GetRule();
204}
205
206// --------------------------------------------------------------------------
207//
208// Return the data members existing in this array in a comma-seperated list
209// (This is mainly used for MTask::AddToBranchList)
210//
211TString MDataArray::GetDataMember() const
212{
213 TString str;
214
215 TIter Next(&fList);
216 MData *data = NULL;
217 while ((data=(MData*)Next()))
218 {
219 if (data->GetDataMember().IsNull())
220 continue;
221
222 str += ",";
223 str += data->GetDataMember();
224 }
225 return str;
226}
227
228void MDataArray::GetData(TVector &v) const
229{
230 Int_t ncols = GetNumEntries();
231
232 v.ResizeTo(ncols);
233
234 // same as: for (int i=0; i<ncols; i++) <should be faster>
235 while (ncols--)
236 v(ncols) = (*this)(ncols);
237}
238
239void MDataArray::SetVariables(const TArrayD &arr)
240{
241 fList.ForEach(MData, SetVariables)(arr);
242}
Note: See TracBrowser for help on using the repository browser.