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