ROOT logo
//*-- AUTHOR : J. Kempter
//*-- Modified : 17/01/2002 by I. Koenig
//*-- Modified : 06/09/2004 by S. Lang
//*-- Modified : 13/09/2004 by J. Wuestenfeld

//_HADES_CLASS_DESCRIPTION
////////////////////////////////////////////////////////////////////////////
// HMdcTdcThreshold
//
// Container class for the TDC threshold parameters of the MDC
//
////////////////////////////////////////////////////////////////////////////
using namespace std;
#include <stdlib.h>

#include "hmdctdcthreshold.h"
#include "hades.h"
#include "hruntimedb.h"
#include "hspectrometer.h"
#include "hmdcdetector.h"
#include "hpario.h"
#include "hdetpario.h"
#include "hmdcrawstruct.h"
#include "hmessagemgr.h"
#include "TSystem.h"
#include <iostream>
#include <iomanip>

ClassImp(HMdcTdcThresholdDbo)
ClassImp(HMdcTdcThresholdMbo)
ClassImp(HMdcTdcThresholdMod)
ClassImp(HMdcTdcThresholdSec)
ClassImp(HMdcTdcThreshold)

void HMdcTdcThresholdDbo::fill(HMdcTdcThresholdDbo& r) {
    // copies the threshold
    threshold=r.getThreshold();
}

HMdcTdcThresholdMbo::HMdcTdcThresholdMbo(Int_t dbo, const Text_t* name) {
    // constructor takes to number of DBOs  and the name of the MBO
    SetName(name);
    array = new TObjArray(dbo);
    for (Int_t i=0; i<dbo; ++i) array->AddAt(new HMdcTdcThresholdDbo(),i);
}

HMdcTdcThresholdMbo::~HMdcTdcThresholdMbo() {
    // destructor
    array->Delete();
    delete array;
}

HMdcTdcThresholdMod::HMdcTdcThresholdMod(Int_t mbo) {
    // constructor takes the number of MBOs
    // The array objects of type HMdcThresholdMbo are created in the init function.
    array = new TObjArray(mbo);
}

HMdcTdcThresholdMod::~HMdcTdcThresholdMod() {
    // destructor
    array->Delete();
    delete array;
}

void HMdcTdcThresholdMod::createMbo(Int_t mbo, Int_t nDbos, const Char_t* mboName) {
  // creates the array object of type HMdcTdcThresholdMbo
  if (mbo<array->GetSize() && array->At(mbo)==NULL) {
    array->AddAt(new HMdcTdcThresholdMbo(nDbos,mboName),mbo);
  }
}

HMdcTdcThresholdSec::HMdcTdcThresholdSec(Int_t mod) {
    // constructor takes the number of modules
    array = new TObjArray(mod);
    for (Int_t i=0; i<mod; i++)
	array->AddAt(new HMdcTdcThresholdMod(),i);
}

HMdcTdcThresholdSec::~HMdcTdcThresholdSec() {
    // destructor
    array->Delete();
    delete array;
}

HMdcTdcThreshold::HMdcTdcThreshold(const Char_t* name,const Char_t* title,
				   const Char_t* context,Int_t n)
: HParSet(name,title,context) {
    // constructor
    strcpy(detName,"Mdc");
    oraVersion=-1;
    array = new TObjArray(n);
    for (Int_t i=0; i<n; i++) array->AddAt(new HMdcTdcThresholdSec(),i);
    pRawStruct=(HMdcRawStruct*)(gHades->getRuntimeDb()->getContainer("MdcRawStruct"));
}

HMdcTdcThreshold::~HMdcTdcThreshold() {
    // destructor
    array->Delete();
    delete array;
}

Bool_t HMdcTdcThreshold::init(HParIo* inp,Int_t* set) {
    // creates the array objects of type HMdcTdcThresholdMbo using the data in the parameter container
    // MdcRawStruct and initializes the container from an input
    if (pRawStruct==NULL) {
      Error("HMdcTdcThreshold::init()","MdcTdcThreshold not initialized, container MdcRawStruct is NULL");
      return kFALSE;
    }
    for(Int_t s=0;s<getSize();s++) {
      HMdcTdcThresholdSec& secPar=(*this)[s];
      HMdcRawSecStru& secStru=(*pRawStruct)[s];
      for(Int_t m=0;m<secPar.getSize();m++) {
        HMdcTdcThresholdMod& modPar=secPar[m];
        HMdcRawModStru& modStru=secStru[m];
        for(Int_t i=0;i<modStru.getSize();i++) {
          modPar.createMbo(i,modStru[i].getNTdcs()/16,modStru[i].GetName());
        }
      }
    }
    HDetParIo* input=inp->getDetParIo("HMdcParIo");
    if (input) return (input->init(this,set));
    return kFALSE;
}

Int_t HMdcTdcThreshold::write(HParIo* output) {
    // writes the container to an output
    HDetParIo* out=output->getDetParIo("HMdcParIo");
    if (out) return out->write(this);
    return -1;
}

void HMdcTdcThreshold::readline(const Char_t* buf, Int_t* set) {
    // decodes one line read from ascii file I/O and calls HMdcTdcThresholdDbo::setThreshold(...)
    Int_t   sec, mod, mbo, dbo;
    Int_t thresh;
    Char_t mboName[10];
    sscanf(buf,"%i%i%i%s%i%x",
	   &sec, &mod, &mbo, mboName, &dbo, &thresh);
    Int_t n=sec*4+mod;
    if (!set[n]) return;
    HMdcTdcThresholdMbo& rMbo=(*this)[sec][mod][mbo];
    rMbo.SetName(mboName);
    HMdcTdcThresholdDbo& db=rMbo[dbo];
    db.setThreshold(thresh);
    set[n]=999;
}

void HMdcTdcThreshold::readlinefromfile(const Char_t* buf) {
    // decodes one line read from ascii file and calls HMdcTdcThresholdDbo::setThreshold(...)
    Int_t   sec, mod, mbo, dbo;
    Int_t thresh;
    sscanf(buf,"%i%i%i%i%x",
	   &mod, &sec, &mbo, &dbo, &thresh);
    HMdcRawModStru & rawMod=(*pRawStruct)[sec][mod];
    //  cout << rawMod.getSize() << endl;
    if(rawMod.getSize()>mbo)
    {
	HMdcRawMothStru &rawMbo=rawMod[mbo];
	// cout << rawMbo.getNTdcs() << endl;
	if (rawMbo.getNTdcs() > dbo*16 )
	{
	    HMdcTdcThresholdMbo& rMbo=(*this)[sec][mod][mbo];
	    HMdcTdcThresholdDbo& db=rMbo[dbo];
	    db.setThreshold(thresh);
	}
	//      else{cout<<"dbo number to big"<<endl;};
    }
    //  else{cout<<"mbo number"<<endl;};
}

void HMdcTdcThreshold::writelinetofile(Char_t* buf, Int_t sec, Int_t mod, Int_t mbo, Int_t dbo) {
    // writes one line of threshold information into buffer, based on
    // sec mod mbo dbo address. If no dbo found sets threshold to 60!
    Int_t thresh;

    HMdcRawModStru & rawMod=(*pRawStruct)[sec][mod];
    //cout << rawMod.getSize() << endl;
    if(rawMod.getSize()>mbo)
    {
	HMdcRawMothStru &rawMbo=rawMod[mbo];
	//cout << rawMbo.getNTdcs() << endl;
	if (rawMbo.getNTdcs() > dbo*16 )
	{
	    HMdcTdcThresholdMbo& rMbo=(*this)[sec][mod][mbo];
	    HMdcTdcThresholdDbo& db=rMbo[dbo];
	    thresh = db.getThreshold();
	    sprintf(buf, "%i %i %i %i %x", mod, sec, mbo, dbo, thresh);

	}
	else sprintf(buf,"%i %i %i %i 60",mod,sec,mbo,dbo);
	//else{cout<<"dbo number to big"<<endl;};
    }
    else sprintf(buf,"%i %i %i %i 60",mod,sec,mbo,dbo);
    //else{cout<<"mbo number"<<endl;};
}


void HMdcTdcThreshold::addvalue(Int_t factor) {
    //to increase or decrease value of threshold by the constant factor
    //if you want to increast threshold then the value of the factor
    //should be positive, for decreasing the factor must be negative.

    Int_t sec, mbo, mod, dbo;
    for(sec=0; sec<6; sec++)
    {
	HMdcTdcThresholdSec & rawSec= (*this)[sec];
	for(mod=0; mod<rawSec.getSize(); mod++)
	{
	    HMdcTdcThresholdMod & rawMod=rawSec[mod];
	    for(mbo =0 ; mbo < rawMod.getSize() ; mbo++)
	    {
		HMdcTdcThresholdMbo &rawMbo=rawMod[mbo];
		for(dbo =0; dbo < rawMbo.getSize(); dbo++ )
		{
		    HMdcTdcThresholdDbo& db=rawMbo[dbo];
		    db.setThreshold((db.getThreshold())+factor);
		}
	    }
	}
    }
}

void HMdcTdcThreshold::putAsciiHeader(TString& header) {
    // puts the ascii header to the string used in HMdcParAsciiFileIo
    header=
	"# Calibration parameters of the MDC\n"
	"# Format:\n"
	"# sector  module  mbo  mboName  threshold\n";
}

Bool_t HMdcTdcThreshold::writeline(Char_t *buf, Int_t sec, Int_t mod, Int_t mbo, Int_t dbo) {
    // writes one line to the buffer used by ascii file I/O
    HMdcTdcThresholdMbo& rMbo=(*this)[sec][mod][mbo];
    HMdcTdcThresholdDbo& db=rMbo[dbo];
    sprintf(buf,"%1i %1i %2i %s %2i %3x\n",
	    sec, mod, mbo, rMbo.GetName(), dbo, db.getThreshold());
    return kTRUE;
}

void HMdcTdcThreshold::clear() {
    // clears the container
    oraVersion=-1;
    for(Int_t s=0;s<getSize();s++) {
	HMdcTdcThresholdSec& sec=(*this)[s];
	for(Int_t m=0;m<sec.getSize();m++) {
	    HMdcTdcThresholdMod& mod=sec[m];
	    for(Int_t l=0;l<mod.getSize();l++) {
		HMdcTdcThresholdMbo& mbo=mod[l];
		for(Int_t c=0;c<mbo.getSize();c++) mbo[c].clear();
	    }
	}
    }
    status=kFALSE;
    resetInputVersions();
}
void HMdcTdcThreshold::printParam() {
    // prints the container
    SEPERATOR_msg("-",60);
    INFO_msg(10,HMessageMgr::DET_MDC,"HMdcTdcThreshold");
    for(Int_t s=0;s<getSize();s++) {
	HMdcTdcThresholdSec& sec=(*this)[s];
	for(Int_t m=0;m<sec.getSize();m++) {
	    HMdcTdcThresholdMod& mod=sec[m];
	    for(Int_t l=0;l<mod.getSize();l++) {
		HMdcTdcThresholdMbo& mbo=mod[l];
		for(Int_t c=0;c<mbo.getSize();c++){
		    gHades->getMsg()->info(10,HMessageMgr::DET_MDC,GetName(),
					   "%1i %1i %2i %s % 1i %3x",
					   s, m, l, mbo.GetName(), c, mbo[c].getThreshold());
		}
	    }
	}
    }
    SEPERATOR_msg("-",60);
}

void HMdcTdcThreshold::Streamer(TBuffer &R__b)
{
   // Stream an object of class HMdcTdcThreshold.

   UInt_t R__s, R__c;
   if (R__b.IsReading()) {
      Version_t R__v = R__b.ReadVersion(&R__s, &R__c); if (R__v) { }
      HParSet::Streamer(R__b);
      R__b >> array;
      R__b >> oraVersion;
      if (R__v==3) {
        Int_t* dummy;
        R__b >> dummy;  // HMdcRawStruct*
        R__b >> dummy;  // HMdcRawSecStru*
        R__b >> dummy;  // HMdcMboReadout*
      }
      R__b.CheckByteCount(R__s, R__c, HMdcTdcThreshold::IsA());
   } else {
      R__c = R__b.WriteVersion(HMdcTdcThreshold::IsA(), kTRUE);
      HParSet::Streamer(R__b);
      R__b << array;
      R__b << oraVersion;
      R__b.SetByteCount(R__c, kTRUE);
   }
}
 hmdctdcthreshold.cc:1
 hmdctdcthreshold.cc:2
 hmdctdcthreshold.cc:3
 hmdctdcthreshold.cc:4
 hmdctdcthreshold.cc:5
 hmdctdcthreshold.cc:6
 hmdctdcthreshold.cc:7
 hmdctdcthreshold.cc:8
 hmdctdcthreshold.cc:9
 hmdctdcthreshold.cc:10
 hmdctdcthreshold.cc:11
 hmdctdcthreshold.cc:12
 hmdctdcthreshold.cc:13
 hmdctdcthreshold.cc:14
 hmdctdcthreshold.cc:15
 hmdctdcthreshold.cc:16
 hmdctdcthreshold.cc:17
 hmdctdcthreshold.cc:18
 hmdctdcthreshold.cc:19
 hmdctdcthreshold.cc:20
 hmdctdcthreshold.cc:21
 hmdctdcthreshold.cc:22
 hmdctdcthreshold.cc:23
 hmdctdcthreshold.cc:24
 hmdctdcthreshold.cc:25
 hmdctdcthreshold.cc:26
 hmdctdcthreshold.cc:27
 hmdctdcthreshold.cc:28
 hmdctdcthreshold.cc:29
 hmdctdcthreshold.cc:30
 hmdctdcthreshold.cc:31
 hmdctdcthreshold.cc:32
 hmdctdcthreshold.cc:33
 hmdctdcthreshold.cc:34
 hmdctdcthreshold.cc:35
 hmdctdcthreshold.cc:36
 hmdctdcthreshold.cc:37
 hmdctdcthreshold.cc:38
 hmdctdcthreshold.cc:39
 hmdctdcthreshold.cc:40
 hmdctdcthreshold.cc:41
 hmdctdcthreshold.cc:42
 hmdctdcthreshold.cc:43
 hmdctdcthreshold.cc:44
 hmdctdcthreshold.cc:45
 hmdctdcthreshold.cc:46
 hmdctdcthreshold.cc:47
 hmdctdcthreshold.cc:48
 hmdctdcthreshold.cc:49
 hmdctdcthreshold.cc:50
 hmdctdcthreshold.cc:51
 hmdctdcthreshold.cc:52
 hmdctdcthreshold.cc:53
 hmdctdcthreshold.cc:54
 hmdctdcthreshold.cc:55
 hmdctdcthreshold.cc:56
 hmdctdcthreshold.cc:57
 hmdctdcthreshold.cc:58
 hmdctdcthreshold.cc:59
 hmdctdcthreshold.cc:60
 hmdctdcthreshold.cc:61
 hmdctdcthreshold.cc:62
 hmdctdcthreshold.cc:63
 hmdctdcthreshold.cc:64
 hmdctdcthreshold.cc:65
 hmdctdcthreshold.cc:66
 hmdctdcthreshold.cc:67
 hmdctdcthreshold.cc:68
 hmdctdcthreshold.cc:69
 hmdctdcthreshold.cc:70
 hmdctdcthreshold.cc:71
 hmdctdcthreshold.cc:72
 hmdctdcthreshold.cc:73
 hmdctdcthreshold.cc:74
 hmdctdcthreshold.cc:75
 hmdctdcthreshold.cc:76
 hmdctdcthreshold.cc:77
 hmdctdcthreshold.cc:78
 hmdctdcthreshold.cc:79
 hmdctdcthreshold.cc:80
 hmdctdcthreshold.cc:81
 hmdctdcthreshold.cc:82
 hmdctdcthreshold.cc:83
 hmdctdcthreshold.cc:84
 hmdctdcthreshold.cc:85
 hmdctdcthreshold.cc:86
 hmdctdcthreshold.cc:87
 hmdctdcthreshold.cc:88
 hmdctdcthreshold.cc:89
 hmdctdcthreshold.cc:90
 hmdctdcthreshold.cc:91
 hmdctdcthreshold.cc:92
 hmdctdcthreshold.cc:93
 hmdctdcthreshold.cc:94
 hmdctdcthreshold.cc:95
 hmdctdcthreshold.cc:96
 hmdctdcthreshold.cc:97
 hmdctdcthreshold.cc:98
 hmdctdcthreshold.cc:99
 hmdctdcthreshold.cc:100
 hmdctdcthreshold.cc:101
 hmdctdcthreshold.cc:102
 hmdctdcthreshold.cc:103
 hmdctdcthreshold.cc:104
 hmdctdcthreshold.cc:105
 hmdctdcthreshold.cc:106
 hmdctdcthreshold.cc:107
 hmdctdcthreshold.cc:108
 hmdctdcthreshold.cc:109
 hmdctdcthreshold.cc:110
 hmdctdcthreshold.cc:111
 hmdctdcthreshold.cc:112
 hmdctdcthreshold.cc:113
 hmdctdcthreshold.cc:114
 hmdctdcthreshold.cc:115
 hmdctdcthreshold.cc:116
 hmdctdcthreshold.cc:117
 hmdctdcthreshold.cc:118
 hmdctdcthreshold.cc:119
 hmdctdcthreshold.cc:120
 hmdctdcthreshold.cc:121
 hmdctdcthreshold.cc:122
 hmdctdcthreshold.cc:123
 hmdctdcthreshold.cc:124
 hmdctdcthreshold.cc:125
 hmdctdcthreshold.cc:126
 hmdctdcthreshold.cc:127
 hmdctdcthreshold.cc:128
 hmdctdcthreshold.cc:129
 hmdctdcthreshold.cc:130
 hmdctdcthreshold.cc:131
 hmdctdcthreshold.cc:132
 hmdctdcthreshold.cc:133
 hmdctdcthreshold.cc:134
 hmdctdcthreshold.cc:135
 hmdctdcthreshold.cc:136
 hmdctdcthreshold.cc:137
 hmdctdcthreshold.cc:138
 hmdctdcthreshold.cc:139
 hmdctdcthreshold.cc:140
 hmdctdcthreshold.cc:141
 hmdctdcthreshold.cc:142
 hmdctdcthreshold.cc:143
 hmdctdcthreshold.cc:144
 hmdctdcthreshold.cc:145
 hmdctdcthreshold.cc:146
 hmdctdcthreshold.cc:147
 hmdctdcthreshold.cc:148
 hmdctdcthreshold.cc:149
 hmdctdcthreshold.cc:150
 hmdctdcthreshold.cc:151
 hmdctdcthreshold.cc:152
 hmdctdcthreshold.cc:153
 hmdctdcthreshold.cc:154
 hmdctdcthreshold.cc:155
 hmdctdcthreshold.cc:156
 hmdctdcthreshold.cc:157
 hmdctdcthreshold.cc:158
 hmdctdcthreshold.cc:159
 hmdctdcthreshold.cc:160
 hmdctdcthreshold.cc:161
 hmdctdcthreshold.cc:162
 hmdctdcthreshold.cc:163
 hmdctdcthreshold.cc:164
 hmdctdcthreshold.cc:165
 hmdctdcthreshold.cc:166
 hmdctdcthreshold.cc:167
 hmdctdcthreshold.cc:168
 hmdctdcthreshold.cc:169
 hmdctdcthreshold.cc:170
 hmdctdcthreshold.cc:171
 hmdctdcthreshold.cc:172
 hmdctdcthreshold.cc:173
 hmdctdcthreshold.cc:174
 hmdctdcthreshold.cc:175
 hmdctdcthreshold.cc:176
 hmdctdcthreshold.cc:177
 hmdctdcthreshold.cc:178
 hmdctdcthreshold.cc:179
 hmdctdcthreshold.cc:180
 hmdctdcthreshold.cc:181
 hmdctdcthreshold.cc:182
 hmdctdcthreshold.cc:183
 hmdctdcthreshold.cc:184
 hmdctdcthreshold.cc:185
 hmdctdcthreshold.cc:186
 hmdctdcthreshold.cc:187
 hmdctdcthreshold.cc:188
 hmdctdcthreshold.cc:189
 hmdctdcthreshold.cc:190
 hmdctdcthreshold.cc:191
 hmdctdcthreshold.cc:192
 hmdctdcthreshold.cc:193
 hmdctdcthreshold.cc:194
 hmdctdcthreshold.cc:195
 hmdctdcthreshold.cc:196
 hmdctdcthreshold.cc:197
 hmdctdcthreshold.cc:198
 hmdctdcthreshold.cc:199
 hmdctdcthreshold.cc:200
 hmdctdcthreshold.cc:201
 hmdctdcthreshold.cc:202
 hmdctdcthreshold.cc:203
 hmdctdcthreshold.cc:204
 hmdctdcthreshold.cc:205
 hmdctdcthreshold.cc:206
 hmdctdcthreshold.cc:207
 hmdctdcthreshold.cc:208
 hmdctdcthreshold.cc:209
 hmdctdcthreshold.cc:210
 hmdctdcthreshold.cc:211
 hmdctdcthreshold.cc:212
 hmdctdcthreshold.cc:213
 hmdctdcthreshold.cc:214
 hmdctdcthreshold.cc:215
 hmdctdcthreshold.cc:216
 hmdctdcthreshold.cc:217
 hmdctdcthreshold.cc:218
 hmdctdcthreshold.cc:219
 hmdctdcthreshold.cc:220
 hmdctdcthreshold.cc:221
 hmdctdcthreshold.cc:222
 hmdctdcthreshold.cc:223
 hmdctdcthreshold.cc:224
 hmdctdcthreshold.cc:225
 hmdctdcthreshold.cc:226
 hmdctdcthreshold.cc:227
 hmdctdcthreshold.cc:228
 hmdctdcthreshold.cc:229
 hmdctdcthreshold.cc:230
 hmdctdcthreshold.cc:231
 hmdctdcthreshold.cc:232
 hmdctdcthreshold.cc:233
 hmdctdcthreshold.cc:234
 hmdctdcthreshold.cc:235
 hmdctdcthreshold.cc:236
 hmdctdcthreshold.cc:237
 hmdctdcthreshold.cc:238
 hmdctdcthreshold.cc:239
 hmdctdcthreshold.cc:240
 hmdctdcthreshold.cc:241
 hmdctdcthreshold.cc:242
 hmdctdcthreshold.cc:243
 hmdctdcthreshold.cc:244
 hmdctdcthreshold.cc:245
 hmdctdcthreshold.cc:246
 hmdctdcthreshold.cc:247
 hmdctdcthreshold.cc:248
 hmdctdcthreshold.cc:249
 hmdctdcthreshold.cc:250
 hmdctdcthreshold.cc:251
 hmdctdcthreshold.cc:252
 hmdctdcthreshold.cc:253
 hmdctdcthreshold.cc:254
 hmdctdcthreshold.cc:255
 hmdctdcthreshold.cc:256
 hmdctdcthreshold.cc:257
 hmdctdcthreshold.cc:258
 hmdctdcthreshold.cc:259
 hmdctdcthreshold.cc:260
 hmdctdcthreshold.cc:261
 hmdctdcthreshold.cc:262
 hmdctdcthreshold.cc:263
 hmdctdcthreshold.cc:264
 hmdctdcthreshold.cc:265
 hmdctdcthreshold.cc:266
 hmdctdcthreshold.cc:267
 hmdctdcthreshold.cc:268
 hmdctdcthreshold.cc:269
 hmdctdcthreshold.cc:270
 hmdctdcthreshold.cc:271
 hmdctdcthreshold.cc:272
 hmdctdcthreshold.cc:273
 hmdctdcthreshold.cc:274
 hmdctdcthreshold.cc:275
 hmdctdcthreshold.cc:276
 hmdctdcthreshold.cc:277
 hmdctdcthreshold.cc:278
 hmdctdcthreshold.cc:279
 hmdctdcthreshold.cc:280
 hmdctdcthreshold.cc:281
 hmdctdcthreshold.cc:282
 hmdctdcthreshold.cc:283
 hmdctdcthreshold.cc:284
 hmdctdcthreshold.cc:285
 hmdctdcthreshold.cc:286
 hmdctdcthreshold.cc:287
 hmdctdcthreshold.cc:288
 hmdctdcthreshold.cc:289
 hmdctdcthreshold.cc:290
 hmdctdcthreshold.cc:291
 hmdctdcthreshold.cc:292
 hmdctdcthreshold.cc:293
 hmdctdcthreshold.cc:294
 hmdctdcthreshold.cc:295
 hmdctdcthreshold.cc:296
 hmdctdcthreshold.cc:297
 hmdctdcthreshold.cc:298
 hmdctdcthreshold.cc:299
 hmdctdcthreshold.cc:300
 hmdctdcthreshold.cc:301