00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "CondFormats/DTObjects/interface/DTT0.h"
00014
00015
00016
00017
00018
00019 #include "CondFormats/DTObjects/interface/DTSequentialLayerNumber.h"
00020
00021
00022
00023
00024 #include <iostream>
00025 #include <sstream>
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 DTT0::DTT0():
00036 dataVersion( " " ),
00037 nsPerCount( 25.0 / 32.0 ) {
00038 dataList.reserve( 12000 );
00039
00040 dBuf = 0;
00041 sortedLayers = 0;
00042 sequencePtr = 0;
00043 }
00044
00045
00046 DTT0::DTT0( const std::string& version ):
00047 dataVersion( version ),
00048 nsPerCount( 25.0 / 32.0 ) {
00049 dataList.reserve( 12000 );
00050
00051 dBuf = 0;
00052 sortedLayers = 0;
00053 sequencePtr = 0;
00054 }
00055
00056
00057 DTT0Id::DTT0Id() :
00058 wheelId( 0 ),
00059 stationId( 0 ),
00060 sectorId( 0 ),
00061 slId( 0 ),
00062 layerId( 0 ),
00063 cellId( 0 ) {
00064 }
00065
00066
00067 DTT0Data::DTT0Data() :
00068 t0mean( 0.0 ),
00069 t0rms( 0.0 ) {
00070 }
00071
00072
00073
00074
00075
00076 DTT0::~DTT0() {
00077 delete dBuf;
00078 delete sortedLayers;
00079 delete sequencePtr;
00080 }
00081
00082
00083 DTT0Id::~DTT0Id() {
00084 }
00085
00086
00087 DTT0Data::~DTT0Data() {
00088 }
00089
00090
00091
00092
00093
00094 int DTT0::get( int wheelId,
00095 int stationId,
00096 int sectorId,
00097 int slId,
00098 int layerId,
00099 int cellId,
00100 float& t0mean,
00101 float& t0rms,
00102 DTTimeUnits::type unit ) const {
00103
00104 t0mean =
00105 t0rms = 0.0;
00106
00107 if ( ( dBuf == 0 ) && ( sortedLayers == 0 ) ) checkOrder();
00108
00109
00110 if ( sortedLayers != 0 ) return getSorted( wheelId, stationId, sectorId,
00111 slId, layerId, cellId,
00112 t0mean, t0rms, unit );
00113 else return getRandom( wheelId, stationId, sectorId,
00114 slId, layerId, cellId,
00115 t0mean, t0rms, unit );
00116 return -999999999;
00117 }
00118
00119
00120 int DTT0::getRandom( int wheelId,
00121 int stationId,
00122 int sectorId,
00123 int slId,
00124 int layerId,
00125 int cellId,
00126 float& t0mean,
00127 float& t0rms,
00128 DTTimeUnits::type unit ) const {
00129
00130
00131
00132
00133 std::vector<int> chanKey;
00134 chanKey.reserve(6);
00135 chanKey.push_back( wheelId );
00136 chanKey.push_back( stationId );
00137 chanKey.push_back( sectorId );
00138 chanKey.push_back( slId );
00139 chanKey.push_back( layerId );
00140 chanKey.push_back( cellId );
00141
00142 int ientry;
00143
00144
00145
00146 int searchStatus = dBuf->find( chanKey.begin(), chanKey.end(), ientry );
00147 if ( !searchStatus ) {
00148
00149 const DTT0Data& data( dataList[ientry].second );
00150 t0mean = data.t0mean;
00151 t0rms = data.t0rms;
00152 if ( unit == DTTimeUnits::ns ) {
00153 t0mean *= nsPerCount;
00154 t0rms *= nsPerCount;
00155 }
00156 }
00157
00158 return searchStatus;
00159
00160 }
00161
00162
00163 int DTT0::maxCellsPerLayer() const {
00164 return 2000;
00165 }
00166
00167
00168 int DTT0::getSorted( int wheelId,
00169 int stationId,
00170 int sectorId,
00171 int slId,
00172 int layerId,
00173 int cellId,
00174 float& t0mean,
00175 float& t0rms,
00176 DTTimeUnits::type unit ) const {
00177
00178 int lCode;
00179 int seqId;
00180 std::vector<int>& sequenceLay = *sequencePtr;
00181
00182 if ( ( seqId = DTSequentialLayerNumber::id( wheelId, stationId, sectorId,
00183 slId, layerId ) ) < 0 ) {
00184
00185
00186
00187 std::vector<int> chanKey;
00188 chanKey.reserve(6);
00189 chanKey.push_back( wheelId );
00190 chanKey.push_back( stationId );
00191 chanKey.push_back( sectorId );
00192 chanKey.push_back( slId );
00193 chanKey.push_back( layerId );
00194
00195
00196
00197
00198
00199
00200
00201 if ( sortedLayers->find( chanKey.begin(), chanKey.end(), lCode ) )
00202 return 1;
00203 }
00204 else {
00205 lCode = sequenceLay[seqId];
00206 }
00207 int mCells = maxCellsPerLayer();
00208 int idprev = lCode / mCells;
00209 int length = lCode % mCells;
00210 int idnext = idprev + length;
00211
00212 --idprev;
00213 int idtest;
00214 while ( ( length = idnext - idprev ) >= 2 ) {
00215 idtest = idprev + ( length / 2 );
00216 int cCell = dataList[idtest].first.cellId;
00217 if ( cCell < cellId ) {
00218 idprev = idtest;
00219 continue;
00220 }
00221 if ( cCell > cellId ) {
00222 idnext = idtest;
00223 continue;
00224 }
00225 idprev = idtest++;
00226 idnext = idtest;
00227 const DTT0Data& data( dataList[idprev].second );
00228 t0mean = data.t0mean;
00229 t0rms = data.t0rms;
00230 if ( unit == DTTimeUnits::ns ) {
00231 t0mean *= nsPerCount;
00232 t0rms *= nsPerCount;
00233 }
00234 return 0;
00235 }
00236
00237 return 1;
00238 }
00239
00240
00241 int DTT0::get( const DTWireId& id,
00242 float& t0mean,
00243 float& t0rms,
00244 DTTimeUnits::type unit ) const {
00245 return get( id.wheel(),
00246 id.station(),
00247 id.sector(),
00248 id.superLayer(),
00249 id.layer(),
00250 id.wire(),
00251 t0mean, t0rms, unit );
00252 }
00253
00254
00255 float DTT0::unit() const {
00256 return nsPerCount;
00257 }
00258
00259
00260 const
00261 std::string& DTT0::version() const {
00262 return dataVersion;
00263 }
00264
00265
00266 std::string& DTT0::version() {
00267 return dataVersion;
00268 }
00269
00270
00271 void DTT0::clear() {
00272 delete dBuf;
00273 dBuf = 0;
00274 dataList.clear();
00275 return;
00276 }
00277
00278
00279 int DTT0::set( int wheelId,
00280 int stationId,
00281 int sectorId,
00282 int slId,
00283 int layerId,
00284 int cellId,
00285 float t0mean,
00286 float t0rms,
00287 DTTimeUnits::type unit ) {
00288
00289 if ( unit == DTTimeUnits::ns ) {
00290 t0mean /= nsPerCount;
00291 t0rms /= nsPerCount;
00292 }
00293
00294 if ( dBuf == 0 ) cacheMap();
00295
00296
00297
00298
00299 if ( dBuf == 0 ) cacheMap();
00300 std::vector<int> chanKey;
00301 chanKey.reserve(6);
00302 chanKey.push_back( wheelId );
00303 chanKey.push_back( stationId );
00304 chanKey.push_back( sectorId );
00305 chanKey.push_back( slId );
00306 chanKey.push_back( layerId );
00307 chanKey.push_back( cellId );
00308
00309 int ientry;
00310
00311
00312
00313 int searchStatus = dBuf->find( chanKey.begin(), chanKey.end(), ientry );
00314
00315 if ( !searchStatus ) {
00316
00317 DTT0Data& data( dataList[ientry].second );
00318 data.t0mean = t0mean;
00319 data.t0rms = t0rms;
00320 return -1;
00321 }
00322 else {
00323 DTT0Id key;
00324 key. wheelId = wheelId;
00325 key.stationId = stationId;
00326 key. sectorId = sectorId;
00327 key. slId = slId;
00328 key. layerId = layerId;
00329 key. cellId = cellId;
00330 DTT0Data data;
00331 data.t0mean = t0mean;
00332 data.t0rms = t0rms;
00333 ientry = dataList.size();
00334 dataList.push_back( std::pair<const DTT0Id,DTT0Data>( key, data ) );
00335
00336 dBuf->insert( chanKey.begin(), chanKey.end(), ientry );
00337 return 0;
00338 }
00339
00340 return 99;
00341
00342 }
00343
00344
00345 int DTT0::set( const DTWireId& id,
00346 float t0mean,
00347 float t0rms,
00348 DTTimeUnits::type unit ) {
00349 return set( id.wheel(),
00350 id.station(),
00351 id.sector(),
00352 id.superLayer(),
00353 id.layer(),
00354 id.wire(),
00355 t0mean, t0rms, unit );
00356 }
00357
00358
00359 void DTT0::setUnit( float unit ) {
00360 nsPerCount = unit;
00361 }
00362
00363
00364 void DTT0::sortData() {
00365 if ( sortedLayers != 0 ) return;
00366 if ( dBuf == 0 ) checkOrder();
00367 if ( sortedLayers != 0 ) return;
00368 if ( dBuf == 0 ) return;
00369 std::vector< std::pair<DTT0Id,DTT0Data> > tempList;
00370
00371
00372
00373 std::vector<int> indexList = dBuf->contList();
00374 std::vector<int>::const_iterator iter = indexList.begin();
00375 std::vector<int>::const_iterator iend = indexList.end();
00376 while ( iter != iend ) tempList.push_back( dataList[*iter++] );
00377 dataList = tempList;
00378 delete dBuf;
00379 dBuf = 0;
00380 return;
00381 }
00382
00383
00384 DTT0::const_iterator DTT0::begin() const {
00385 return dataList.begin();
00386 }
00387
00388
00389 DTT0::const_iterator DTT0::end() const {
00390 return dataList.end();
00391 }
00392
00393
00394 std::string DTT0::mapName() const {
00395 std::stringstream name;
00396 name << dataVersion << "_map_T0" << this;
00397 return name.str();
00398 }
00399
00400
00401 bool DTT0::checkOrder() const {
00402
00403 delete sortedLayers;
00404 delete sequencePtr;
00405
00406 sortedLayers = new DTBufferTree<int,int>;
00407
00408
00409 sequencePtr = new std::vector<int>( DTSequentialLayerNumber::max() + 2 );
00410 std::vector<int>::iterator iter = sequencePtr->begin();
00411 std::vector<int>::iterator iend = sequencePtr->end();
00412 while ( iter != iend ) *iter++ = 0;
00413
00414 int entryNum = 0;
00415 int mCells = maxCellsPerLayer();
00416 int entryMax = dataList.size();
00417
00418 std::vector<int> chanOld;
00419 int lCell = -999999999;
00420 bool layerOrder = true;
00421
00422 int lCode;
00423 int seqId;
00424 int oldId = 0;
00425 std::vector<int>& sequenceLay = *sequencePtr;
00426
00427 std::vector<int> chanKey;
00428 chanKey.reserve(6);
00429
00430 while ( entryNum < entryMax ) {
00431 const DTT0Id& chan = dataList[entryNum].first;
00432 if ( ( seqId = DTSequentialLayerNumber::id( chan. wheelId,
00433 chan.stationId,
00434 chan. sectorId,
00435 chan. slId,
00436 chan. layerId ) ) < 0 ) {
00437 oldId = 0;
00438
00439
00440
00441
00442 chanKey.clear();
00443 chanKey.push_back( chan. wheelId );
00444 chanKey.push_back( chan.stationId );
00445 chanKey.push_back( chan. sectorId );
00446 chanKey.push_back( chan. slId );
00447 chanKey.push_back( chan. layerId );
00448
00449
00450
00451 if ( sortedLayers->find( chanKey.begin(), chanKey.end(), lCode ) ) {
00452
00453
00454
00455 sortedLayers->insert( chanKey.begin(), chanKey.end(),
00456 1 + ( entryNum * mCells ) );
00457 chanOld = chanKey;
00458 }
00459 else {
00460
00461 int offset = lCode / mCells;
00462 int length = lCode % mCells;
00463 int ncells = entryNum - offset;
00464
00465
00466
00467
00468 if ( ( ncells >= mCells ) ||
00469 ( ncells != length ) ||
00470 ( chanKey[0] != chanOld[0] ) ||
00471 ( chanKey[1] != chanOld[1] ) ||
00472 ( chanKey[2] != chanOld[2] ) ||
00473 ( chanKey[3] != chanOld[3] ) ||
00474 ( chanKey[4] != chanOld[4] ) ||
00475 ( chan.cellId <= lCell ) ) layerOrder = false;
00476 sortedLayers->insert( chanKey.begin(), chanKey.end(),
00477 ( offset * mCells ) + ncells + 1 );
00478 }
00479 }
00480 else {
00481 chanOld.clear();
00482
00483 lCode = sequenceLay[seqId];
00484 if ( lCode == 0 ) {
00485 sequenceLay[seqId] = 1 + ( entryNum * mCells );
00486 oldId = seqId;
00487 }
00488 else {
00489 int offset = lCode / mCells;
00490 int length = lCode % mCells;
00491 int ncells = entryNum - offset;
00492 if ( ( ncells >= mCells ) ||
00493 ( ncells != length ) ||
00494 ( seqId != oldId ) ||
00495 ( chan.cellId <= lCell ) ) layerOrder = false;
00496 sequenceLay[seqId] = ( offset * mCells ) + ncells + 1;
00497 }
00498 }
00499 lCell = chan.cellId;
00500 entryNum++;
00501 }
00502 if ( !layerOrder ) cacheMap();
00503 return layerOrder;
00504 }
00505
00506
00507 void DTT0::cacheMap() const {
00508
00509 delete sortedLayers;
00510 sortedLayers = 0;
00511
00512 dBuf = new DTBufferTree<int,int>;
00513
00514 int entryNum = 0;
00515 int entryMax = dataList.size();
00516 std::vector<int> chanKey;
00517 chanKey.reserve(6);
00518 while ( entryNum < entryMax ) {
00519
00520 const DTT0Id& chan = dataList[entryNum].first;
00521
00522
00523
00524
00525 chanKey.clear();
00526 chanKey.push_back( chan. wheelId );
00527 chanKey.push_back( chan.stationId );
00528 chanKey.push_back( chan. sectorId );
00529 chanKey.push_back( chan. slId );
00530 chanKey.push_back( chan. layerId );
00531 chanKey.push_back( chan. cellId );
00532 dBuf->insert( chanKey.begin(), chanKey.end(), entryNum++ );
00533
00534 }
00535
00536 return;
00537
00538 }
00539