ROOT logo
//*-- AUTHOR : Ilse Koenig
//*-- Modified : 26/04/2002 by Ilse Koenig

//_HADES_CLASS_DESCRIPTION 
///////////////////////////////////////////////////////////////////////////////
// HDetParRootFileIo
//
// Interface class to ROOT file for parameter input/output
// Base class for all detectors
//
// This class is derived from the common base class HDetParIo
// for all inputs/outputs (ROOT file, Oracle, Ascii file).
// It contains all common functionality for all detectors which can be
// implemeted without knowing the exact type of the detector or of the detector
// dependend parameter containers using only the base class types.
//
// The data element modulesFound stores the indices of all modules in a
// detector which are are defined in the actual analysis which can be
// initialized from the ROOT file. If a detector cannot be found in the file
// this pointer is 0. The array ic created and filled via the function
// readModules(Text_t* detectorName) which is called when the first init()
// function of a parameter container belonging to this detector is called.
//
// The data element initModules stores the  indices of all modules for which a
// container has been initialized from the ROOT file.
// 
///////////////////////////////////////////////////////////////////////////////
using namespace std;
#include "TROOT.h"  
#include "hdetparrootfileio.h"
#include "hades.h"
#include "hruntimedb.h"
#include "hparrootfileio.h"
#include "hrun.h"
#include "hspectrometer.h"
#include "hdetector.h"
#include "hparset.h"
#include "hdetgeompar.h"
#include "hgeomcompositevolume.h"
#include "TKey.h"
#include <iostream> 
#include <iomanip>

ClassImp(HDetParRootFileIo)

HDetParRootFileIo::HDetParRootFileIo(HParRootFile* f) : HDetParIo() {
  // Constructor takes a pointer to the parameter ROOT file
  pFile=f;
  modulesFound=0;    // modules found in ROOT file
  initModules=0;
  isActiv=kFALSE;
}


HDetParRootFileIo::~HDetParRootFileIo() {
  // destructor
  if (modulesFound) {
    delete modulesFound;
    modulesFound=0;
  }
  if (initModules) {
    delete initModules;
    initModules=0;
  }
}


Int_t HDetParRootFileIo::readModules(const Text_t* detName) {
  // reads the setup of the detector with given name from the ROOT file,
  // compares it with the setup of this detector in the current analysis and
  // creates and fills the data element modulesFound;
  // Sets the flag isAktiv kTRUE.
  // returns the number of modules not found;
  HDetector* setup=gHades->getSetup()->getDetector(detName);
  Int_t* set=setup->getModules();
  if (pFile) pFile->cd();
  HDetector* det=(HDetector*)gROOT->FindObject(detName);
  Int_t num=0;
  Int_t len=6*(setup->getMaxModules());
  if (det) {
    modulesFound=new TArrayI(len);
    Int_t* mod=det->getModules();
    for(Int_t m=0;m<len;m++) {
      if (set[m]!=0) {
        if (mod[m]!=0) modulesFound->AddAt(m+1,m);
        else num++;
      }
    }
    delete det;
  }
  isActiv=kTRUE;
  return num;
}


Bool_t HDetParRootFileIo::read(HParSet* pPar) {
  // generic read function for parameter containers
  Text_t* name=(Char_t*)pPar->GetName();
  Int_t version=findInputVersion(name);    
  if (version<=0) {
    pPar->setInputVersion(-1,inputNumber);
    return kFALSE;
  }
  if (pPar->getInputVersion(inputNumber)==version
      && pPar->getInputVersion(inputNumber)!=-1) return kTRUE;
  TKey* key = (TKey*)gDirectory->GetKey(name,version);
  if (key) {
    pPar->clear();
    key->Read(pPar); 
    pPar->setInputVersion(version,inputNumber);
    pPar->setChanged();
    cout<<"Container "<<pPar->GetName()<<" initialized from "<<pFile->GetName()<<endl;
    return kTRUE;
  }
  pPar->setInputVersion(-1,inputNumber);
  return kFALSE;
}


Bool_t HDetParRootFileIo::write(HDetector* p) {
  // writes the setup of a detector to the ROOT file
  // returns kFALSE if the file is not writable
  if (pFile) {
    pFile->cd();
    if (pFile->IsWritable()) {
      p->Write(((Char_t*)p->GetName()));
      return kTRUE;
    }
  }
  Error("write(HDetector*)","Output %s is not writable",pFile->GetName());
  return kFALSE;
}


Int_t HDetParRootFileIo::write(HParSet* pPar) {
  // writes a parameter container to the ROOT file and returns the new version
  // number (returns -1 if the file is not writable)
  if (pFile) {
    pFile->cd();
    if (pFile->IsWritable()) {
      Text_t* name=(Char_t*)pPar->GetName();
      pPar->Write(name);
      pPar->setChanged(kFALSE);
      gHades->getRuntimeDb()->setVersionsChanged(kTRUE);
      return getMaxVersion(name);
    }
  }
  Error("write(HDetector*)","Output %s is not writable",pFile->GetName());
  return -1;
}


Int_t HDetParRootFileIo::getMaxVersion(const Text_t* name) {
  // returns the maximum version of the container given by name in the ROOT
  // file (return -1 if not found)
  TKey* key=pFile->GetKey(name);
  if (key) return key->GetCycle();
  else return -1;
}


Int_t HDetParRootFileIo::findInputVersion(const Text_t* name) {
  // finds the input version to initialize the container given by name;
  // returns -1 if the version cannot be determined
  HParVersion* currVers=
      gHades->getRuntimeDb()->getCurrentRun()->getParVersion(name);
  Int_t v=currVers->getInputVersion(inputNumber);
  if (v>0) return v;      // predefined
  HRun* r=pFile->getRun();
  if (!r) return -1;        // run not in ROOT file
  HParVersion* vers=r->getParVersion(name);
  if (!vers) return -1;     // container not in ROOT file
  return vers->getRootVersion();
}  


TObject* HDetParRootFileIo::findContainer(const Text_t* name, Int_t vers) {
  // finds the parameter container given by its name with a special version in
  // the ROOT file (returns 0 if not successful)
  // This funtion uses internally the ROOT function FindObject(Text_t*), which
  // creates a new object. The calling function must therefore delete the
  // object after usage!
  Text_t cn[80];
  sprintf(cn,"%s;%i",name,vers);
  pFile->cd();
  TObject* p=gROOT->FindObject(cn);
  return p;
}


void HDetParRootFileIo::printInfo(const Text_t* msg) {
  // prints the module indices for which the container was initialized from
  // the ROOT file (will later go to the log file)
  Bool_t first=kTRUE;
  for(Int_t i=0;i<initModules->GetSize();i++) {
    if (initModules->At(i)) {
      if (first) {
        cout<<msg;
        cout<<pFile->GetName()<<": ";
        first=kFALSE;
      }
      cout<<(initModules->At(i)-1)<<" ";
    }
  }
  cout<<'\n';
}


Bool_t HDetParRootFileIo::read(HDetGeomPar* pPar,Int_t* set) {
  // reads and fills the container for the basic geometry parameters of the
  // detectors
  Text_t* name=(Char_t*)pPar->GetName();
  Int_t version=findInputVersion(name);    
  if (version==-1) {
    pPar->setInputVersion(-1,inputNumber);
    return kFALSE;
  }
  if (pPar->getInputVersion(inputNumber)==version) return kTRUE;
  pPar-> clear();
  HDetGeomPar* rGeom=(HDetGeomPar*)findContainer(name,version);
  if (!rGeom) return kFALSE;
  Bool_t allFound=kTRUE;
  initModules->Reset();
  pPar->copyComment(*rGeom);
  Int_t nMod=pPar->getNumModules();
  Int_t nRef=pPar->getNumRefModules();
  Int_t nComp=pPar->getNumComponents();
  for(Int_t m=0;m<nMod;m++) {
    if (set[m]) {
      HModGeomPar* pMod=pPar->getModule(m);
      HModGeomPar* rMod=rGeom->getModule(m);
      if (rMod&&allFound) {
        HGeomTransform& tp=pMod->getLabTransform();
        tp=rMod->getLabTransform();
        if (pPar->isFirstInitialization()) {
          pMod->SetName(rMod->GetName());
          pMod->setRefName(rMod->getRefName());
          const Text_t* ref=rMod->getRefName();
          pMod->setRefName(ref);
          Int_t mr=pPar->getModNumInMod(ref);
          HGeomCompositeVolume* refMod=pPar->getRefVolume(mr);
          if (refMod==0) {
            refMod=new HGeomCompositeVolume(nComp);
            refMod->SetName(ref);
            pPar->addRefVolume(refMod,mr);
          }
          pMod->setVolume(refMod);
          set[m]=0;
          initModules->AddAt(m+1,m);
        }
      } else allFound=kFALSE;
    }
  }
  if (pPar->isFirstInitialization() && allFound) {
    for(Int_t m=0;m<nRef;m++) {
      HGeomCompositeVolume* pMod=pPar->getRefVolume(m);
      HGeomCompositeVolume* rMod=rGeom->getRefVolume(m);
      if (pMod && rMod && pMod->getNumPoints()==0) {
        pMod->setShape(rMod->getShape());
        pMod->setMother(rMod->getMother());
        Int_t np=rMod->getNumPoints();
        pMod->createPoints(np);
        for(Int_t i=0;i<np;i++) pMod->setPoint(i,*(rMod->getPoint(i)));
        HGeomTransform& tp=pMod->getTransform();
        tp=rMod->getTransform();
        for(Int_t c=0;c<nComp;c++) {
          HGeomVolume* pComp=pMod->getComponent(c);
          HGeomVolume* rComp=rMod->getComponent(c);
          pComp->SetName(rComp->GetName());
          pComp->setShape(rComp->getShape());
          pComp->setMother(rComp->getMother());
          Int_t npc=rComp->getNumPoints();
          pComp->createPoints(npc);
          for(Int_t i=0;i<npc;i++) pComp->setPoint(i,*(rComp->getPoint(i)));
          HGeomTransform& tpc=pComp->getTransform();
          tpc=rComp->getTransform();
        }
      }
    }
  }
  delete rGeom;
  if (allFound) {
    pPar->setInputVersion(version,inputNumber);
    pPar->setChanged();
    pPar->setNotFirstInit();
    cout<<name<<" initialized from "<<pFile->GetName()<<endl;
  } else pPar->clear();
  return allFound;
}
 hdetparrootfileio.cc:1
 hdetparrootfileio.cc:2
 hdetparrootfileio.cc:3
 hdetparrootfileio.cc:4
 hdetparrootfileio.cc:5
 hdetparrootfileio.cc:6
 hdetparrootfileio.cc:7
 hdetparrootfileio.cc:8
 hdetparrootfileio.cc:9
 hdetparrootfileio.cc:10
 hdetparrootfileio.cc:11
 hdetparrootfileio.cc:12
 hdetparrootfileio.cc:13
 hdetparrootfileio.cc:14
 hdetparrootfileio.cc:15
 hdetparrootfileio.cc:16
 hdetparrootfileio.cc:17
 hdetparrootfileio.cc:18
 hdetparrootfileio.cc:19
 hdetparrootfileio.cc:20
 hdetparrootfileio.cc:21
 hdetparrootfileio.cc:22
 hdetparrootfileio.cc:23
 hdetparrootfileio.cc:24
 hdetparrootfileio.cc:25
 hdetparrootfileio.cc:26
 hdetparrootfileio.cc:27
 hdetparrootfileio.cc:28
 hdetparrootfileio.cc:29
 hdetparrootfileio.cc:30
 hdetparrootfileio.cc:31
 hdetparrootfileio.cc:32
 hdetparrootfileio.cc:33
 hdetparrootfileio.cc:34
 hdetparrootfileio.cc:35
 hdetparrootfileio.cc:36
 hdetparrootfileio.cc:37
 hdetparrootfileio.cc:38
 hdetparrootfileio.cc:39
 hdetparrootfileio.cc:40
 hdetparrootfileio.cc:41
 hdetparrootfileio.cc:42
 hdetparrootfileio.cc:43
 hdetparrootfileio.cc:44
 hdetparrootfileio.cc:45
 hdetparrootfileio.cc:46
 hdetparrootfileio.cc:47
 hdetparrootfileio.cc:48
 hdetparrootfileio.cc:49
 hdetparrootfileio.cc:50
 hdetparrootfileio.cc:51
 hdetparrootfileio.cc:52
 hdetparrootfileio.cc:53
 hdetparrootfileio.cc:54
 hdetparrootfileio.cc:55
 hdetparrootfileio.cc:56
 hdetparrootfileio.cc:57
 hdetparrootfileio.cc:58
 hdetparrootfileio.cc:59
 hdetparrootfileio.cc:60
 hdetparrootfileio.cc:61
 hdetparrootfileio.cc:62
 hdetparrootfileio.cc:63
 hdetparrootfileio.cc:64
 hdetparrootfileio.cc:65
 hdetparrootfileio.cc:66
 hdetparrootfileio.cc:67
 hdetparrootfileio.cc:68
 hdetparrootfileio.cc:69
 hdetparrootfileio.cc:70
 hdetparrootfileio.cc:71
 hdetparrootfileio.cc:72
 hdetparrootfileio.cc:73
 hdetparrootfileio.cc:74
 hdetparrootfileio.cc:75
 hdetparrootfileio.cc:76
 hdetparrootfileio.cc:77
 hdetparrootfileio.cc:78
 hdetparrootfileio.cc:79
 hdetparrootfileio.cc:80
 hdetparrootfileio.cc:81
 hdetparrootfileio.cc:82
 hdetparrootfileio.cc:83
 hdetparrootfileio.cc:84
 hdetparrootfileio.cc:85
 hdetparrootfileio.cc:86
 hdetparrootfileio.cc:87
 hdetparrootfileio.cc:88
 hdetparrootfileio.cc:89
 hdetparrootfileio.cc:90
 hdetparrootfileio.cc:91
 hdetparrootfileio.cc:92
 hdetparrootfileio.cc:93
 hdetparrootfileio.cc:94
 hdetparrootfileio.cc:95
 hdetparrootfileio.cc:96
 hdetparrootfileio.cc:97
 hdetparrootfileio.cc:98
 hdetparrootfileio.cc:99
 hdetparrootfileio.cc:100
 hdetparrootfileio.cc:101
 hdetparrootfileio.cc:102
 hdetparrootfileio.cc:103
 hdetparrootfileio.cc:104
 hdetparrootfileio.cc:105
 hdetparrootfileio.cc:106
 hdetparrootfileio.cc:107
 hdetparrootfileio.cc:108
 hdetparrootfileio.cc:109
 hdetparrootfileio.cc:110
 hdetparrootfileio.cc:111
 hdetparrootfileio.cc:112
 hdetparrootfileio.cc:113
 hdetparrootfileio.cc:114
 hdetparrootfileio.cc:115
 hdetparrootfileio.cc:116
 hdetparrootfileio.cc:117
 hdetparrootfileio.cc:118
 hdetparrootfileio.cc:119
 hdetparrootfileio.cc:120
 hdetparrootfileio.cc:121
 hdetparrootfileio.cc:122
 hdetparrootfileio.cc:123
 hdetparrootfileio.cc:124
 hdetparrootfileio.cc:125
 hdetparrootfileio.cc:126
 hdetparrootfileio.cc:127
 hdetparrootfileio.cc:128
 hdetparrootfileio.cc:129
 hdetparrootfileio.cc:130
 hdetparrootfileio.cc:131
 hdetparrootfileio.cc:132
 hdetparrootfileio.cc:133
 hdetparrootfileio.cc:134
 hdetparrootfileio.cc:135
 hdetparrootfileio.cc:136
 hdetparrootfileio.cc:137
 hdetparrootfileio.cc:138
 hdetparrootfileio.cc:139
 hdetparrootfileio.cc:140
 hdetparrootfileio.cc:141
 hdetparrootfileio.cc:142
 hdetparrootfileio.cc:143
 hdetparrootfileio.cc:144
 hdetparrootfileio.cc:145
 hdetparrootfileio.cc:146
 hdetparrootfileio.cc:147
 hdetparrootfileio.cc:148
 hdetparrootfileio.cc:149
 hdetparrootfileio.cc:150
 hdetparrootfileio.cc:151
 hdetparrootfileio.cc:152
 hdetparrootfileio.cc:153
 hdetparrootfileio.cc:154
 hdetparrootfileio.cc:155
 hdetparrootfileio.cc:156
 hdetparrootfileio.cc:157
 hdetparrootfileio.cc:158
 hdetparrootfileio.cc:159
 hdetparrootfileio.cc:160
 hdetparrootfileio.cc:161
 hdetparrootfileio.cc:162
 hdetparrootfileio.cc:163
 hdetparrootfileio.cc:164
 hdetparrootfileio.cc:165
 hdetparrootfileio.cc:166
 hdetparrootfileio.cc:167
 hdetparrootfileio.cc:168
 hdetparrootfileio.cc:169
 hdetparrootfileio.cc:170
 hdetparrootfileio.cc:171
 hdetparrootfileio.cc:172
 hdetparrootfileio.cc:173
 hdetparrootfileio.cc:174
 hdetparrootfileio.cc:175
 hdetparrootfileio.cc:176
 hdetparrootfileio.cc:177
 hdetparrootfileio.cc:178
 hdetparrootfileio.cc:179
 hdetparrootfileio.cc:180
 hdetparrootfileio.cc:181
 hdetparrootfileio.cc:182
 hdetparrootfileio.cc:183
 hdetparrootfileio.cc:184
 hdetparrootfileio.cc:185
 hdetparrootfileio.cc:186
 hdetparrootfileio.cc:187
 hdetparrootfileio.cc:188
 hdetparrootfileio.cc:189
 hdetparrootfileio.cc:190
 hdetparrootfileio.cc:191
 hdetparrootfileio.cc:192
 hdetparrootfileio.cc:193
 hdetparrootfileio.cc:194
 hdetparrootfileio.cc:195
 hdetparrootfileio.cc:196
 hdetparrootfileio.cc:197
 hdetparrootfileio.cc:198
 hdetparrootfileio.cc:199
 hdetparrootfileio.cc:200
 hdetparrootfileio.cc:201
 hdetparrootfileio.cc:202
 hdetparrootfileio.cc:203
 hdetparrootfileio.cc:204
 hdetparrootfileio.cc:205
 hdetparrootfileio.cc:206
 hdetparrootfileio.cc:207
 hdetparrootfileio.cc:208
 hdetparrootfileio.cc:209
 hdetparrootfileio.cc:210
 hdetparrootfileio.cc:211
 hdetparrootfileio.cc:212
 hdetparrootfileio.cc:213
 hdetparrootfileio.cc:214
 hdetparrootfileio.cc:215
 hdetparrootfileio.cc:216
 hdetparrootfileio.cc:217
 hdetparrootfileio.cc:218
 hdetparrootfileio.cc:219
 hdetparrootfileio.cc:220
 hdetparrootfileio.cc:221
 hdetparrootfileio.cc:222
 hdetparrootfileio.cc:223
 hdetparrootfileio.cc:224
 hdetparrootfileio.cc:225
 hdetparrootfileio.cc:226
 hdetparrootfileio.cc:227
 hdetparrootfileio.cc:228
 hdetparrootfileio.cc:229
 hdetparrootfileio.cc:230
 hdetparrootfileio.cc:231
 hdetparrootfileio.cc:232
 hdetparrootfileio.cc:233
 hdetparrootfileio.cc:234
 hdetparrootfileio.cc:235
 hdetparrootfileio.cc:236
 hdetparrootfileio.cc:237
 hdetparrootfileio.cc:238
 hdetparrootfileio.cc:239
 hdetparrootfileio.cc:240
 hdetparrootfileio.cc:241
 hdetparrootfileio.cc:242
 hdetparrootfileio.cc:243
 hdetparrootfileio.cc:244
 hdetparrootfileio.cc:245
 hdetparrootfileio.cc:246
 hdetparrootfileio.cc:247
 hdetparrootfileio.cc:248
 hdetparrootfileio.cc:249
 hdetparrootfileio.cc:250
 hdetparrootfileio.cc:251
 hdetparrootfileio.cc:252
 hdetparrootfileio.cc:253
 hdetparrootfileio.cc:254
 hdetparrootfileio.cc:255
 hdetparrootfileio.cc:256
 hdetparrootfileio.cc:257
 hdetparrootfileio.cc:258
 hdetparrootfileio.cc:259
 hdetparrootfileio.cc:260
 hdetparrootfileio.cc:261
 hdetparrootfileio.cc:262
 hdetparrootfileio.cc:263
 hdetparrootfileio.cc:264
 hdetparrootfileio.cc:265
 hdetparrootfileio.cc:266
 hdetparrootfileio.cc:267
 hdetparrootfileio.cc:268
 hdetparrootfileio.cc:269
 hdetparrootfileio.cc:270
 hdetparrootfileio.cc:271
 hdetparrootfileio.cc:272
 hdetparrootfileio.cc:273
 hdetparrootfileio.cc:274
 hdetparrootfileio.cc:275
 hdetparrootfileio.cc:276
 hdetparrootfileio.cc:277
 hdetparrootfileio.cc:278
 hdetparrootfileio.cc:279
 hdetparrootfileio.cc:280
 hdetparrootfileio.cc:281
 hdetparrootfileio.cc:282
 hdetparrootfileio.cc:283
 hdetparrootfileio.cc:284
 hdetparrootfileio.cc:285
 hdetparrootfileio.cc:286
 hdetparrootfileio.cc:287
 hdetparrootfileio.cc:288
 hdetparrootfileio.cc:289