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

Last change on this file since 4983 was 3572, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 5.4 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-2004
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// Return the i-th entry
89//
90MData &MDataArray::operator[](Int_t i) const
91{
92 return (MData&)*((TObjArray)fList)[i];
93}
94
95// --------------------------------------------------------------------------
96//
97// Return the data value of the i-th entry
98//
99Double_t MDataArray::operator()(Int_t i) const
100{
101 return (*this)[i].GetValue();
102}
103
104// --------------------------------------------------------------------------
105//
106// PreProcesses all members in the list
107//
108Bool_t MDataArray::PreProcess(const MParList *plist)
109{
110 if (fList.GetSize()==0)
111 {
112 *fLog << err << "Error - No Column specified... aborting." << endl;
113 return kFALSE;
114 }
115
116 TIter Next(&fList);
117 MData *data = NULL;
118 while ((data=(MData*)Next()))
119 if (!data->PreProcess(plist))
120 return kFALSE;
121
122 return kTRUE;
123}
124
125// --------------------------------------------------------------------------
126//
127// Print the rules for all entries of the array
128//
129void MDataArray::Print(Option_t *opt) const
130{
131 Int_t n=0;
132
133 TIter Next(&fList);
134 MData *data = NULL;
135 while ((data=(MData*)Next()))
136 {
137 *fLog << all << " Line " << setw(3) << n++ << ": " << flush;
138 data->Print();
139 *fLog << endl;
140 }
141}
142
143Bool_t MDataArray::AsciiWrite(ostream &out) const
144{
145 ((TObjArray)fList).ForEach(MParContainer, AsciiWrite)(out);
146 return kTRUE;
147}
148
149// --------------------------------------------------------------------------
150//
151// Implementation of SavePrimitive. Used to write the call to a constructor
152// to a macro. In the original root implementation it is used to write
153// gui elements to a macro-file.
154//
155void MDataArray::StreamPrimitive(ofstream &out) const
156{
157 out << " MDataArray " << GetUniqueName();
158
159 if (fName!=gsDefName)
160 {
161 out << "(\"" << fName << "\"";
162 if (fTitle!=gsDefTitle)
163 out << ", \"" << fTitle << "\")";
164 }
165 out << ";" << endl;
166
167 TIter Next(&fList);
168 MData *data = NULL;
169 while ((data=(MData*)Next()))
170 out << " " << GetUniqueName() << ".AddEntry(\"" << data->GetRule() << "\");" << endl;
171}
172
173TString MDataArray::GetRule(int i) const
174{
175 return (*this)[i].GetRule();
176}
177
178// --------------------------------------------------------------------------
179//
180// Return the data members existing in this array in a comma-seperated list
181// (This is mainly used for MTask::AddToBranchList)
182//
183TString MDataArray::GetDataMember() const
184{
185 TString str;
186
187 TIter Next(&fList);
188 MData *data = NULL;
189 while ((data=(MData*)Next()))
190 {
191 if (data->GetDataMember().IsNull())
192 continue;
193
194 str += ",";
195 str += data->GetDataMember();
196 }
197 return str;
198}
199
200void MDataArray::GetData(TVector &v) const
201{
202 Int_t ncols = GetNumEntries();
203
204 v.ResizeTo(ncols);
205
206 // same as: for (int i=0; i<ncols; i++) <should be faster>
207 while (ncols--)
208 v(ncols) = (*this)(ncols);
209}
210
211void MDataArray::SetVariables(const TArrayD &arr)
212{
213 fList.ForEach(MData, SetVariables)(arr);
214}
Note: See TracBrowser for help on using the repository browser.