#ifndef __HCATEGORYMANAGER_H__ #define __HCATEGORYMANAGER_H__ #include "hades.h" #include "hrecevent.h" #include "hcategory.h" #include "hmatrixcategory.h" #include "hlocation.h" #include "htool.h" #include "TObject.h" #include "TString.h" class HCategoryManager: public TObject { private: static HLocation loc; //! public: HCategoryManager () {;} ~HCategoryManager () {;} static HCategory *getCategory(Short_t num,Int_t warn = 0, TString name = ""); static HCategory *addCategory(Short_t num,TString name = "",Int_t size = 1000,TString partev = "", Bool_t dynamicsize = kFALSE); template static T* getObject(T* pout,Short_t num = -1, Int_t index = -1,Bool_t silent=kFALSE) { // returns object with index index from category with number num. // In error case return 0. HCategory *cat = 0; pout = 0; if (index < 0 || num < 0) { return pout; } HRecEvent *ev = (HRecEvent*) gHades->getCurrentEvent (); if (ev) { cat = ev->getCategory(num); if (!cat) { if(!silent) ::Error("getObject()", "Cannot get category cat num = %i", num); return pout; } else { pout = (T*) cat->getObject(index); if (!pout) { if(!silent) ::Error("getObject()", "ZERO pointer retrieved for object!"); return pout; } } } else { ::Error("getObject()", "ZERO pointer retrieved for event!"); return pout; } return pout; } template static T* getObject(T* pout,HCategory* cat = 0, Int_t index = -1,Bool_t silent=kFALSE) { // returns object with index index from category cat. // In error case return 0. pout = 0; if (index < 0 || cat == 0) { return pout; } pout = (T*) cat->getObject(index); if (!pout) { if(!silent) ::Error("getObject()", "ZERO pointer retrieved for object!"); return pout; } return pout; } template static T* newObject(T* pout,Short_t num , Int_t& index) { // returns new object with index index from category with number num. // In error case return 0. HCategory *cat = 0; pout = 0; if (num < 0) { return pout; } HRecEvent *ev = (HRecEvent*) gHades->getCurrentEvent (); if (ev) { cat = ev->getCategory(num); if (!cat) { ::Error("newObject()", "Cannot get category cat num = %i", num); return pout; } else { pout = (T*) cat->getNewSlot(loc,&index); if (!pout) { ::Error("newObject()", "ZERO pointer retrieved for object!"); return pout; } pout = new (pout) T; } } else { ::Error("newObject()", "ZERO pointer retrieved for event!"); return pout; } return pout; } template static T* newObject(T* pout,HCategory* cat, Int_t& index) { // returns new object with index index from category cat. // In error case return 0. pout = 0; if (cat == 0) { return pout; } pout = (T*) cat->getNewSlot(loc,&index); if (!pout) { ::Error("newObject()", "ZERO pointer retrieved for object!"); return pout; } pout = new (pout) T; return pout; } template static T* newObject(T* pout,const T* pin,Short_t num , Int_t& index) { // returns new object with index index from category with number num. // the new object is created via copy constructor // In error case return 0. HCategory *cat = 0; pout = 0; if (num < 0) { return pout; } HRecEvent *ev = (HRecEvent*) gHades->getCurrentEvent (); if (ev) { cat = ev->getCategory(num); if (!cat) { ::Error("newObject()", "Cannot get category cat num = %i", num); return pout; } else { pout = (T*) cat->getNewSlot(loc,&index); if (!pout) { ::Error("newObject()", "ZERO pointer retrieved for object!"); return pout; } pout = new (pout) T(*pin); } } else { ::Error("newObject()", "ZERO pointer retrieved for event!"); return pout; } return pout; } template static T* newObjectCopy(T* pout,T* pin,HCategory* cat, Int_t& index) { // returns new object with index index from category cat. // created via copy contructor! // In error case return 0. pout = 0; if (cat == 0) { return pout; } pout = (T*) cat->getNewSlot(loc,&index); if (!pout) { ::Error("newObject()", "ZERO pointer retrieved for object!"); return pout; } pout = new (pout) T(*pin); return pout; } template static T* newSlot(T* pout,Short_t num , Int_t& index) { // returns new object pointer (no object yet) with index index from category with number num. // In error case return 0. HCategory *cat = 0; pout = 0; if (num < 0) { return pout; } HRecEvent *ev = (HRecEvent*) gHades->getCurrentEvent (); if (ev) { cat = ev->getCategory(num); if (!cat) { ::Error("getObject()", "Cannot get category cat num = %i", num); return pout; } else { pout = (T*) cat->getNewSlot(loc,&index); if (!pout) { ::Error("getObject()", "ZERO pointer retrieved for object!"); return pout; } return pout; } } } template static T* newSlot(T* pout,HCategory* cat , Int_t& index) { // returns new object pointer (no object yet) with index index from category cat. // In error case return 0. pout = 0; if (cat == 0) { return pout; } pout = (T*) cat->getNewSlot(loc,&index); if (!pout) { ::Error("newSlot()", "ZERO pointer retrieved for object!"); return pout; } return pout; } template static Bool_t copyCategory(HCategory* catIn , HCategory* catOut,T* obj) { // copy all objects for catIn to catOut if (catIn == 0 || catOut == 0) { return kFALSE; } Int_t version = 0 ; TString cname = catIn->ClassName(); if(cname == "HLinearCategory") version = 1; if(cname == "HMatrixCategory") version = 2; Int_t n = catIn->getEntries(); Int_t index = 0; if(version == 1){ T* hit1 = 0; T* hit2 = 0; for(Int_t i = 0; i < n; i++){ hit1 = HCategoryManager::getObject (hit1,catIn,i); hit2 = HCategoryManager::newObjectCopy(hit2,hit1,catOut,index); } } else if (version == 2){ HMatrixCategory* catI =(HMatrixCategory*) catIn; HMatrixCategory* catO =(HMatrixCategory*) catOut; TArrayI* s = catI->getSizes(); HIndexTable* t = catI->getIndexTable(); HPairListI* tab = t->getCompactTable(); Int_t nd = s->GetSize(); HLocation loc; if(nd == 1) loc.set(1,0); if(nd == 2) loc.set(2,0,0); if(nd == 3) loc.set(3,0,0,0); if(nd == 4) loc.set(4,0,0,0,0); if(nd == 5) loc.set(5,0,0,0,0,0); T* hit1 = 0; T* hit2 = 0; n = tab->getN(); Int_t iD[5]; for(Int_t i = 0; i < n; i++){ UInt_t linaddr = tab->getIndex1(i); UInt_t ind = tab->getIndex2(i); if (nd == 1) HTool::getDimIndex(linaddr,iD[0],iD[1],iD[2],iD[3],iD[4], s->At(0)); else if(nd == 2) HTool::getDimIndex(linaddr,iD[0],iD[1],iD[2],iD[3],iD[4], s->At(0),s->At(1)); else if(nd == 3) HTool::getDimIndex(linaddr,iD[0],iD[1],iD[2],iD[3],iD[4], s->At(0),s->At(1),s->At(2)); else if(nd == 4) HTool::getDimIndex(linaddr,iD[0],iD[1],iD[2],iD[3],iD[4], s->At(0),s->At(1),s->At(2),s->At(3)); else if(nd == 5) HTool::getDimIndex(linaddr,iD[0],iD[1],iD[2],iD[3],iD[4], s->At(0),s->At(1),s->At(2),s->At(3),s->At(4)); for(Int_t j = 0; j < nd; j++) loc[j] = iD[j]; hit1 = HCategoryManager::getObject(hit1,catIn,ind); hit2 =(T*) catO->getSlot(loc,&index); new (hit2) T(*hit1); } } else { ::Error("copyCategory()", "Unsupported category type! Supports HLiniearCategory and HMatrixCategory only."); } return kTRUE; } ClassDef(HCategoryManager, 0) // A simple helper object }; #endif // __HCATEGORYMANAGER_H__