#include "THaDetMap.h"
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;
const int THaDetMap::kDetMapSize;
struct ModuleType {
UInt_t model;
Bool_t adc;
Bool_t tdc;
};
static const ModuleType module_list[] = {
{ 1875, 0, 1 },
{ 1877, 0, 1 },
{ 1881, 1, 0 },
{ 1872, 0, 1 },
{ 3123, 1, 0 },
{ 1182, 1, 0 },
{ 792, 1, 0 },
{ 775, 0, 1 },
{ 767, 0, 1 },
{ 3201, 0, 1 },
{ 6401, 0, 1 },
{ 0 }
};
void THaDetMap::Module::SetModel( UInt_t m )
{
model = m & kModelMask;
const ModuleType* md = module_list;
while ( md->model && model != md->model ) md++;
if( md->adc ) MakeADC();
if( md->tdc ) MakeTDC();
}
void THaDetMap::Module::SetResolution( Double_t res )
{
resolution = res;
}
THaDetMap::THaDetMap() : fNmodules(0), fMap(0), fMaplength(0)
{
}
THaDetMap::THaDetMap( const THaDetMap& rhs )
{
fMaplength = rhs.fMaplength;
fNmodules = rhs.fNmodules;
if( fMaplength > 0 ) {
fMap = new Module[fMaplength];
memcpy(fMap,rhs.fMap,fNmodules*sizeof(Module));
}
}
THaDetMap& THaDetMap::operator=( const THaDetMap& rhs )
{
if ( this != &rhs ) {
if ( fMaplength != rhs.fMaplength ) {
delete [] fMap;
fMaplength = rhs.fMaplength;
if( fMaplength > 0 )
fMap = new Module[fMaplength];
}
fNmodules = rhs.fNmodules;
memcpy(fMap,rhs.fMap,fNmodules*sizeof(Module));
}
return *this;
}
THaDetMap::~THaDetMap()
{
delete [] fMap;
}
Int_t THaDetMap::AddModule( UShort_t crate, UShort_t slot,
UShort_t chan_lo, UShort_t chan_hi,
UInt_t first, UInt_t model, Int_t refindex,
Int_t refchan )
{
if( fNmodules >= kDetMapSize ) return -1;
bool reverse = ( chan_lo > chan_hi );
if ( fNmodules >= fMaplength ) {
Int_t oldlen = fMaplength;
fMaplength += 10;
Module* tmpmap = new Module[fMaplength];
if( oldlen > 0 ) {
memcpy(tmpmap,fMap,oldlen*sizeof(Module));
delete [] fMap;
}
fMap = tmpmap;
}
Module& m = fMap[fNmodules];
m.crate = crate;
m.slot = slot;
if( reverse ) {
m.lo = chan_hi;
m.hi = chan_lo;
} else {
m.lo = chan_lo;
m.hi = chan_hi;
}
m.first = first;
m.SetModel( model );
m.refindex = refindex;
m.refchan = refchan;
m.SetResolution( 0.0 );
m.reverse = reverse;
return ++fNmodules;
}
THaDetMap::Module* THaDetMap::Find( UShort_t crate, UShort_t slot,
UShort_t chan )
{
Module* found = NULL;
for( UShort_t i = 0; i < fNmodules; ++i ) {
Module* d = uGetModule(i);
if( d->crate == crate && d->slot == slot &&
d->lo <= chan && chan <= d->hi ) {
found = d;
break;
}
}
return found;
}
Int_t THaDetMap::Fill( const vector<Int_t>& values, UInt_t flags )
{
typedef vector<Int_t>::size_type vsiz_t;
if( (flags & kDoNotClear) == 0 )
Clear();
vsiz_t tuple_size = 4;
if( flags & kFillLogicalChannel )
tuple_size++;
if( flags & kFillModel )
tuple_size++;
if( flags & kFillRefChan )
tuple_size++;
if( flags & kFillRefIndex )
tuple_size++;
UInt_t prev_first = 0, prev_nchan = 0;
UInt_t first = 0, model = 0;
Int_t rchan = -1, ref = -1;
Int_t ret = 0;
for( vsiz_t i = 0; i < values.size(); i += tuple_size ) {
if( values[i] < 0 )
break;
if( i+tuple_size > values.size() ) {
ret = -127;
break;
}
vsiz_t k = 4;
if( flags & kFillLogicalChannel )
first = values[i+k++];
else {
first = prev_first + prev_nchan;
}
if( flags & kFillModel )
model = values[i+k++];
if( flags & kFillRefChan )
rchan = values[i+k++];
if( flags & kFillRefIndex )
ref = values[i+k++];
ret = AddModule( values[i], values[i+1], values[i+2], values[i+3],
first, model, ref, rchan );
if( ret<=0 )
break;
prev_first = first;
prev_nchan = GetNchan( ret-1 );
}
return ret;
}
Int_t THaDetMap::GetTotNumChan() const
{
Int_t sum = 0;
for( UShort_t i = 0; i < fNmodules; i++ )
sum += GetNchan(i);
return sum;
}
void THaDetMap::GetMinMaxChan( Int_t& min, Int_t& max, ECountMode mode ) const
{
min = kMaxInt;
max = kMinInt;
bool do_ref = ( mode == kRefIndex );
for( UShort_t i = 0; i < fNmodules; i++ ) {
Module* m = GetModule(i);
Int_t m_min = do_ref ? m->refindex : m->first;
Int_t m_max = do_ref ? m->refindex : m->first + m->hi - m->lo;
if( m_min < min )
min = m_min;
if( m_max > max )
max = m_max;
}
}
void THaDetMap::Print( Option_t* ) const
{
cout << "Size: " << fNmodules << endl;
for( UShort_t i=0; i<fNmodules; i++ ) {
Module* m = GetModule(i);
cout << " "
<< setw(5) << m->crate
<< setw(5) << m->slot
<< setw(5) << m->lo
<< setw(5) << m->hi
<< setw(5) << m->first
<< setw(5) << GetModel(m);
if( IsADC(m) )
cout << setw(4) << " ADC";
if( IsTDC(m) )
cout << setw(4) << " TDC";
cout << setw(5) << m->refchan
<< setw(5) << m->refindex
<< setw(8) << m->resolution
<< endl;
}
}
void THaDetMap::Reset()
{
Clear();
delete [] fMap;
fMap = NULL;
fMaplength = 0;
}
static int compare_modules( const void* p1, const void* p2 )
{
const THaDetMap::Module* lhs = static_cast<const THaDetMap::Module*>(p1);
const THaDetMap::Module* rhs = static_cast<const THaDetMap::Module*>(p2);
if( lhs->crate < rhs->crate ) return -1;
if( lhs->crate > rhs->crate ) return 1;
if( lhs->slot < rhs->slot ) return -1;
if( lhs->slot > rhs->slot ) return 1;
if( lhs->lo < rhs->lo ) return -1;
if( lhs->lo > rhs->lo ) return 1;
return 0;
}
void THaDetMap::Sort()
{
if( fMap && fNmodules )
qsort( fMap, fNmodules, sizeof(Module), compare_modules );
}
ClassImp(THaDetMap)
Last change: Sat Nov 7 21:26:44 2009
Last generated: 2009-11-07 21:26
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.