| 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  04/2002 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2000-2004 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | // | 
|---|
| 27 | //   MDataMember | 
|---|
| 28 | // | 
|---|
| 29 | //   This objects corresponds to the data member of another object. | 
|---|
| 30 | //   You can either specify the object as a string, eg "MHillas.fWidth" | 
|---|
| 31 | //   where MHillas is the name of the container in the parameterlist | 
|---|
| 32 | //   and fWidth is it's data member, or you can specify it by giving | 
|---|
| 33 | //   the pointer corresponding to the instance of your object and | 
|---|
| 34 | //   a TMethodCall pointer corresponding to the Member function returning | 
|---|
| 35 | //   the requested value. | 
|---|
| 36 | // | 
|---|
| 37 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 38 | #include "MDataMember.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include <fstream> | 
|---|
| 41 |  | 
|---|
| 42 | #include <TMethodCall.h> | 
|---|
| 43 |  | 
|---|
| 44 | #include "MLog.h" | 
|---|
| 45 | #include "MLogManip.h" | 
|---|
| 46 |  | 
|---|
| 47 | #include "MParList.h" | 
|---|
| 48 |  | 
|---|
| 49 | ClassImp(MDataMember); | 
|---|
| 50 |  | 
|---|
| 51 | using namespace std; | 
|---|
| 52 |  | 
|---|
| 53 | // -------------------------------------------------------------------------- | 
|---|
| 54 | // | 
|---|
| 55 | //  obj is a pointer to the instance of your class from which the data | 
|---|
| 56 | //  should be requested. TMethodCall (s. root dox) is a pointer | 
|---|
| 57 | //  to a TMethodCall object which should be the getter function for | 
|---|
| 58 | //  the data you want to get. | 
|---|
| 59 | // | 
|---|
| 60 | MDataMember::MDataMember(MParContainer *obj, TMethodCall *call) | 
|---|
| 61 | { | 
|---|
| 62 | fObject = obj; | 
|---|
| 63 | fCall   = call; | 
|---|
| 64 |  | 
|---|
| 65 | if (fCall->ReturnType()==TMethodCall::kLong) | 
|---|
| 66 | SetBit(kIsInt); | 
|---|
| 67 | else | 
|---|
| 68 | ResetBit(kIsInt); | 
|---|
| 69 |  | 
|---|
| 70 | fDataMember = (TString)obj->GetName() + "." + call->GetName(); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | // -------------------------------------------------------------------------- | 
|---|
| 74 | // | 
|---|
| 75 | //  obj is a pointer to the instance of your class from which the data | 
|---|
| 76 | //  should be requested. TMethodCall (s. root dox) is a pointer | 
|---|
| 77 | //  to a TMethodCall object which should be the getter function for | 
|---|
| 78 | //  the data you want to get. | 
|---|
| 79 | // | 
|---|
| 80 | MDataMember::MDataMember(MParContainer *obj, const TString call) | 
|---|
| 81 | { | 
|---|
| 82 | fObject = obj; | 
|---|
| 83 | fCall   = obj->GetterMethod(call); | 
|---|
| 84 |  | 
|---|
| 85 | if (fCall->ReturnType()==TMethodCall::kLong) | 
|---|
| 86 | SetBit(kIsInt); | 
|---|
| 87 | else | 
|---|
| 88 | ResetBit(kIsInt); | 
|---|
| 89 |  | 
|---|
| 90 | fDataMember = (TString)obj->GetName() + "." + call; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | // -------------------------------------------------------------------------- | 
|---|
| 94 | // | 
|---|
| 95 | // returns the value you requested | 
|---|
| 96 | // | 
|---|
| 97 | Double_t MDataMember::GetValue() const | 
|---|
| 98 | { | 
|---|
| 99 | if (!fCall) | 
|---|
| 100 | { | 
|---|
| 101 | *fLog << err << "No TMethodCall for " << fDataMember << " of "; | 
|---|
| 102 | *fLog << fObject->GetName() << " available... returning 0." << endl; | 
|---|
| 103 | return 0; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | switch (fCall->ReturnType()) | 
|---|
| 107 | { | 
|---|
| 108 | case TMethodCall::kLong: | 
|---|
| 109 | Long_t l; | 
|---|
| 110 | fCall->Execute(fObject, l); | 
|---|
| 111 | return (Double_t)l; | 
|---|
| 112 |  | 
|---|
| 113 | case TMethodCall::kDouble: | 
|---|
| 114 | Double_t v; | 
|---|
| 115 | fCall->Execute(fObject, v); | 
|---|
| 116 | return v; | 
|---|
| 117 |  | 
|---|
| 118 | default: | 
|---|
| 119 | *fLog << err << "DataMember " << fDataMember << " of "; | 
|---|
| 120 | *fLog << fObject->GetName() << " neither int nor float... returning 0." << endl; | 
|---|
| 121 | return 0; | 
|---|
| 122 | } | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | // -------------------------------------------------------------------------- | 
|---|
| 126 | // | 
|---|
| 127 | // If a string was given PreProcess try to resolv the object name and | 
|---|
| 128 | // tries to get it from the parlist. And also tries to resolve | 
|---|
| 129 | // the data member (variable) name you requested and tries to get a | 
|---|
| 130 | // corresponding TMethodCall from the root Dictionary. | 
|---|
| 131 | // Remark: If your Data Member is called fDataMember the corresponding | 
|---|
| 132 | //         getter Method in your class must be calles fDataMember | 
|---|
| 133 | // | 
|---|
| 134 | Bool_t MDataMember::PreProcess(const MParList *plist) | 
|---|
| 135 | { | 
|---|
| 136 | // The following change has to be checked : | 
|---|
| 137 | // In order that fCall is initialized properly | 
|---|
| 138 | // the original statement "if (fCall)" is replaced by the statement | 
|---|
| 139 | if (fCall && !fObject) | 
|---|
| 140 | return kTRUE; | 
|---|
| 141 |  | 
|---|
| 142 | TString cname(fDataMember); | 
|---|
| 143 | TString mname(fDataMember); | 
|---|
| 144 |  | 
|---|
| 145 | const char *dot = strrchr(cname, '.'); | 
|---|
| 146 |  | 
|---|
| 147 | if (dot) | 
|---|
| 148 | { | 
|---|
| 149 | const int pos = dot-cname; | 
|---|
| 150 |  | 
|---|
| 151 | cname.Remove(pos); | 
|---|
| 152 | mname.Remove(0, pos+1); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | fObject = (MParContainer*)plist->FindObject(cname); | 
|---|
| 156 | if (!fObject) | 
|---|
| 157 | { | 
|---|
| 158 | *fLog << err << "Object '" << cname << "' not in parameter list... aborting." << endl; | 
|---|
| 159 | return kFALSE; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | fCall = fObject->GetterMethod(mname); | 
|---|
| 163 |  | 
|---|
| 164 | return fCall ? kTRUE : kFALSE; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | // -------------------------------------------------------------------------- | 
|---|
| 168 | // | 
|---|
| 169 | // Returns the ready-to-save flag of the data member container | 
|---|
| 170 | // | 
|---|
| 171 | Bool_t MDataMember::IsReadyToSave() const | 
|---|
| 172 | { | 
|---|
| 173 | return IsValid() ? fObject->IsReadyToSave() : kFALSE; | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | // -------------------------------------------------------------------------- | 
|---|
| 177 | // | 
|---|
| 178 | // Print the name of the data member without an CR. | 
|---|
| 179 | // | 
|---|
| 180 | /* | 
|---|
| 181 | void MDataMember::Print(Option_t *opt) const | 
|---|
| 182 | { | 
|---|
| 183 | *fLog << fName << flush; | 
|---|
| 184 | } | 
|---|
| 185 | */ | 
|---|
| 186 |  | 
|---|
| 187 | // -------------------------------------------------------------------------- | 
|---|
| 188 | // | 
|---|
| 189 | // Builds a rule which can be used in a MDataPhrase to describe this object | 
|---|
| 190 | // | 
|---|
| 191 | TString MDataMember::GetRule() const | 
|---|
| 192 | { | 
|---|
| 193 | return fDataMember; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | // -------------------------------------------------------------------------- | 
|---|
| 197 | // | 
|---|
| 198 | // Returns the data member. | 
|---|
| 199 | // This is mainly used in MTask::AddToBranchList | 
|---|
| 200 | // | 
|---|
| 201 | TString MDataMember::GetDataMember() const | 
|---|
| 202 | { | 
|---|
| 203 | return fDataMember; | 
|---|
| 204 | } | 
|---|