#include "hdetpario.h"
#include "hpario.h"
#include "hrich700thresholdpar.h"
#include "richdef.h"
#include <fstream>
using namespace std;
ClassImp(HRich700ThresholdPar)
    HRich700ThresholdPar::HRich700ThresholdPar(const Char_t* name,
					       const Char_t* title, const Char_t* context) : HParSet(name, title, context)
{
    strcpy(detName, "Rich");
    fLoc.setNIndex(2);
    fParamsTable.set(2, RICH700_MAX_PMT+1, RICH700_MAX_PMTPIXELS+1);
    fParamsTable.setCellClassName("HRich700PixelThreshold");
    fParamsTable.makeObjTable();
    clear();
    status = kFALSE;
    resetInputVersions();
}
HRich700ThresholdPar::~HRich700ThresholdPar()
{
    fParamsTable.deleteTab();
}
void HRich700ThresholdPar::clear() {
    
    
    
    HRich700PixelThreshold* pCell = NULL;
    for (Int_t pmt = 0; pmt <= RICH700_MAX_PMT; ++pmt) {
	fLoc[0] = pmt;
	for (Int_t pixel = 0; pixel <= RICH700_MAX_PMTPIXELS; ++pixel) {
	    fLoc[1] = pixel;
	    pCell = static_cast<HRich700PixelThreshold*>(fParamsTable.getSlot(
									      fLoc));
	    if (NULL != pCell) {
		pCell = new (pCell) HRich700PixelThreshold;
		pCell->reset();
		pCell->setPMT(pmt);
		pCell->setPixel(pixel);
	    } else {
		Error("HRich700ThresholdPar", "Slot not found:  %i %i ",
		      fLoc[0], fLoc[1]);
	    }
	} 
    } 
}
Bool_t HRich700ThresholdPar::init(HParIo* inp, Int_t* set)
{
    
    HDetParIo* input = inp->getDetParIo("HRichParIo");
    if (NULL != input) {
	Bool_t returnValue = input->init(this, set);
#if DEBUG_LEVEL > 3
	printParams();
#endif
	return returnValue;
    }
    return kFALSE;
}
Int_t HRich700ThresholdPar::write(HParIo* output)
{
    
    HDetParIo* out = output->getDetParIo("HRichParIo");
    if (NULL != out) {
	return out->write(this);
    }
    return -1;
}
Bool_t HRich700ThresholdPar::readline(const Char_t * buf)
{
    
    Bool_t rc = kFALSE;
    Int_t pmt = 0;
    Int_t pix = 0;
    Double_t t_min = 0.0;
    Double_t t_max = 0.0;
    Double_t tot_min = 0.0;
    Double_t tot_max = 0.0;
    Int_t flag = 0;
    Int_t n = sscanf(buf, "%i %i %lf %lf %lf %lf %i", &pmt, &pix, &t_min, &t_max,
		     &tot_min, &tot_max, &flag);
    
    
    
    if (7 == n) {
	rc = setPixelThreshold(pmt, pix, t_min, t_max, tot_min, tot_max, flag);
    } else {
	if (n < 7)
	    Error("readline", "Not enough values in line %s\n", buf);
	else
	    Error("readline", "Too many values in line %s\n", buf);
    }
    return rc;
}
void HRich700ThresholdPar::write(fstream& fout)
{
    
    HRich700PixelThreshold* pCell = NULL;
    for (Int_t pmt = 1; pmt <= RICH700_MAX_PMT; ++pmt) {
	fLoc[0] = pmt;
	for (Int_t pixel = 1; pixel <= RICH700_MAX_PMTPIXELS; ++pixel) {
	    fLoc[1] = pixel;
	    pCell = static_cast<HRich700PixelThreshold*>(fParamsTable.getSlot(
									      fLoc));
	    if (NULL != pCell) {
		if (-1 != pCell->getFlag()) {
		    fout.width(4); 
		    fout << left << pCell->getPMT();
		    fout.width(4); 
		    fout << left << pCell->getPixel();
		    fout.width(10); 
		    fout << left << pCell->getT_Min();
		    fout.width(10); 
		    fout << left << pCell->getT_Max();
		    fout.width(10); 
		    fout << left << pCell->getTot_Min();
		    fout.width(10); 
		    fout << left << pCell->getTot_Max();
		    fout.width(4); 
		    fout << left << pCell->getFlag();
		    fout << endl;
		} 
	    } else {
		Error("HRich700ThresholdPar::write ", "Slot not found:  %i %i ",
		      fLoc[0], fLoc[1]);
	    }
	} 
    } 
}
Bool_t HRich700ThresholdPar::setPixelThreshold(Int_t pmt, Int_t pix,
					       Double_t tmin, Double_t tmax, Double_t totmin, Double_t totmax,
					       Short_t flag)
{
    
    if ((pmt <= 0) || (pmt > RICH700_MAX_PMT) || (pix <= 0)
	|| (pix > RICH700_MAX_PMTPIXELS)) {
	Error("setPixelThreshold", "Wrong pixel coordinates");
	return kFALSE;
    }
    HRich700PixelThreshold* pCell = NULL;
    fLoc[0] = pmt;
    fLoc[1] = pix;
    pCell = static_cast<HRich700PixelThreshold*>(fParamsTable.getSlot(fLoc));
    if (NULL != pCell) {
	pCell->setParams(tmin, tmax, totmin, totmax, flag);
    } else {
	Error("HRich700ThresholdPar::setPixelThreshold", "Cell not found.");
	return kFALSE;
    }
    return kTRUE;
}
void HRich700ThresholdPar::printParams()
{
    
    HRich700PixelThreshold* pCell = NULL;
    for (Int_t pmt = 1; pmt <= RICH700_MAX_PMT; ++pmt) {
	fLoc[0] = pmt;
	for (Int_t pixel = 1; pixel <= RICH700_MAX_PMTPIXELS; ++pixel) {
	    fLoc[1] = pixel;
	    pCell = static_cast<HRich700PixelThreshold*>(fParamsTable.getSlot(
									      fLoc));
	    if (NULL != pCell) {
		if (-1 != pCell->getFlag()) {
		    std::cout.width(4); 
		    std::cout << left << pCell->getPMT(); 
		    std::cout.width(4); 
		    std::cout << left << pCell->getPixel(); 
		    std::cout.width(10); 
		    std::cout << left << pCell->getT_Min();
		    std::cout.width(10); 
		    std::cout << left << pCell->getT_Max();
		    std::cout.width(10); 
		    std::cout << left << pCell->getTot_Min();
		    std::cout.width(10); 
		    std::cout << left << pCell->getTot_Max();
		    std::cout.width(4); 
		    std::cout << left << pCell->getFlag();
		    std::cout << endl;
		} 
	    } else {
		Error("HRich700ThresholdPar::write ", "Slot not found:  %i %i ",
		      fLoc[0], fLoc[1]);
	    }
	} 
    } 
    cout
	<< "#########################################################################"
	<< endl;
}
void HRich700ThresholdPar::putAsciiHeader(TString & header) {
    
    header =
	"# Rich700 trb3 unpacker time threshold parameters\n"
	"# Format:\n"
	"# PMT \tPixel \tT_min (ns) \tT_max (ns) \tTot_min (ns) \tTot_max (ns) \tFlag (0=OK, 1=suppress pixel)\n";
}
HRich700PixelThreshold* HRich700ThresholdPar::getSlot(HLocation &loc) {
    return static_cast<HRich700PixelThreshold*>(fParamsTable.getSlot(loc));
}
HRich700PixelThreshold* HRich700ThresholdPar::getObject(HLocation &loc) {
    return static_cast<HRich700PixelThreshold*>(fParamsTable.getObject(loc));
}
 hrich700thresholdpar.cc:1  hrich700thresholdpar.cc:2  hrich700thresholdpar.cc:3  hrich700thresholdpar.cc:4  hrich700thresholdpar.cc:5  hrich700thresholdpar.cc:6  hrich700thresholdpar.cc:7  hrich700thresholdpar.cc:8  hrich700thresholdpar.cc:9  hrich700thresholdpar.cc:10  hrich700thresholdpar.cc:11  hrich700thresholdpar.cc:12  hrich700thresholdpar.cc:13  hrich700thresholdpar.cc:14  hrich700thresholdpar.cc:15  hrich700thresholdpar.cc:16  hrich700thresholdpar.cc:17  hrich700thresholdpar.cc:18  hrich700thresholdpar.cc:19  hrich700thresholdpar.cc:20  hrich700thresholdpar.cc:21  hrich700thresholdpar.cc:22  hrich700thresholdpar.cc:23  hrich700thresholdpar.cc:24  hrich700thresholdpar.cc:25  hrich700thresholdpar.cc:26  hrich700thresholdpar.cc:27  hrich700thresholdpar.cc:28  hrich700thresholdpar.cc:29  hrich700thresholdpar.cc:30  hrich700thresholdpar.cc:31  hrich700thresholdpar.cc:32  hrich700thresholdpar.cc:33  hrich700thresholdpar.cc:34  hrich700thresholdpar.cc:35  hrich700thresholdpar.cc:36  hrich700thresholdpar.cc:37  hrich700thresholdpar.cc:38  hrich700thresholdpar.cc:39  hrich700thresholdpar.cc:40  hrich700thresholdpar.cc:41  hrich700thresholdpar.cc:42  hrich700thresholdpar.cc:43  hrich700thresholdpar.cc:44  hrich700thresholdpar.cc:45  hrich700thresholdpar.cc:46  hrich700thresholdpar.cc:47  hrich700thresholdpar.cc:48  hrich700thresholdpar.cc:49  hrich700thresholdpar.cc:50  hrich700thresholdpar.cc:51  hrich700thresholdpar.cc:52  hrich700thresholdpar.cc:53  hrich700thresholdpar.cc:54  hrich700thresholdpar.cc:55  hrich700thresholdpar.cc:56  hrich700thresholdpar.cc:57  hrich700thresholdpar.cc:58  hrich700thresholdpar.cc:59  hrich700thresholdpar.cc:60  hrich700thresholdpar.cc:61  hrich700thresholdpar.cc:62  hrich700thresholdpar.cc:63  hrich700thresholdpar.cc:64  hrich700thresholdpar.cc:65  hrich700thresholdpar.cc:66  hrich700thresholdpar.cc:67  hrich700thresholdpar.cc:68  hrich700thresholdpar.cc:69  hrich700thresholdpar.cc:70  hrich700thresholdpar.cc:71  hrich700thresholdpar.cc:72  hrich700thresholdpar.cc:73  hrich700thresholdpar.cc:74  hrich700thresholdpar.cc:75  hrich700thresholdpar.cc:76  hrich700thresholdpar.cc:77  hrich700thresholdpar.cc:78  hrich700thresholdpar.cc:79  hrich700thresholdpar.cc:80  hrich700thresholdpar.cc:81  hrich700thresholdpar.cc:82  hrich700thresholdpar.cc:83  hrich700thresholdpar.cc:84  hrich700thresholdpar.cc:85  hrich700thresholdpar.cc:86  hrich700thresholdpar.cc:87  hrich700thresholdpar.cc:88  hrich700thresholdpar.cc:89  hrich700thresholdpar.cc:90  hrich700thresholdpar.cc:91  hrich700thresholdpar.cc:92  hrich700thresholdpar.cc:93  hrich700thresholdpar.cc:94  hrich700thresholdpar.cc:95  hrich700thresholdpar.cc:96  hrich700thresholdpar.cc:97  hrich700thresholdpar.cc:98  hrich700thresholdpar.cc:99  hrich700thresholdpar.cc:100  hrich700thresholdpar.cc:101  hrich700thresholdpar.cc:102  hrich700thresholdpar.cc:103  hrich700thresholdpar.cc:104  hrich700thresholdpar.cc:105  hrich700thresholdpar.cc:106  hrich700thresholdpar.cc:107  hrich700thresholdpar.cc:108  hrich700thresholdpar.cc:109  hrich700thresholdpar.cc:110  hrich700thresholdpar.cc:111  hrich700thresholdpar.cc:112  hrich700thresholdpar.cc:113  hrich700thresholdpar.cc:114  hrich700thresholdpar.cc:115  hrich700thresholdpar.cc:116  hrich700thresholdpar.cc:117  hrich700thresholdpar.cc:118  hrich700thresholdpar.cc:119  hrich700thresholdpar.cc:120  hrich700thresholdpar.cc:121  hrich700thresholdpar.cc:122  hrich700thresholdpar.cc:123  hrich700thresholdpar.cc:124  hrich700thresholdpar.cc:125  hrich700thresholdpar.cc:126  hrich700thresholdpar.cc:127  hrich700thresholdpar.cc:128  hrich700thresholdpar.cc:129  hrich700thresholdpar.cc:130  hrich700thresholdpar.cc:131  hrich700thresholdpar.cc:132  hrich700thresholdpar.cc:133  hrich700thresholdpar.cc:134  hrich700thresholdpar.cc:135  hrich700thresholdpar.cc:136  hrich700thresholdpar.cc:137  hrich700thresholdpar.cc:138  hrich700thresholdpar.cc:139  hrich700thresholdpar.cc:140  hrich700thresholdpar.cc:141  hrich700thresholdpar.cc:142  hrich700thresholdpar.cc:143  hrich700thresholdpar.cc:144  hrich700thresholdpar.cc:145  hrich700thresholdpar.cc:146  hrich700thresholdpar.cc:147  hrich700thresholdpar.cc:148  hrich700thresholdpar.cc:149  hrich700thresholdpar.cc:150  hrich700thresholdpar.cc:151  hrich700thresholdpar.cc:152  hrich700thresholdpar.cc:153  hrich700thresholdpar.cc:154  hrich700thresholdpar.cc:155  hrich700thresholdpar.cc:156  hrich700thresholdpar.cc:157  hrich700thresholdpar.cc:158  hrich700thresholdpar.cc:159  hrich700thresholdpar.cc:160  hrich700thresholdpar.cc:161  hrich700thresholdpar.cc:162  hrich700thresholdpar.cc:163  hrich700thresholdpar.cc:164  hrich700thresholdpar.cc:165  hrich700thresholdpar.cc:166  hrich700thresholdpar.cc:167  hrich700thresholdpar.cc:168  hrich700thresholdpar.cc:169  hrich700thresholdpar.cc:170  hrich700thresholdpar.cc:171  hrich700thresholdpar.cc:172  hrich700thresholdpar.cc:173  hrich700thresholdpar.cc:174  hrich700thresholdpar.cc:175  hrich700thresholdpar.cc:176  hrich700thresholdpar.cc:177  hrich700thresholdpar.cc:178  hrich700thresholdpar.cc:179  hrich700thresholdpar.cc:180  hrich700thresholdpar.cc:181  hrich700thresholdpar.cc:182  hrich700thresholdpar.cc:183  hrich700thresholdpar.cc:184  hrich700thresholdpar.cc:185  hrich700thresholdpar.cc:186  hrich700thresholdpar.cc:187  hrich700thresholdpar.cc:188  hrich700thresholdpar.cc:189  hrich700thresholdpar.cc:190  hrich700thresholdpar.cc:191  hrich700thresholdpar.cc:192  hrich700thresholdpar.cc:193  hrich700thresholdpar.cc:194  hrich700thresholdpar.cc:195  hrich700thresholdpar.cc:196  hrich700thresholdpar.cc:197  hrich700thresholdpar.cc:198  hrich700thresholdpar.cc:199  hrich700thresholdpar.cc:200  hrich700thresholdpar.cc:201  hrich700thresholdpar.cc:202  hrich700thresholdpar.cc:203  hrich700thresholdpar.cc:204  hrich700thresholdpar.cc:205  hrich700thresholdpar.cc:206  hrich700thresholdpar.cc:207  hrich700thresholdpar.cc:208  hrich700thresholdpar.cc:209  hrich700thresholdpar.cc:210  hrich700thresholdpar.cc:211  hrich700thresholdpar.cc:212  hrich700thresholdpar.cc:213  hrich700thresholdpar.cc:214  hrich700thresholdpar.cc:215  hrich700thresholdpar.cc:216  hrich700thresholdpar.cc:217  hrich700thresholdpar.cc:218  hrich700thresholdpar.cc:219  hrich700thresholdpar.cc:220  hrich700thresholdpar.cc:221  hrich700thresholdpar.cc:222  hrich700thresholdpar.cc:223  hrich700thresholdpar.cc:224  hrich700thresholdpar.cc:225  hrich700thresholdpar.cc:226  hrich700thresholdpar.cc:227  hrich700thresholdpar.cc:228  hrich700thresholdpar.cc:229  hrich700thresholdpar.cc:230  hrich700thresholdpar.cc:231  hrich700thresholdpar.cc:232  hrich700thresholdpar.cc:233  hrich700thresholdpar.cc:234  hrich700thresholdpar.cc:235  hrich700thresholdpar.cc:236  hrich700thresholdpar.cc:237  hrich700thresholdpar.cc:238  hrich700thresholdpar.cc:239  hrich700thresholdpar.cc:240  hrich700thresholdpar.cc:241  hrich700thresholdpar.cc:242  hrich700thresholdpar.cc:243  hrich700thresholdpar.cc:244  hrich700thresholdpar.cc:245  hrich700thresholdpar.cc:246  hrich700thresholdpar.cc:247  hrich700thresholdpar.cc:248  hrich700thresholdpar.cc:249  hrich700thresholdpar.cc:250  hrich700thresholdpar.cc:251  hrich700thresholdpar.cc:252  hrich700thresholdpar.cc:253  hrich700thresholdpar.cc:254