| 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-2008 | 
|---|
| 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 <TMath.h> | 
|---|
| 38 |  | 
|---|
| 39 | #include "MLog.h" | 
|---|
| 40 | #include "MLogManip.h" | 
|---|
| 41 |  | 
|---|
| 42 | #include "MDataPhrase.h" | 
|---|
| 43 |  | 
|---|
| 44 | ClassImp(MDataArray); | 
|---|
| 45 |  | 
|---|
| 46 | using namespace std; | 
|---|
| 47 |  | 
|---|
| 48 | static const TString gsDefName  = "MDataArray"; | 
|---|
| 49 | static const TString gsDefTitle = "Array to store MData cntainers"; | 
|---|
| 50 |  | 
|---|
| 51 | // -------------------------------------------------------------------------- | 
|---|
| 52 | // | 
|---|
| 53 | // Default Constructor | 
|---|
| 54 | // | 
|---|
| 55 | MDataArray::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 | // Copy Constructor | 
|---|
| 67 | // | 
|---|
| 68 | MDataArray::MDataArray(MDataArray &a, const char *name, const char *title) | 
|---|
| 69 | { | 
|---|
| 70 | fName  = name  ? name  : gsDefName.Data(); | 
|---|
| 71 | fTitle = title ? title : gsDefTitle.Data(); | 
|---|
| 72 |  | 
|---|
| 73 | gROOT->GetListOfCleanups()->Add(&fList); | 
|---|
| 74 | fList.SetBit(kMustCleanup); | 
|---|
| 75 |  | 
|---|
| 76 | TIter Next(&a.fList); | 
|---|
| 77 | MData *data = NULL; | 
|---|
| 78 | while ((data=(MData*)Next())) | 
|---|
| 79 | AddEntry(data->GetRule()); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | // -------------------------------------------------------------------------- | 
|---|
| 83 | // | 
|---|
| 84 | // Add a new data rule as a new entry (MDataPhrase) | 
|---|
| 85 | // | 
|---|
| 86 | void MDataArray::AddEntry(const TString rule) | 
|---|
| 87 | { | 
|---|
| 88 | TObject *obj = new MDataPhrase(rule); | 
|---|
| 89 | obj->SetBit(kCanDelete); | 
|---|
| 90 | fList.Add(obj); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | // -------------------------------------------------------------------------- | 
|---|
| 94 | // | 
|---|
| 95 | // Add a new data as a new entry (MData). If the destructor of MDataArray | 
|---|
| 96 | // should delete the object set its bit kCanDelete | 
|---|
| 97 | // | 
|---|
| 98 | void MDataArray::AddEntry(MData *data) | 
|---|
| 99 | { | 
|---|
| 100 | data->SetBit(kMustCleanup); | 
|---|
| 101 | fList.Add(data); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | // -------------------------------------------------------------------------- | 
|---|
| 105 | // | 
|---|
| 106 | // Try to find a MData which has the same rule (GetRule()). | 
|---|
| 107 | // Be carefull: This may already fail if the rule is A*B and you are | 
|---|
| 108 | // searching for B*A - FIXME: A more intelligent comaparton (based on | 
|---|
| 109 | // IsEqual()) should be developed. | 
|---|
| 110 | // | 
|---|
| 111 | // Returns the index in the array, -1 if rule was not found. | 
|---|
| 112 | // | 
|---|
| 113 | Int_t MDataArray::FindRule(const char *rule) const | 
|---|
| 114 | { | 
|---|
| 115 | const MDataPhrase data(rule); | 
|---|
| 116 | if (!data.IsValid()) | 
|---|
| 117 | return -1; | 
|---|
| 118 |  | 
|---|
| 119 | const TString r(data.GetRule()); | 
|---|
| 120 |  | 
|---|
| 121 | TIter Next(&fList); | 
|---|
| 122 | const MData *d = NULL; | 
|---|
| 123 | while ((d=(MData*)Next())) | 
|---|
| 124 | if (d->GetRule()==r) | 
|---|
| 125 | return fList.IndexOf(d); | 
|---|
| 126 |  | 
|---|
| 127 | return -1; | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | // -------------------------------------------------------------------------- | 
|---|
| 131 | // | 
|---|
| 132 | // Return the i-th entry | 
|---|
| 133 | // | 
|---|
| 134 | MData &MDataArray::operator[](Int_t i) const | 
|---|
| 135 | { | 
|---|
| 136 | return (MData&)*((TObjArray)fList)[i]; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | // -------------------------------------------------------------------------- | 
|---|
| 140 | // | 
|---|
| 141 | // Return the data value of the i-th entry | 
|---|
| 142 | // | 
|---|
| 143 | Double_t MDataArray::operator()(Int_t i) const | 
|---|
| 144 | { | 
|---|
| 145 | return (*this)[i].GetValue(); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | // -------------------------------------------------------------------------- | 
|---|
| 149 | // | 
|---|
| 150 | // PreProcesses all members in the list | 
|---|
| 151 | // | 
|---|
| 152 | Bool_t MDataArray::PreProcess(const MParList *plist) | 
|---|
| 153 | { | 
|---|
| 154 | if (fList.GetSize()==0) | 
|---|
| 155 | { | 
|---|
| 156 | *fLog << err << "Error - No Column specified... aborting." << endl; | 
|---|
| 157 | return kFALSE; | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | TIter Next(&fList); | 
|---|
| 161 | MData *data = NULL; | 
|---|
| 162 | while ((data=(MData*)Next())) | 
|---|
| 163 | if (!data->PreProcess(plist)) | 
|---|
| 164 | return kFALSE; | 
|---|
| 165 |  | 
|---|
| 166 | return kTRUE; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | // -------------------------------------------------------------------------- | 
|---|
| 170 | // | 
|---|
| 171 | // Print the rules for all entries of the array | 
|---|
| 172 | // | 
|---|
| 173 | void MDataArray::Print(Option_t *opt) const | 
|---|
| 174 | { | 
|---|
| 175 | Int_t n=0; | 
|---|
| 176 |  | 
|---|
| 177 | const Int_t w = 1+(Int_t)TMath::Log10(fList.GetEntries()); | 
|---|
| 178 |  | 
|---|
| 179 | TIter Next(&fList); | 
|---|
| 180 | MData *data = NULL; | 
|---|
| 181 | while ((data=(MData*)Next())) | 
|---|
| 182 | { | 
|---|
| 183 | *fLog << all << " " << fName << "[" << setw(w) << n++ << "] = " << flush; | 
|---|
| 184 | data->Print(); | 
|---|
| 185 | *fLog << endl; | 
|---|
| 186 | } | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | Bool_t MDataArray::AsciiWrite(ostream &out) const | 
|---|
| 190 | { | 
|---|
| 191 | ((TObjArray)fList).R__FOR_EACH(MParContainer, AsciiWrite)(out); | 
|---|
| 192 | return kTRUE; | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | // -------------------------------------------------------------------------- | 
|---|
| 196 | // | 
|---|
| 197 | // Implementation of SavePrimitive. Used to write the call to a constructor | 
|---|
| 198 | // to a macro. In the original root implementation it is used to write | 
|---|
| 199 | // gui elements to a macro-file. | 
|---|
| 200 | // | 
|---|
| 201 | void MDataArray::StreamPrimitive(ostream &out) const | 
|---|
| 202 | { | 
|---|
| 203 | out << "   MDataArray " << GetUniqueName(); | 
|---|
| 204 |  | 
|---|
| 205 | if (fName!=gsDefName) | 
|---|
| 206 | { | 
|---|
| 207 | out << "(\"" << fName << "\""; | 
|---|
| 208 | if (fTitle!=gsDefTitle) | 
|---|
| 209 | out << ", \"" << fTitle << "\")"; | 
|---|
| 210 | } | 
|---|
| 211 | out << ";" << endl; | 
|---|
| 212 |  | 
|---|
| 213 | TIter Next(&fList); | 
|---|
| 214 | MData *data = NULL; | 
|---|
| 215 | while ((data=(MData*)Next())) | 
|---|
| 216 | out << "   " << GetUniqueName() << ".AddEntry(\"" << data->GetRule() << "\");" << endl; | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | TString MDataArray::GetRule(int i) const | 
|---|
| 220 | { | 
|---|
| 221 | return (*this)[i].GetRule(); | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | // -------------------------------------------------------------------------- | 
|---|
| 225 | // | 
|---|
| 226 | // Return the data members existing in this array in a comma-seperated list | 
|---|
| 227 | // (This is mainly used for MTask::AddToBranchList) | 
|---|
| 228 | // | 
|---|
| 229 | TString MDataArray::GetDataMember() const | 
|---|
| 230 | { | 
|---|
| 231 | TString str; | 
|---|
| 232 |  | 
|---|
| 233 | TIter Next(&fList); | 
|---|
| 234 | MData *data = NULL; | 
|---|
| 235 | while ((data=(MData*)Next())) | 
|---|
| 236 | { | 
|---|
| 237 | if (data->GetDataMember().IsNull()) | 
|---|
| 238 | continue; | 
|---|
| 239 |  | 
|---|
| 240 | str += ","; | 
|---|
| 241 | str += data->GetDataMember(); | 
|---|
| 242 | } | 
|---|
| 243 | return str; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | void MDataArray::GetData(TVector &v) const | 
|---|
| 247 | { | 
|---|
| 248 | Int_t ncols = GetNumEntries(); | 
|---|
| 249 |  | 
|---|
| 250 | v.ResizeTo(ncols); | 
|---|
| 251 |  | 
|---|
| 252 | // same as: for (int i=0; i<ncols; i++) <should be faster> | 
|---|
| 253 | while (ncols--) | 
|---|
| 254 | v(ncols) = (*this)(ncols); | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | void MDataArray::SetVariables(const TArrayD &arr) | 
|---|
| 258 | { | 
|---|
| 259 | fList.R__FOR_EACH(MData, SetVariables)(arr); | 
|---|
| 260 | } | 
|---|