ROOT logo
//*-- AUTHOR : Ilse Koenig
//*-- Created : 10/11/2003

//_HADES_CLASS_DESCRIPTION 
///////////////////////////////////////////////////////////////////////////////
// HGeomAsciiIo
//
// Class for geometry I/O from ASCII file
//
///////////////////////////////////////////////////////////////////////////////

#include "hgeomasciiio.h"
#include "hgeommedia.h"
#include "hgeomset.h"
#include "hgeominterface.h"
#include "hgeomhit.h"

ClassImp(HGeomAsciiIo)

HGeomAsciiIo::HGeomAsciiIo() {
  // Constructor
  file=0;
  filename="";
  filedir="";
  writable=kFALSE;
}

HGeomAsciiIo::~HGeomAsciiIo() {
  // Destructor
  close();
  if (file) {
    delete file;
    file=0;
  }
}

Bool_t HGeomAsciiIo::open(const Char_t* fname,const Text_t* status) {
  // Opens the file fname
  close();
  if (!file) file=new fstream();
  else (file->clear());
  if (!filedir.IsNull()) filename=filedir+"/"+fname;
  else filename=fname;
  filename=filename.Strip();  
  if (strcmp(status,"in")==0) {
    file->open(filename,ios::in);
    writable=kFALSE;
  } else {
    if (strcmp(status,"out")==0) {
      file->open(filename,ios::in);
      if (!isOpen()) {
        file->close();
        file->clear();
        file->open(filename,ios::out);
        writable=kTRUE;
      } else {
        file->close();
        Error("open","Output file %s exists already and will not be recreated.",
              filename.Data());
        return kFALSE;
      }
    } else Error("open","Invalid file option!");
  }
  if (file->rdbuf()->is_open()==0) {
    Error("open","Failed to open file %s",filename.Data());
    return kFALSE;
  }
  return kTRUE;
}

Bool_t HGeomAsciiIo::isOpen() {
  // Returns kTRUE, if the file is open
  if (file && file->rdbuf()->is_open()==1) return kTRUE; 
  return kFALSE;
}

Bool_t HGeomAsciiIo::isWritable() {
  // Returns kTRUE, if the file is open and writable
  if (isOpen() && writable) return kTRUE; 
  return kFALSE;
}

void HGeomAsciiIo::close() {
  // Closes the file
  if (isOpen()) { 
    file->close();
    filename="";
  }
}

void HGeomAsciiIo::print() {
  // Prints file information
  if (isOpen()) {
     if (writable) cout<<"Open output file: "<<filename<<endl;
     else cout<<"Open input file: "<<filename<<endl;
  }
  else cout<<"No file open."<<endl;
}

Bool_t HGeomAsciiIo::read(HGeomMedia* media) {
  // Reads the media from file
  if (!isOpen() || writable || media==0) return kFALSE;
  media->read(*file);
  return kTRUE;
}

Bool_t HGeomAsciiIo::read(HGeomHit* hit) {
  // Reads the hit definition from file
  if (!isOpen() || writable || hit==0) return kFALSE;
  hit->read(*file);
  return kTRUE;
}

Bool_t HGeomAsciiIo::read(HGeomSet* set,HGeomMedia* media) {
  // Reads the geometry set from file
  if (!isOpen() || writable || set==0) return kFALSE;
  set->read(*file,media);
  return kTRUE;
}

Bool_t HGeomAsciiIo::write(HGeomMedia* media) {
  // Writes the media to file
  if (!isOpen() || !writable || media==0) return kFALSE;
  media->write(*file);
  return kTRUE;
}

Bool_t HGeomAsciiIo::write(HGeomSet* set) {
  // Writes the geometry set to file
  if (!isOpen() || !writable || set==0) return kFALSE;
  set->write(*file);
  return kTRUE;
}

Bool_t HGeomAsciiIo::write(HGeomHit* hit) {
  // Writes the geometry set to file
  if (!isOpen() || !writable || hit==0) return kFALSE;
  hit->write(*file);
  return kTRUE;
}

Bool_t HGeomAsciiIo::readGeomConfig(HGeomInterface* interface) {
  // Reads the GEANT configuration file
  if (!isOpen() || writable || interface==0) return kFALSE;
  TString buf(256);
  TString simRefRun,historyDate,paramFile;  
  Int_t k=-1;
  while(!(*file).eof()) {
    buf.ReadLine(*file);
    buf=buf.Strip(buf.kBoth);
    if (!buf.IsNull() && buf(0,2)!="//" && buf(0,1)!="*") {
      if (buf.Contains(".geo")||buf.Contains("_gdb")||buf.Contains(".hit")) {
        interface->addInputFile(buf.Data());
      } else {
        if (buf.Contains(".setup")) {
          interface->addSetupFile(buf.Data());
        } else {
          if (buf.Contains("SimulRefRunDb:")) {
            k=buf.Last(' ')+1;
            if (k) simRefRun=buf(k,buf.Length()-k);
          } else {
            if (buf.Contains("HistoryDateDb:")) {
              k=buf.First(' ')+1;
              if (buf.Length()>k) {
                historyDate=buf(k,buf.Length()-k);
                historyDate=historyDate.Strip(historyDate.kBoth);
	      }
            } else {
              if (buf.Contains("ParameterFile:")) {
                k=buf.First(' ')+1;
                if (buf.Length()>k) {
                  paramFile=buf(k,buf.Length()-k);
                  paramFile=paramFile.Strip(paramFile.kBoth);
                  interface->addParamFile(paramFile.Data());
		}
              }
            }
          }
        }
      }
    }
  }
  Bool_t rc=kTRUE;
  HGeomIo* oraIo=interface->getOraInput(); 
  if (oraIo) {
    if (historyDate.Length()>0) rc=oraIo->setHistoryDate(historyDate.Data());
    if (rc&&simRefRun.Length()>0) rc=oraIo->setSimulRefRun(simRefRun.Data());
  }
  return rc;
}

Bool_t HGeomAsciiIo::readDetectorSetup(HGeomInterface* interface) {
  // Reads the detector setups, needed for create only subsets
  if (!isOpen() || writable || interface==0) return kFALSE;
  const Int_t maxbuf=256;
  Char_t buf[maxbuf];
  TString s, det;
  HGeomSet* set=0;
  Int_t maxModules=0, secNo=-1;
  Int_t* mod=0;
  const Char_t d[]=" ";
  while(!(*file).eof()) {
    (*file).getline(buf,maxbuf);
    if (strlen(buf)>=3 && buf[1]!='/') { 
      if (buf[0]=='[') {
        set=0;
        if (mod) {
          delete [] mod;
          mod=0;
        }
        s=buf;
        Ssiz_t n=s.First(']');
        det=s(1,n-1);
        det.ToLower();
        set=interface->findSet(det);
        if (!set) {
          Error("readDetectorSetup","Detector %s not found",det.Data());
          if (mod) delete [] mod;
          return kFALSE;
        }
        maxModules=set->getMaxModules();
        mod=new Int_t[maxModules];
      } else {
        if (set&&mod) {
          Char_t* ss=strtok(buf,d);
          if (ss&&strlen(ss)>3) {
            secNo=(Int_t)(ss[3]-'0')-1;
            for(Int_t i=0;i<maxModules&&mod;i++) {
              ss=strtok(NULL,d);
              if (ss) sscanf(ss,"%i",&mod[i]);
            }
            set->setModules(secNo,mod);
          }
        } else {
          if (mod) delete [] mod;
          return kFALSE;
        }
      }
    }
  }
  if (mod) delete [] mod;
  return kTRUE;
}
 hgeomasciiio.cc:1
 hgeomasciiio.cc:2
 hgeomasciiio.cc:3
 hgeomasciiio.cc:4
 hgeomasciiio.cc:5
 hgeomasciiio.cc:6
 hgeomasciiio.cc:7
 hgeomasciiio.cc:8
 hgeomasciiio.cc:9
 hgeomasciiio.cc:10
 hgeomasciiio.cc:11
 hgeomasciiio.cc:12
 hgeomasciiio.cc:13
 hgeomasciiio.cc:14
 hgeomasciiio.cc:15
 hgeomasciiio.cc:16
 hgeomasciiio.cc:17
 hgeomasciiio.cc:18
 hgeomasciiio.cc:19
 hgeomasciiio.cc:20
 hgeomasciiio.cc:21
 hgeomasciiio.cc:22
 hgeomasciiio.cc:23
 hgeomasciiio.cc:24
 hgeomasciiio.cc:25
 hgeomasciiio.cc:26
 hgeomasciiio.cc:27
 hgeomasciiio.cc:28
 hgeomasciiio.cc:29
 hgeomasciiio.cc:30
 hgeomasciiio.cc:31
 hgeomasciiio.cc:32
 hgeomasciiio.cc:33
 hgeomasciiio.cc:34
 hgeomasciiio.cc:35
 hgeomasciiio.cc:36
 hgeomasciiio.cc:37
 hgeomasciiio.cc:38
 hgeomasciiio.cc:39
 hgeomasciiio.cc:40
 hgeomasciiio.cc:41
 hgeomasciiio.cc:42
 hgeomasciiio.cc:43
 hgeomasciiio.cc:44
 hgeomasciiio.cc:45
 hgeomasciiio.cc:46
 hgeomasciiio.cc:47
 hgeomasciiio.cc:48
 hgeomasciiio.cc:49
 hgeomasciiio.cc:50
 hgeomasciiio.cc:51
 hgeomasciiio.cc:52
 hgeomasciiio.cc:53
 hgeomasciiio.cc:54
 hgeomasciiio.cc:55
 hgeomasciiio.cc:56
 hgeomasciiio.cc:57
 hgeomasciiio.cc:58
 hgeomasciiio.cc:59
 hgeomasciiio.cc:60
 hgeomasciiio.cc:61
 hgeomasciiio.cc:62
 hgeomasciiio.cc:63
 hgeomasciiio.cc:64
 hgeomasciiio.cc:65
 hgeomasciiio.cc:66
 hgeomasciiio.cc:67
 hgeomasciiio.cc:68
 hgeomasciiio.cc:69
 hgeomasciiio.cc:70
 hgeomasciiio.cc:71
 hgeomasciiio.cc:72
 hgeomasciiio.cc:73
 hgeomasciiio.cc:74
 hgeomasciiio.cc:75
 hgeomasciiio.cc:76
 hgeomasciiio.cc:77
 hgeomasciiio.cc:78
 hgeomasciiio.cc:79
 hgeomasciiio.cc:80
 hgeomasciiio.cc:81
 hgeomasciiio.cc:82
 hgeomasciiio.cc:83
 hgeomasciiio.cc:84
 hgeomasciiio.cc:85
 hgeomasciiio.cc:86
 hgeomasciiio.cc:87
 hgeomasciiio.cc:88
 hgeomasciiio.cc:89
 hgeomasciiio.cc:90
 hgeomasciiio.cc:91
 hgeomasciiio.cc:92
 hgeomasciiio.cc:93
 hgeomasciiio.cc:94
 hgeomasciiio.cc:95
 hgeomasciiio.cc:96
 hgeomasciiio.cc:97
 hgeomasciiio.cc:98
 hgeomasciiio.cc:99
 hgeomasciiio.cc:100
 hgeomasciiio.cc:101
 hgeomasciiio.cc:102
 hgeomasciiio.cc:103
 hgeomasciiio.cc:104
 hgeomasciiio.cc:105
 hgeomasciiio.cc:106
 hgeomasciiio.cc:107
 hgeomasciiio.cc:108
 hgeomasciiio.cc:109
 hgeomasciiio.cc:110
 hgeomasciiio.cc:111
 hgeomasciiio.cc:112
 hgeomasciiio.cc:113
 hgeomasciiio.cc:114
 hgeomasciiio.cc:115
 hgeomasciiio.cc:116
 hgeomasciiio.cc:117
 hgeomasciiio.cc:118
 hgeomasciiio.cc:119
 hgeomasciiio.cc:120
 hgeomasciiio.cc:121
 hgeomasciiio.cc:122
 hgeomasciiio.cc:123
 hgeomasciiio.cc:124
 hgeomasciiio.cc:125
 hgeomasciiio.cc:126
 hgeomasciiio.cc:127
 hgeomasciiio.cc:128
 hgeomasciiio.cc:129
 hgeomasciiio.cc:130
 hgeomasciiio.cc:131
 hgeomasciiio.cc:132
 hgeomasciiio.cc:133
 hgeomasciiio.cc:134
 hgeomasciiio.cc:135
 hgeomasciiio.cc:136
 hgeomasciiio.cc:137
 hgeomasciiio.cc:138
 hgeomasciiio.cc:139
 hgeomasciiio.cc:140
 hgeomasciiio.cc:141
 hgeomasciiio.cc:142
 hgeomasciiio.cc:143
 hgeomasciiio.cc:144
 hgeomasciiio.cc:145
 hgeomasciiio.cc:146
 hgeomasciiio.cc:147
 hgeomasciiio.cc:148
 hgeomasciiio.cc:149
 hgeomasciiio.cc:150
 hgeomasciiio.cc:151
 hgeomasciiio.cc:152
 hgeomasciiio.cc:153
 hgeomasciiio.cc:154
 hgeomasciiio.cc:155
 hgeomasciiio.cc:156
 hgeomasciiio.cc:157
 hgeomasciiio.cc:158
 hgeomasciiio.cc:159
 hgeomasciiio.cc:160
 hgeomasciiio.cc:161
 hgeomasciiio.cc:162
 hgeomasciiio.cc:163
 hgeomasciiio.cc:164
 hgeomasciiio.cc:165
 hgeomasciiio.cc:166
 hgeomasciiio.cc:167
 hgeomasciiio.cc:168
 hgeomasciiio.cc:169
 hgeomasciiio.cc:170
 hgeomasciiio.cc:171
 hgeomasciiio.cc:172
 hgeomasciiio.cc:173
 hgeomasciiio.cc:174
 hgeomasciiio.cc:175
 hgeomasciiio.cc:176
 hgeomasciiio.cc:177
 hgeomasciiio.cc:178
 hgeomasciiio.cc:179
 hgeomasciiio.cc:180
 hgeomasciiio.cc:181
 hgeomasciiio.cc:182
 hgeomasciiio.cc:183
 hgeomasciiio.cc:184
 hgeomasciiio.cc:185
 hgeomasciiio.cc:186
 hgeomasciiio.cc:187
 hgeomasciiio.cc:188
 hgeomasciiio.cc:189
 hgeomasciiio.cc:190
 hgeomasciiio.cc:191
 hgeomasciiio.cc:192
 hgeomasciiio.cc:193
 hgeomasciiio.cc:194
 hgeomasciiio.cc:195
 hgeomasciiio.cc:196
 hgeomasciiio.cc:197
 hgeomasciiio.cc:198
 hgeomasciiio.cc:199
 hgeomasciiio.cc:200
 hgeomasciiio.cc:201
 hgeomasciiio.cc:202
 hgeomasciiio.cc:203
 hgeomasciiio.cc:204
 hgeomasciiio.cc:205
 hgeomasciiio.cc:206
 hgeomasciiio.cc:207
 hgeomasciiio.cc:208
 hgeomasciiio.cc:209
 hgeomasciiio.cc:210
 hgeomasciiio.cc:211
 hgeomasciiio.cc:212
 hgeomasciiio.cc:213
 hgeomasciiio.cc:214
 hgeomasciiio.cc:215
 hgeomasciiio.cc:216
 hgeomasciiio.cc:217
 hgeomasciiio.cc:218
 hgeomasciiio.cc:219
 hgeomasciiio.cc:220
 hgeomasciiio.cc:221
 hgeomasciiio.cc:222
 hgeomasciiio.cc:223
 hgeomasciiio.cc:224
 hgeomasciiio.cc:225
 hgeomasciiio.cc:226
 hgeomasciiio.cc:227
 hgeomasciiio.cc:228
 hgeomasciiio.cc:229
 hgeomasciiio.cc:230
 hgeomasciiio.cc:231
 hgeomasciiio.cc:232
 hgeomasciiio.cc:233
 hgeomasciiio.cc:234
 hgeomasciiio.cc:235
 hgeomasciiio.cc:236
 hgeomasciiio.cc:237
 hgeomasciiio.cc:238
 hgeomasciiio.cc:239
 hgeomasciiio.cc:240
 hgeomasciiio.cc:241
 hgeomasciiio.cc:242
 hgeomasciiio.cc:243