00001
00012 #include "St_pp2pp_Maker.h"
00013 #include "StRtsTable.h"
00014 #include "TDataSetIter.h"
00015 #include "RTS/src/DAQ_PP2PP/pp2pp.h"
00016
00017 #include "St_db_Maker/St_db_Maker.h"
00018
00019 #include "tables/St_pp2ppPedestal_Table.h"
00020 #include "tables/St_pp2ppOffset_Table.h"
00021 #include "tables/St_pp2ppZ_Table.h"
00022
00023 #include "StEvent/StEvent.h"
00024 #include "StEvent/StRpsCollection.h"
00025 #include "StEvent/StRpsCluster.h"
00026
00027 #include "StEvent/StTriggerData2009.h"
00028
00029 using namespace std;
00030
00031 ClassImp(St_pp2pp_Maker)
00032
00033 St_pp2pp_Maker::St_pp2pp_Maker(const char *name) : StRTSBaseMaker("pp2pp",name),
00034 mPedestalPerchannelFilename("pedestal.in.perchannel"), mLDoCluster(kTRUE) {
00035
00036
00037 }
00038
00039
00040 St_pp2pp_Maker::~St_pp2pp_Maker() {
00041 }
00042
00043
00045 Int_t St_pp2pp_Maker::Init() {
00046 mLastSvx = ErrorCode;
00047 mLastChain = ErrorCode;
00048 mLastSeq = ErrorCode ;
00049 return StMaker::Init();
00050 }
00051
00052 Int_t St_pp2pp_Maker::InitRun(int runumber) {
00053 if ( mLDoCluster ) {
00054 readPedestalPerchannel() ;
00055 readOffsetPerplane() ;
00056 readZPerplane() ;
00057 }
00058 return kStOk ;
00059 }
00060
00061 Int_t St_pp2pp_Maker::readPedestalPerchannel() {
00062
00063
00064
00065
00066
00067 memset(mPedave,0,sizeof(mPedave));
00068 memset(mPedrms,0,sizeof(mPedrms));
00069
00070 Int_t s, c, sv, ch, idb = 0 ;
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 TDataSet *DB = 0;
00087 DB = GetInputDB("Calibrations/pp2pp");
00088 if (!DB) {
00089 LOG_ERROR << "ERROR: cannot find database Calibrations_pp2pp?" << endm ;
00090 }
00091 else {
00092
00093 St_pp2ppPedestal *descr = 0;
00094 descr = (St_pp2ppPedestal*) DB->Find("pp2ppPedestal");
00095
00096 if (descr) {
00097 pp2ppPedestal_st *table = descr->GetTable();
00098
00099 for ( idb = 0; idb < descr->GetNRows(); idb++ ) {
00100 s = (Int_t) table[idb].sequencer ;
00101 c = (Int_t) table[idb].chain ;
00102 sv = (Int_t) table[idb].SVX ;
00103 ch = (Int_t) table[idb].channel ;
00104
00105 if ( s > 0 ) {
00106 mPedave[s-1][c][sv][ch] = table[idb].mean ;
00107 mPedrms[s-1][c][sv][ch] = table[idb].rms ;
00108 }
00109
00110
00111 }
00112 } else {
00113 LOG_ERROR << "St_pp2pp_Maker: No data in pp2ppPedestal table (wrong timestamp?). Nothing to return, then." << endm ;
00114 }
00115 }
00116
00117 LOG_DEBUG << idb << " pedestal entries read from DB table Calibration/pp2pp read. " << endm ;
00118
00119
00120 return kStOk ;
00121
00122 }
00123
00124 Int_t St_pp2pp_Maker::readOffsetPerplane() {
00125
00126 mOffsetTable = 0;
00127
00128 TDataSet *DB = 0;
00129 DB = GetInputDB("Geometry/pp2pp");
00130 if (!DB) {
00131 LOG_ERROR << "ERROR: cannot find database Geometry_pp2pp?" << std::endl;
00132 }
00133 else {
00134
00135
00136 St_pp2ppOffset *descr = 0;
00137 descr = (St_pp2ppOffset*) DB->Find("pp2ppOffset");
00138
00139 if (descr) {
00140 mOffsetTable = descr->GetTable();
00141 LOG_DEBUG << "Reading pp2ppOffset table with nrows = " << descr->GetNRows() << endm ;
00142
00143
00144
00145
00146
00147
00148
00149 } else {
00150 LOG_ERROR << "St_pp2pp_Maker : No data in pp2ppOffset table (wrong timestamp?). Nothing to return, then" << endm ;
00151 }
00152
00153 }
00154
00155 return kStOk ;
00156
00157 }
00158
00159
00160 Int_t St_pp2pp_Maker::readZPerplane() {
00161
00162 mZTable = 0;
00163
00164 TDataSet *DB = 0;
00165 DB = GetInputDB("Geometry/pp2pp");
00166 if (!DB) {
00167 LOG_ERROR << "ERROR: cannot find database Geometry_pp2pp?" << std::endl;
00168 }
00169 else {
00170
00171
00172 St_pp2ppZ *descr = 0;
00173 descr = (St_pp2ppZ*) DB->Find("pp2ppZ");
00174
00175 if (descr) {
00176 mZTable = descr->GetTable();
00177 LOG_DEBUG << "Reading pp2ppZ table with nrows = " << descr->GetNRows() << endm ;
00178
00179
00180
00181
00182
00183
00184
00185 } else {
00186 LOG_ERROR << "St_pp2pp_Maker : No data in pp2ppZ table (wrong timestamp?). Nothing to return, then" << endm ;
00187 }
00188
00189 }
00190
00191 return kStOk ;
00192
00193 }
00194
00195
00196
00197
00199 void St_pp2pp_Maker::Clear(Option_t *) {
00200
00201
00202 for ( Int_t i=0; i<kMAXSEQ; i++)
00203 for ( Int_t j=0; j<kMAXCHAIN; j++)
00204 (mValidHits[i][j]).clear();
00205
00206 StMaker::Clear();
00207
00208 }
00209
00210
00211
00213 Int_t St_pp2pp_Maker::Make(){
00214
00215
00216
00217
00218
00219 if ( Token() == 0 )
00220 return kStOK ;
00221
00222
00223
00224
00225
00226
00227 int counter = -1;
00228
00229 TGenericTable *pp2ppRawHits = new TGenericTable("pp2ppRawHit_st","pp2ppRawHits");
00230
00231
00232
00233 while ( GetNextAdc() ) {
00234 counter++;
00235 TGenericTable::iterator iword = DaqDta()->begin();
00236 for (;iword != DaqDta()->end();++iword) {
00237 pp2pp_t &d = *(pp2pp_t *)*iword;
00238
00239 DoerPp2pp(d,*pp2ppRawHits);
00240 if ( counter == 0 ) mSiliconBunch = d.bunch_xing ;
00241 }
00242 }
00243
00244
00245 if (counter < 0) {
00246 LOG_DEBUG << "There was no pp2pp data for this event. " << endm;
00247 } else {
00248 LOG_DEBUG << "End of pp2pp data for this event : " << GetEventNumber() << ", Total = " << counter+1
00249 << " records were found" << endm;
00250 }
00251
00252 AddData(pp2ppRawHits);
00253
00254
00255
00256
00257 if ( mLDoCluster ) {
00258
00259 for ( Int_t i=0; i<kMAXSEQ; i++)
00260 for ( Int_t j=0; j<kMAXCHAIN; j++) {
00261 sort( (mValidHits[i][j]).begin(), (mValidHits[i][j]).end(), hitcompare);
00262
00263 }
00264
00265 MakeClusters();
00266
00267 }
00268
00269 return kStOK;
00270
00271 }
00272
00273
00275 Int_t St_pp2pp_Maker::DoerPp2pp(const pp2pp_t &d, TGenericTable &hitsTable) {
00276
00277 pp2ppRawHit_st oneSihit = {0};
00278 oneSihit.sec = Sector() ;
00279 oneSihit.sequencer = d.seq_id ;
00280 oneSihit.chain = d.chain_id ;
00281 oneSihit.svx = d.svx_id ;
00282
00283
00284 HitChannel onehit ;
00285
00286
00287
00288
00289
00290 if ( (oneSihit.svx != mLastSvx) && (mLastSvx != ErrorCode) ) {
00291
00292 if ( Int_t(oneSihit.svx-1) != mLastSvx )
00293
00294 if ( ( (oneSihit.svx-mLastSvx) != -3 && ( (oneSihit.chain%2)==1 ) ) ||
00295 ( (oneSihit.svx-mLastSvx) != -5 && ( (oneSihit.chain%2)==0 ) ) ) {
00296
00297 if ( oneSihit.svx == 7 && oneSihit.sequencer == 3 && oneSihit.chain == 2 )
00298 oneSihit.svx = 3 ;
00299
00300 else if ( oneSihit.svx < mLastSvx && ( GetRunNumber()<10185015 || (mLastSeq!=2 && mLastChain!=2)) ) {
00301
00302 LOG_WARN << "Decreased ? " << GetEventNumber() << " : mLastSeq = " << mLastSeq << ", mLastChain = " << mLastChain << ", mLastSvx = " << mLastSvx << endm ;
00303 LOG_WARN << "Decreased ? " << GetEventNumber() << " : Now, seq = " << (int) oneSihit.sequencer << ", chain = " << (int) oneSihit.chain << ", svx = " << (int) oneSihit.svx << endm ;
00304
00305 oneSihit.svx = mLastSvx + 1 ;
00306
00307 LOG_WARN << "Decreased ? : So -> " << " svx is now = " << (int) oneSihit.svx << endm ;
00308
00309 }
00310
00311 else if ( GetRunNumber()<10185015 || ( mLastSeq!=2 && mLastChain!=2 ) ) {
00312
00313 LOG_WARN << GetEventNumber() << " : mLastSeq = " << mLastSeq << ", mLastChain = " << mLastChain << ", mLastSvx = " << mLastSvx << endm ;
00314 LOG_WARN << GetEventNumber() << " : Now, seq = " << (int) oneSihit.sequencer << ", chain = " << (int) oneSihit.chain << ", svx = " << (int) oneSihit.svx << endm ;
00315
00316 }
00317
00318 }
00319
00320
00321 }
00322 else if ( (oneSihit.chain==mLastChain) && (mLastChain != ErrorCode) ) {
00323 LOG_WARN << "Repeated ? :" << GetEventNumber() << " : mLastSeq = " << mLastSeq << ", mLastChain = " << mLastChain << ", mLastSvx = " << mLastSvx << endm ;
00324 LOG_WARN << "Repeated ? : " << GetEventNumber() << " : Now, seq = " << (int) oneSihit.sequencer << ", chain = " << (int) oneSihit.chain << ", svx = " << (int) oneSihit.svx << endm ;
00325
00326 oneSihit.svx = mLastSvx + 1 ;
00327
00328 LOG_WARN << "Repeated : So -> " << " svx is now = " << (int) oneSihit.svx << endm ;
00329 }
00330
00331
00332 mRpStatus[oneSihit.sequencer - 1] = d.bunch_xing ;
00333
00334 mLastSeq = oneSihit.sequencer;
00335 mLastChain = oneSihit.chain;
00336 mLastSvx = oneSihit.svx;
00337
00338
00339
00340 for(unsigned int c=0;c<sizeof(d.adc);c++) {
00341
00342
00343 if ( d.trace[c] == 1 ) {
00344 oneSihit.channel = c ;
00345 oneSihit.adc = d.adc[c];
00346 hitsTable.AddAt(&oneSihit);
00347
00348
00349
00350 if ( mLDoCluster && (c != 127) && (c != 0) ) {
00351
00352
00353 onehit.first = mLastSvx*(kMAXSTRIP-2) + oneSihit.channel - 1 ;
00354
00355 onehit.second = oneSihit.adc - mPedave[mLastSeq-1][mLastChain][mLastSvx][oneSihit.channel] ;
00356
00357 if ( onehit.second > 5*mPedrms[mLastSeq-1][mLastChain][mLastSvx][oneSihit.channel] ) {
00358 (mValidHits[mLastSeq-1][mLastChain]).push_back(onehit);
00359
00360 }
00361 }
00362
00363 }
00364 else if ( d.trace[c] != 0 )
00365 std::cout << GetEventNumber() << " : trace = " << (Int_t) d.trace[c] << ", Seq " << (Int_t) oneSihit.sequencer
00366 << ", chain " << (Int_t) oneSihit.chain << ", SVX " << (Int_t) oneSihit.svx << ", channel " << c
00367 << " is duplicated ? ==> " << (Int_t) d.adc[c] << std::endl ;
00368 }
00369
00370 return kStOk;
00371
00372 }
00373
00374 Int_t St_pp2pp_Maker::MakeClusters() {
00375
00376
00377
00379 const short orientations[kMAXCHAIN*kMAXSEQ] = {-1,1,-1,1, 1,-1,1,-1, 1,1,1,1, -1,-1,-1,-1, -1,-1,-1,-1, 1,1,1,1, -1,1,-1,1, 1,-1,1,-1 };
00381 const double zcoordinates[kMAXSEQ] = { -55.496, -55.496, -58.496, -58.496, 55.496, 55.496, 58.496, 58.496 };
00382
00384 const short EW[kMAXSEQ] = { 0, 0, 0, 0, 1, 1, 1, 1 } ;
00385 const short VH[kMAXSEQ] = { 1, 1, 0, 0, 1, 1, 0, 0 } ;
00386 const short UDOI[kMAXSEQ] = { 1, 0, 0, 1, 1, 0, 1, 0 } ;
00387
00388 Bool_t is_candidate_to_store ;
00389
00390 Int_t NCluster_Length, Diff_Bunch ;
00391 Double_t ECluster, POStimesE, position, offset ;
00392
00393 StTriggerData* trg_p = 0 ;
00395 TObjectSet *os = (TObjectSet*)GetDataSet("StTriggerData");
00396 if (os) {
00397 trg_p = (StTriggerData*)os->GetObject();
00398 }
00399
00400
00402 StRpsCollection * pp2ppColl = new StRpsCollection();
00403
00405 pp2ppColl->setSiliconBunch(mSiliconBunch) ;
00406
00407 vector< HitChannel >::iterator it, it_next ;
00408
00409 for ( Int_t i=0; i<kMAXSEQ; i++)
00410 for ( Int_t j=0; j<kMAXCHAIN; j++) {
00411
00412
00413
00414
00415 if(trg_p){
00416
00417 pp2ppColl->romanPot(i)->setAdc( (u_int) trg_p->pp2ppADC( (StBeamDirection) EW[i],VH[i],UDOI[i],0),
00418 (u_int) trg_p->pp2ppADC( (StBeamDirection) EW[i],VH[i],UDOI[i],1) );
00419 pp2ppColl->romanPot(i)->setTac( (u_int) trg_p->pp2ppTAC( (StBeamDirection) EW[i],VH[i],UDOI[i],0),
00420 (u_int) trg_p->pp2ppTAC( (StBeamDirection) EW[i],VH[i],UDOI[i],1) );
00421
00422
00423 Diff_Bunch = mRpStatus[i] - trg_p->bunchId7Bit() ;
00424 if ( Diff_Bunch < 0 ) Diff_Bunch += 120 ;
00425 pp2ppColl->romanPot(i)->setStatus( (unsigned char) Diff_Bunch ) ;
00426
00427 }
00428 else
00429 LOG_WARN << "No StTriggerData ?! " << endm ;
00430
00431
00432 NCluster_Length = 0 ;
00433 ECluster = 0 ;
00434 POStimesE = 0 ;
00435
00436 if ( mZTable )
00437 pp2ppColl->romanPot(i)->plane(j)->setZ( mZTable[0].rp_z_plane[4*i+j] ) ;
00438 else
00439 pp2ppColl->romanPot(i)->plane(j)->setZ(zcoordinates[i]) ;
00440
00441 if ( mOffsetTable )
00442 offset = mOffsetTable[0].rp_offset_plane[4*i+j]/1000. ;
00443 else
00444 offset = double(ErrorCode) ;
00445
00446
00447 pp2ppColl->romanPot(i)->plane(j)->setOffset( offset ) ;
00448
00449 pp2ppColl->romanPot(i)->plane(j)->setOrientation( orientations[4*i+j] ) ;
00450
00451 it = (mValidHits[i][j]).begin() ;
00452
00453 while ( it != (mValidHits[i][j]).end() ) {
00454
00455
00456 NCluster_Length++ ;
00457 ECluster += it->second ;
00458 POStimesE += it->first*it->second ;
00459
00460 it_next = it + 1 ;
00461
00462 is_candidate_to_store = kFALSE ;
00463
00464
00465 if ( it_next != (mValidHits[i][j]).end() ) {
00466
00467
00468 if ( (it_next->first - it->first)!=1 )
00469 is_candidate_to_store = kTRUE ;
00470
00471 }
00472 else {
00473 is_candidate_to_store = kTRUE ;
00474 }
00475
00476 if ( is_candidate_to_store == kTRUE ) {
00477
00478
00479
00480
00481 StRpsCluster * oneStCluster = new StRpsCluster() ;
00482
00483 oneStCluster->setEnergy(ECluster);
00484 oneStCluster->setLength(NCluster_Length);
00485 if ( (j % 2) == 0 )
00486 position = POStimesE/ECluster*9.74E-5 ;
00487 else
00488 position = POStimesE/ECluster*1.050E-4;
00489
00490
00491 oneStCluster->setPosition(position);
00492
00493 oneStCluster->setXY( offset + orientations[4*i+j]*position ) ;
00494
00495 pp2ppColl->romanPot(i)->plane(j)->addCluster(oneStCluster);
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505 ECluster = 0 ;
00506 POStimesE = 0 ;
00507 NCluster_Length = 0 ;
00508
00509 }
00510
00511 it++ ;
00512
00513 }
00514
00515 }
00516
00517
00518 mEvent = (StEvent *) GetInputDS("StEvent");
00519 if ( mEvent ) {
00520
00521 mEvent->setRpsCollection(pp2ppColl);
00522 }
00523 else
00524 LOG_ERROR << "St_pp2pp_Maker : StEvent not found !" << endm ;
00525
00526 return kStOk ;
00527
00528 }
00529
00530
00531 Int_t St_pp2pp_Maker::Finish() {
00532
00533
00534 return StMaker::Finish();
00535
00536 }
00537
00538