00001
00002
00003
00004 #include <cstdlib>
00005 #include <iomanip>
00006 #include <iostream>
00007
00008
00009 #include "CandNtupleSR/NtpSREvent.h"
00010 #include "CandNtupleSR/NtpSRTrack.h"
00011 #include "Registry/Registry.h"
00012 #include "Util/UtilString.h"
00013
00014
00015 #include "PhysicsNtuple/DataBlock.h"
00016 #include "PhysicsNtuple/Default.h"
00017 #include "PhysicsNtuple/Record.h"
00018 #include "PhysicsNtuple/RunAlgSnarl.h"
00019
00020
00021 #include "RunAlgStore.h"
00022 #include "Interface.h"
00023
00024 using namespace std;
00025
00026
00027 Anp::Interface::Interface()
00028 :fAlgStore(new Anp::RunAlgStore()),
00029 fAlgSnarl(new Anp::RunAlgSnarl()),
00030 fBlock(new DataBlock()),
00031 fRecord(new Anp::Record()),
00032 fPrint(true),
00033 fQuiet(true),
00034 fValid(false),
00035 fNSnarlPass(0),
00036 fNSnarlFail(0),
00037 fNEvent(0),
00038 fNTrack(0),
00039 fDefault(-1.0e6),
00040 fKeys()
00041 {
00042 }
00043
00044
00045 Anp::Interface::~Interface()
00046 {
00047
00048
00049
00050 fAlgSnarl -> End(*fBlock);
00051
00052 delete fAlgStore;
00053 delete fAlgSnarl;
00054
00055 if(fPrint)
00056 {
00057 cout << "--------------------------------------------------------------------" << endl
00058 << " Printing Interface summary " << endl
00059 << "--------------------------------------------------------------------" << endl
00060 << "Number of passed snarls: " << fNSnarlPass << endl
00061 << "Number of failed snarls: " << fNSnarlFail << endl
00062 << "Number of requested events: " << fNEvent << endl
00063 << "Number of requested tracks: " << fNTrack << endl
00064 << *fBlock
00065 << "--------------------------------------------------------------------" << endl
00066 << " End of Interface summary " << endl
00067 << "--------------------------------------------------------------------" << endl;
00068 }
00069
00070 delete fBlock;
00071 delete fRecord;
00072 }
00073
00074
00075 bool Anp::Interface::FillSnarl(TObject *record)
00076 {
00077 if(!record)
00078 {
00079 ++fNSnarlFail;
00080 return false;
00081 }
00082
00083 fRecord -> Clear();
00084
00085 if(fAlgStore -> Run(*fRecord, record) && fAlgSnarl -> Run(*fRecord))
00086 {
00087 fBlock -> Add(*fRecord);
00088 ++fNSnarlPass;
00089 fValid = true;
00090 }
00091 else
00092 {
00093 fRecord -> Clear();
00094 ++fNSnarlFail;
00095 fValid = false;
00096 }
00097
00098 if(!fQuiet)
00099 {
00100 if(fValid)
00101 {
00102 cout << "Interface::FillSnarl - filled new snarl" << endl;
00103 fRecord -> Print();
00104 }
00105 else
00106 {
00107 cout << "Interface::FillSnarl - failed new snarl" << endl;
00108 }
00109 }
00110 return fValid;
00111 }
00112
00113
00114 const map<short, float> Anp::Interface::GetData(TObject *object) const
00115 {
00116
00117
00118
00119
00120
00121
00122 map<short, float> dmap;
00123
00124 if(!fValid)
00125 {
00126 cerr << "Interface::GetData - invalid current snarl state" << endl;
00127 return dmap;
00128 }
00129
00130 NtpSREvent* ntpevt = dynamic_cast<NtpSREvent *>(object);
00131 if(ntpevt)
00132 {
00133 const EventIter ievent = std::find(fRecord -> EventBeg(), fRecord -> EventEnd(), ntpevt -> index);
00134 if(ievent == fRecord -> EventEnd())
00135 {
00136 if(fPrint)
00137 {
00138 cerr << "Interface::GetData - failed to find event with index: " << ntpevt -> index << endl;
00139 }
00140 return dmap;
00141 }
00142
00143 ++fNEvent;
00144
00145 for(DataIter dit = ievent -> DataBeg(); dit != ievent -> DataEnd(); ++dit)
00146 {
00147 dmap[dit -> Key()] = dit -> Data();
00148 }
00149
00150 if(!fQuiet)
00151 {
00152 cout << "Interface::GetData - " << dmap.size()
00153 << " data elements for NtpSREvent with index: " << ntpevt -> index << endl;
00154 }
00155 }
00156
00157 NtpSRTrack* ntptrk = dynamic_cast<NtpSRTrack *>(object);
00158 if(ntptrk)
00159 {
00160 const TrackIter itrack = std::find(fRecord -> TrackBeg(), fRecord -> TrackEnd(), ntptrk -> index);
00161 if(itrack == fRecord -> TrackEnd())
00162 {
00163 if(fPrint)
00164 {
00165 cerr << "Interface::GetData - failed to find track with index: " << ntptrk -> index << endl;
00166 }
00167 return dmap;
00168 }
00169
00170 ++fNTrack;
00171
00172 for(DataIter dit = itrack -> DataBeg(); dit != itrack -> DataEnd(); ++dit)
00173 {
00174 dmap[dit -> Key()] = dit -> Data();
00175 }
00176
00177 if(!fQuiet)
00178 {
00179 cout << "Interface::GetData - " << dmap.size()
00180 << " data elements for NtpSRTrack with index: " << ntptrk -> index << endl;
00181 }
00182 }
00183
00184 if(!fQuiet)
00185 {
00186 for(map<short, float>::const_iterator dit = dmap.begin(); dit != dmap.end(); ++dit)
00187 {
00188 cout << " (key, data) = (" << dit -> first << ", " << dit -> second << ")" << endl;
00189 }
00190 }
00191
00192 return dmap;
00193 }
00194
00195
00196
00197 float Anp::Interface::GetVar(const string &vname, TObject *object) const
00198 {
00199
00200
00201
00202
00203
00204
00205 if(!fValid)
00206 {
00207 cerr << "Interface::GetVar - invalid current snarl state" << endl;
00208 return fDefault;
00209 }
00210
00211 const KeyMap::const_iterator kit = fKeys.find(vname);
00212 if(kit == fKeys.end())
00213 {
00214 cerr << "Interface::GetVar - unknown variable: " << vname << endl;
00215 return fDefault;
00216 }
00217
00218
00219
00220
00221 const map<short, float> dmap = Interface::GetData(object);
00222
00223 if(dmap.empty())
00224 {
00225 return fDefault;
00226 }
00227
00228 const map<short, float>::const_iterator dit = dmap.find(kit -> second);
00229 if(dit != dmap.end())
00230 {
00231 return dit -> second;
00232 }
00233
00234 return fDefault;
00235 }
00236
00237
00238 void Anp::Interface::Config(const Registry& reg_)
00239 {
00240
00241
00242
00243
00244 Registry reg(reg_);
00245
00246
00247
00248
00249 const char *value_path = 0;
00250 if(reg.Get("InterfaceConfigPath", value_path) && value_path)
00251 {
00252 Registry nreg(false);
00253 if(Anp::ReadRegistry(std::string(value_path), nreg, fQuiet))
00254 {
00255 reg.UnLockKeys();
00256 reg.UnLockValues();
00257 reg.Merge(nreg);
00258 reg.RemoveKey("InterfaceConfigPath");
00259 }
00260 else
00261 {
00262 cerr << "Interface::Config - failed to read Registry:\n " << value_path << endl;
00263 }
00264 }
00265
00266 Anp::Read(reg, "InterfacePrint", fPrint);
00267 Anp::Read(reg, "InterfaceQuiet", fQuiet);
00268
00269 reg.Get("InterfaceDefault", fDefault);
00270
00271
00272
00273
00274 unsigned int widthS = 0;
00275
00276 const char *value_keys = 0;
00277 if(reg.Get("InterfaceKeys", value_keys) && value_keys)
00278 {
00279 vector<string> kvec;
00280 UtilString::StringTok(kvec, string(value_keys), ", ");
00281
00282
00283
00284
00285 for(vector<string>::const_iterator kit = kvec.begin(); kit != kvec.end(); ++kit)
00286 {
00287 vector<string> pvec;
00288 UtilString::StringTok(pvec, *kit, "=");
00289
00290 if(pvec.size() != 2)
00291 {
00292 cerr << "Interface::Config - failed to parse key: " << *kit << endl;
00293 continue;
00294 }
00295
00296 const string keyS = pvec.front();
00297 const int keyI = std::atoi(pvec.back().c_str());
00298
00299 if(!(keyI > 0))
00300 {
00301 cerr << "Interface::Config - failed to parse key: " << *kit << endl;
00302 continue;
00303 }
00304
00305 if(!fKeys.insert(KeyMap::value_type(keyS, keyI)).second)
00306 {
00307 cerr << "Interface::Config - key already exists: " << *kit << endl;
00308 continue;
00309 }
00310 else
00311 {
00312 widthS = std::max<unsigned int>(widthS, keyS.size());
00313 }
00314 }
00315 }
00316
00317 if(reg.KeyExists("PrintConfig"))
00318 {
00319 cout << "Interface::Config" << endl
00320 << " Print = " << fPrint << endl
00321 << " Quiet = " << fQuiet << endl
00322 << " Default = " << fDefault << endl
00323 << " Added " << fKeys.size() << " key(s)" << endl;
00324
00325 for(KeyMap::const_iterator kit = fKeys.begin(); kit != fKeys.end(); ++kit)
00326 {
00327 cout << " " << std::setw(widthS) << std::left << kit -> first
00328 << " = " << kit -> second << endl;
00329 }
00330
00331 if(reg.KeyExists("InterfacePrintConfig"))
00332 {
00333 reg.PrettyPrint(std::cout);
00334 }
00335 }
00336
00337
00338
00339
00340 fAlgStore -> Config(reg);
00341 fAlgSnarl -> Config(reg);
00342 }