CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/L1Trigger/DTBti/src/DTBtiChip.cc

Go to the documentation of this file.
00001 //-------------------------------------------------
00002 //
00003 //   Class: DTBtiChip
00004 //
00005 //   Description: Implementation of DTBtiChip 
00006 //                trigger algorithm
00007 //
00008 //
00009 //   Author List:
00010 //   C. Grandi
00011 //   Modifications: 
00012 //   S. Vanini
00013 //   30/IX/03 SV : wire dead time = ST added
00014 //   22/VI/04 SV : last trigger code update
00015 //   15/I/07  SV : new DTConfig setup
00016 //   17/III/07 SV : distp2 truncation bug fixed 
00017 //--------------------------------------------------
00018 
00019 //-----------------------
00020 // This Class's Header --
00021 //-----------------------
00022 #include "L1Trigger/DTBti/interface/DTBtiChip.h"
00023 
00024 //-------------------------------
00025 // Collaborating Class Headers --
00026 //-------------------------------
00027 #include "L1Trigger/DTBti/interface/DTBtiHit.h"
00028 #include "L1Trigger/DTBti/interface/DTBtiTrig.h"
00029 
00030 #include <DataFormats/MuonDetId/interface/DTChamberId.h>
00031 #include <DataFormats/MuonDetId/interface/DTLayerId.h>
00032 #include <DataFormats/MuonDetId/interface/DTSuperLayerId.h>
00033 #include "DataFormats/MuonDetId/interface/DTWireId.h"
00034 
00035 #include <DataFormats/DTDigi/interface/DTDigiCollection.h>
00036 //---------------
00037 // C++ Headers --
00038 //---------------
00039 #include <iostream>
00040 #include <cmath>
00041 
00042 using namespace std;
00043 
00044 //----------------
00045 // Constructors --
00046 //----------------
00047 
00048 DTBtiChip::DTBtiChip(DTBtiCard* card, DTTrigGeom* geom, int supl, int n, DTConfigBti* conf): _card(card), _geom(geom), _config(conf) {
00049 
00050 
00051  // original constructor
00052  setSnap();
00053  reSumSet(); 
00054 
00055  // Debugging...
00056   if(config()->debug()>2){
00057     cout << "DTBtiChip constructor called for BTI number " << n;
00058     cout << " in superlayer " << supl << endl;
00059   }
00060 
00061   // reserve the appropriate amount of space for vectors
00062   int i=0;
00063   for(i=0;i<DTConfig::NSTEPL - DTConfig::NSTEPF;i++) {
00064     _trigs[i].reserve(2);
00065   }
00066 
00067   for(i=0;i<9;i++) {
00068     _digis[i].reserve(10);
00069     _hits[i].reserve(10);
00070   }
00071 
00072   //SV wire dead time init
00073   int DEAD = config()->DEADpar();
00074   for(int cell=1; cell<=9; cell++){
00075     _busyStart_clock[cell-1] = - DEAD -1;
00076   }
00077 
00078   // Identifier
00079   DTChamberId sid = _geom->statId();
00080   _id = DTBtiId(sid, supl, n);
00081 
00082   //if(config()->trigSetupGeom() == 0){
00083     // set K acceptance in theta view for this BTI: 6 bit resolution....  
00084     _MinKAcc = 0;
00085     _MaxKAcc = 63;
00086         
00087 /*         DTBtiId _id1 = DTBtiId(sid,supl,1);
00088            
00089            cout <<"superlayer" << _id.superlayer()<< "BTI1   " <<  _id1.bti()  << " BTICur " << _id.bti()<< endl;
00090            cout <<endl;
00091            GlobalPoint gp1 = _geom->CMSPosition(_id1);
00092            cout << "pos of BTI "<<  _id1.bti()  << gp1 <<endl;
00093            // K of tracks from vertex
00094            GlobalPoint gp = CMSPosition();
00095            cout << "pos of BTI" << _id.bti()  << gp <<endl;        
00096            cout << endl ; */
00097            
00098            
00099 //     // theta bti acceptance cut is in bti chip (no traco in theta!)
00100 //     // acceptance from orca geom: bti theta angle in CMS frame +-2 in K units 
00101 //     if(_id.superlayer()==2){
00102 //       float distp2 = (int)(2*_geom->cellH()*config()->ST()/_geom->cellPitch());
00103 //       float K0 = config()->ST();
00104 
00105 // /*      DTBtiId _id1 = DTBtiId(sid,supl,1);
00106       
00107 //       cout << "BTI1   " <<  _id1.bti() << endl;
00108 //       cout << "BTICur " << _id.bti() <<endl;
00109 //       GlobalPoint gp1 = _geom->CMSPosition(_id1);
00110 //       cout << "pos of BTI 1 " << gp1 <<endl;*/
00111           
00112 //       // K of tracks from vertex
00113 //       GlobalPoint gp = CMSPosition();
00114 //       if(config()->debug()>3){
00115 //         cout << "Position: R=" << gp.perp() << "cm, Phi=" << gp.phi()*180/3.14159;
00116 //         cout << " deg, Z=" << gp.z() << " cm" << endl;
00117 //       }
00118 //       // CB TEST WITH NEW GEOMETRY
00119 //       // new geometry: modified wrt old due to specularity of theta SLs (still to understand on wheel zero) 19/06/06
00120 //       float theta;
00121 //       if (_id.wheel()==0){ 
00122 //      if(_id.sector()%4>1) theta = atan( gp.z()/gp.perp() );
00123 //      else theta = atan( -gp.z()/gp.perp() );
00124 //       }
00125 //       else theta = atan( fabs(gp.z())/gp.perp() );
00126 //       // .11 =TAN(6.3 deg) ==> k=2 (e' ancora vero? forse questa parte va aggiornata sena ripassare per gli angoli) 19/6/06
00127 //       float thetamin = theta-config()->KAccTheta()*0.055;
00128 //       float thetamax = theta+config()->KAccTheta()*0.055;
00129 
00130 //       float fktmin = tan(thetamin)*distp2 + K0;
00131 //       int ktmin = (fktmin>0) ? (int)(fktmin+0.5) : (int)(fktmin-0.5);
00132 //       float fktmax = tan(thetamax)*distp2 + K0;
00133 //       int ktmax = (fktmax>0) ? (int)(fktmax+0.5) : (int)(fktmax-0.5);
00134 // //      float fkbti = -gp.z()/gp.perp()*distp2;
00135 // //      int kbti = (fkbti>0) ? (int)(fkbti+0.5) : (int)(fkbti-0.5);
00136 // //      // K acceptance to point to vertex
00137 // //      int ktmin = kbti-config()->KAccTheta();  // minimum
00138 // //      int ktmax = kbti+config()->KAccTheta();  // maximum
00139 //       if(ktmin>_MinKAcc)_MinKAcc=ktmin;
00140 //       if(ktmax<_MaxKAcc)_MaxKAcc=ktmax;
00141 //     }
00142 
00143 //     // debugging
00144 //     if(config()->debug()>2){
00145 //       cout << "CMS position:" << CMSPosition() << endl;
00146 //       cout << "K acceptance:" << _MinKAcc << "," << _MaxKAcc << endl;
00147 //     }
00148 //     // end debugging
00149 // theta bti acceptance cut is in bti chip (no traco in theta!)
00150     // acceptance is determined about BTI angle wrt vertex with programmable value 
00151     if(_id.superlayer()==2){
00152       // 091105 SV theta bti trigger angular acceptance in CMSSW is computed from geometry 
00153       // (theta of the bti) +- a given tolerance config()->KAccTheta(): tolerance NOT in 
00154       // hardware configuration. The hw tolerance is given in the system and the 
00155       // overall acceptance is computed "before" data (CH,CL) is given to the MC
00156       // or written in the DB. No way to "extract" the tolerance from DB yet.
00157 
00158       if(_card->useAcceptParamFlag()==0){
00159 
00160         //float distp2 = (int)(2*_geom->cellH()*config()->ST()/_geom->cellPitch());   SV fix 17/III/07
00161         float distp2 = 2*_geom->cellH()*config()->ST()/_geom->cellPitch();
00162         float K0 = config()->ST();
00163 
00164         // position of BTI 1 and of current one
00165         DTBtiId _id1 = DTBtiId(sid,supl,1);
00166         GlobalPoint gp1 = _geom->CMSPosition(_id1); 
00167         GlobalPoint gp = CMSPosition();
00168         if(config()->debug()>3){
00169                 cout << "Position: R=" << gp.perp() << "cm, Phi=" << gp.phi()*180/3.14159;
00170                 cout << " deg, Z=" << gp.z() << " cm" << endl;
00171         }
00172         // new geometry: modified wrt old due to specularity of theta SLs --> fixed 6/9/06 
00173         float theta;
00174         if(gp1.z() < 0.0) 
00175                 theta = atan( -(gp.z())/gp.perp() );                            
00176         else 
00177                 theta = atan( (gp.z())/gp.perp() );
00178 
00179         // set BTI acceptance window : fixed wrt ORCA on 6/9/06  
00180         float fktmin = tan(theta)*distp2+K0 ;
00181         int ktmin = static_cast<int>(fktmin)-config()->KAccTheta();
00182         float fktmax = tan(theta)*distp2+K0+1;
00183         int ktmax = static_cast<int>(fktmax)+config()->KAccTheta();
00184         if(ktmin>_MinKAcc)
00185                 _MinKAcc=ktmin;
00186         if(ktmax<_MaxKAcc)
00187                 _MaxKAcc=ktmax;
00188       }
00189       // 091105 SV acceptance is taken simply from CH, CL parameters
00190       else {
00191         _MinKAcc = config()->CL();
00192         _MaxKAcc = config()->CH();
00193       } 
00194 
00195       // debugging
00196       if(config()->debug()>2){
00197         cout << "CMS position:" << CMSPosition() << endl;
00198         cout << "K acceptance (theta view):" << _MinKAcc << "," << _MaxKAcc  << endl;
00199       }// end debugging
00200          
00201     }//end theta acceptance computation 
00202 
00203   //}// end if trigSetupGeom=0
00204 
00205 
00206   //SV flag for initialization....
00207   init_done = 0; 
00208 
00209 }
00210 
00211 
00212 DTBtiChip::DTBtiChip(const DTBtiChip& bti) :
00213   _geom(bti._geom), _id(bti._id),
00214   _curStep(bti._curStep), _nStepUsedHits(bti._nStepUsedHits){
00215 
00216   setSnap();
00217   reSumSet();
00218 
00219   int i=0;
00220   for(i=0;i<DTConfig::NSTEPL - DTConfig::NSTEPF;i++) {
00221     _trigs[i].reserve(2);
00222     vector<DTBtiTrig*>::const_iterator p;
00223     for(p=bti._trigs[i].begin();p<bti._trigs[i].end();p++){
00224       _trigs[i].push_back(*p);
00225     }
00226   }
00227   for(i=0;i<9;i++) {
00228     _digis[i].reserve(10);
00229     vector<const DTDigi*>::const_iterator p;
00230     for(p=bti._digis[i].begin();p<bti._digis[i].end();p++){
00231       _digis[i].push_back(*p);
00232     }
00233     _hits[i].reserve(10);
00234     vector<DTBtiHit*>::const_iterator p1;
00235     for(p1=bti._hits[i].begin();p1<bti._hits[i].end();p1++){
00236       _hits[i].push_back(*p1);
00237     }
00238     _thisStepUsedTimes[i] = bti._thisStepUsedTimes[i];
00239     _thisStepUsedHit[i] = _thisStepUsedHit[i];
00240   }
00241   for(i=0;i<25;i++){
00242     _sums[i] = bti._sums[i];
00243     _difs[i] = bti._difs[i];
00244   }
00245   for(i=0;i<26;i++){
00246     int j = 0;
00247     for(j=0;j<6;j++){
00248       _Keq[i][j] = bti._Keq[i][j];
00249     }
00250     for(j=0;j<3;j++){
00251       _JTR[i][j] = bti._JTR[i][j];
00252     }
00253     for(j=0;j<2;j++){
00254       _Xeq[i][j] = bti._Xeq[i][j];
00255       _KTR[i][j] = bti._KTR[i][j];
00256     }
00257   }
00258   _MinKAcc = bti._MinKAcc;
00259   _MaxKAcc = bti._MaxKAcc;
00260 
00261 
00262 
00263 }
00264 //--------------
00265 // Destructor --
00266 //--------------
00267 DTBtiChip::~DTBtiChip(){
00268   clear();
00269 }
00270 
00271 //--------------
00272 // Operations --
00273 //--------------
00274 DTBtiChip& 
00275 DTBtiChip::operator=(const DTBtiChip& bti) {
00276   if(this != &bti){
00277     _geom = bti._geom;
00278     _id = bti._id;
00279     _curStep = bti._curStep;
00280     _nStepUsedHits = bti._nStepUsedHits;
00281     int i=0;
00282     for(i=0;i<DTConfig::NSTEPL - DTConfig::NSTEPF;i++) {
00283       _trigs[i].reserve(2);
00284       vector<DTBtiTrig*>::const_iterator p;
00285       for(p=bti._trigs[i].begin();p<bti._trigs[i].end();p++){
00286         _trigs[i].push_back(*p);
00287       }
00288     }
00289     for(i=0;i<9;i++) {
00290       _digis[i].reserve(10);
00291       vector<const DTDigi*>::const_iterator p;
00292       for(p=bti._digis[i].begin();p<bti._digis[i].end();p++){
00293         _digis[i].push_back(*p);
00294       }
00295       _hits[i].reserve(10);
00296       vector<DTBtiHit*>::const_iterator p1;
00297       for(p1=bti._hits[i].begin();p1<bti._hits[i].end();p1++){
00298         _hits[i].push_back(*p1);
00299       }
00300       _thisStepUsedTimes[i] = bti._thisStepUsedTimes[i];
00301       _thisStepUsedHit[i] = _thisStepUsedHit[i];
00302     }
00303     for(i=0;i<25;i++){
00304       _sums[i] = bti._sums[i];
00305       _difs[i] = bti._difs[i];
00306     }
00307     for(i=0;i<26;i++){
00308       int j = 0;
00309       for(j=0;j<6;j++){
00310         _Keq[i][j] = bti._Keq[i][j];
00311       }
00312       for(j=0;j<3;j++){
00313         _JTR[i][j] = bti._JTR[i][j];
00314       }
00315       for(j=0;j<2;j++){
00316         _Xeq[i][j] = bti._Xeq[i][j];
00317         _KTR[i][j] = bti._KTR[i][j];
00318       }
00319     }
00320     _MinKAcc = bti._MinKAcc;
00321     _MaxKAcc = bti._MaxKAcc;
00322   }
00323   return *this;
00324 }
00325 
00326 void 
00327 DTBtiChip::add_digi(int cell, const DTDigi* digi) {
00328 
00329   if(_id.bti()<1 || _id.bti() >_geom->nCell(superlayer()))return;
00330   if(cell<1 || cell>9){
00331     cout << "DTBtiChip::add_digi : wrong cell number: " << cell;
00332     cout << ". Digi not added!" << endl;
00333     return;
00334   }
00335 
00336   int DEAD = config()->DEADpar();
00337   float stepTimeTdc = DTBtiHit::_stepTimeTdc;
00338 
00339 
00340   if( int(digi->countsTDC()/stepTimeTdc) - _busyStart_clock[cell-1] > DEAD ){
00341     _busyStart_clock[cell-1] = int(digi->countsTDC()/stepTimeTdc);
00342     _digis[cell-1].push_back(digi);
00343 
00344     // debugging
00345     if(config()->debug()>1){
00346     cout << "DTBtiChip::add_digi: DTBtiChip # " <<_id.bti() <<
00347     " cell " << cell << " --> drift time (tdc units)= " << digi->countsTDC()<< endl;
00348     digi->print();
00349     }
00350   }
00351   else {
00352   // debugging
00353   if(config()->debug()>1)
00354     cout << "DTBtiChip::add_digi: DTBtiChip # " <<_id.bti() <<
00355      " cell " << cell << " in dead time -> digi not added! " << endl;
00356   }
00357 
00358 }
00359 
00360 
00361 void 
00362 DTBtiChip::add_digi_clock(int cell, int digi) {
00363 
00364   if(cell<1 || cell>9){
00365     cout << "DTBtiChip::add_digi_clock : wrong cell number: " << cell;
00366     cout << ". Digi not added!" << endl;
00367     return;
00368   }
00369 
00370   int DEAD = config()->DEADpar();
00371 
00372   if( digi - _busyStart_clock[cell-1] > DEAD ){
00373     _busyStart_clock[cell-1] = digi;
00374     _digis_clock[cell-1].push_back(digi);
00375     // debugging
00376     if(config()->debug()>1)
00377       cout << "DTBtiChip::add_digi_clock: DTBtiChip # " <<number() <<
00378       " cell " << cell << " --> clock time = " << digi << endl;
00379   }
00380   else{
00381   // debugging
00382   if(config()->debug()>1)
00383     cout << "DTBtiChip::add_digi_clock: DTBtiChip # " << number() <<
00384      " cell " << cell << " in dead time -> digi not added! " << endl;
00385   }
00386 }
00387 
00388 
00389 int
00390 DTBtiChip::nCellHit() const {
00391   int n=0;
00392   int i=0;
00393   for(i=0;i<9;i++) {
00394     if( _digis[i].size() >0 ) n++;
00395   }
00396   if(config()->debug()>2) {
00397     cout << n << " cells with hits found:" << endl;
00398   }
00399   if(config()->debug()>2) {
00400     for(i=0;i<9;i++) {
00401       vector<const DTDigi*>::const_iterator p;
00402       for(p=_digis[i].begin();p<_digis[i].end();p++) {
00403         cout << "DTBtiChip # " << 
00404         _id.bti() << 
00405         " cell " << i+1;
00406         cout << " --> drift time (tdc units): " << (*p)->countsTDC() << endl;
00407         (*p)->print();
00408       }
00409     }
00410   }
00411   return n;
00412 /*
00413  //SV 2/IV/03 counting hits from _hits
00414   int n=0;
00415   int i=0;
00416   for(i=0;i<9;i++) {
00417     if( _hits[i].size() >0 ) n++;
00418   }
00419   if(config()->debug()>2) {
00420     cout << n << " cells with hits found:" << endl;
00421   }
00422   if(config()->debug()>2) {
00423     for(i=0;i<9;i++) {
00424       vector<const DTBtiHit*>::const_iterator p;
00425       for(p=_hits[i].begin();p<_hits[i].end();p++) {
00426         cout << "DTBtiChip # " << 
00427         number() << 
00428         " cell " << i+1;
00429         if((*p)->curTime()!=4000)
00430           cout << " --> drift time: " << (*p)->curTime() << endl;
00431         else
00432           cout << " --> clock time: " << (*p)->clockTime() << endl;
00433       }
00434     }
00435   }
00436   return n;
00437 */
00438 }
00439 
00440 void 
00441 DTBtiChip::addTrig(int step, DTBtiTrig* btitrig) { 
00442   if(step>=DTConfig::NSTEPF&&step<=DTConfig::NSTEPL){
00443     if(config()->debug()>3) 
00444       cout << "DTBtiChip: adding trigger..." <<endl;
00445     _trigs[step-DTConfig::NSTEPF].push_back(btitrig);
00446   } else {
00447     if(config()->debug()>3){    
00448       cout << "DTBtiChip::addTrig: step " << step ;
00449       cout << " outside range. Trigger not added" << endl;
00450     }
00451   } 
00452 }
00453 
00454 int
00455 DTBtiChip::nTrig(int step) const {
00456   if(step<DTConfig::NSTEPF||step>DTConfig::NSTEPL){
00457     cout << "DTBtiChip::nTrig: step out of range: " << step ;
00458     cout << " 0 returned" << endl;
00459     return 0;
00460   }
00461   return _trigs[step-DTConfig::NSTEPF].size(); 
00462 }
00463 
00464 vector<DTBtiTrig*>
00465 DTBtiChip::trigList(int step) const {
00466   if(step<DTConfig::NSTEPF||step>DTConfig::NSTEPL){
00467     cout << "DTBtiChip::trigList: step out of range: " << step ;
00468     cout << " empty pointer returned" << endl;
00469     //return 0;
00470   } 
00471   return _trigs[step-DTConfig::NSTEPF]; 
00472 }
00473 
00474 DTBtiTrig*
00475 DTBtiChip::trigger(int step, unsigned n) const {
00476   if(step<DTConfig::NSTEPF||step>DTConfig::NSTEPL){
00477     cout << "DTBtiChip::trigger: step out of range: " << step ;
00478     cout << " empty pointer returned" << endl;
00479     return 0;
00480   } 
00481   if(n<1 || n>_trigs[step-DTConfig::NSTEPF].size()) {
00482     cout << "DTBtiChip::trigger: requested trigger does not exist: " << n;
00483     cout << " empty pointer returned!" << endl;
00484     return 0;
00485   }
00486   vector<DTBtiTrig*>::const_iterator p = _trigs[step-DTConfig::NSTEPF].begin();
00487   return (*(p+n-1));
00488 }
00489 
00490 DTBtiTrigData
00491 DTBtiChip::triggerData(int step, unsigned n) const {
00492   if(step<DTConfig::NSTEPF||step>DTConfig::NSTEPL){
00493     cout << "DTBtiChip::triggerData: step out of range: " << step ;
00494     cout << " dummy trigger returned" << endl;
00495     return DTBtiTrigData();
00496   } 
00497   if(n<1 || n>_trigs[step-DTConfig::NSTEPF].size()) {
00498     cout << "DTBtiChip::triggerData: requested trig. doesn't exist: " << n;
00499     cout << " dummy trigger returned!" << endl;
00500     return DTBtiTrigData();
00501   }
00502   vector<DTBtiTrig*>::const_iterator p = _trigs[step-DTConfig::NSTEPF].begin();
00503   return (*(p+n-1))->data();
00504 }
00505 
00506 void
00507 DTBtiChip::eraseTrigger(int step, unsigned n) {
00508   if(step<DTConfig::NSTEPF||step>DTConfig::NSTEPL){
00509     cout << "DTBtiChip::eraseTrigger: step out of range: " << step ;
00510     cout << " trigger not deleted!" << endl;
00511   } 
00512   if(n<1 || n>_trigs[step-DTConfig::NSTEPF].size()) {
00513     cout << "DTBtiChip::trigger: requested trigger does not exist: " << n;
00514     cout << " trigger not deleted!" << endl;
00515   }
00516   vector<DTBtiTrig*>::iterator p = _trigs[step-DTConfig::NSTEPF].begin()+n-1;
00517   if(&(*p))delete (*p);
00518   _trigs[step-DTConfig::NSTEPF].erase(p);
00519 }
00520 
00521 void
00522 DTBtiChip::clear() {
00523 
00524   if(config()->debug()>3)
00525     cout << "DTBtiChip::clear()" << endl;
00526 
00527   for(int c=0;c<9;c++) {
00528 
00529     _digis[c].clear();
00530     _digis_clock[c].clear();
00531 
00532     vector<DTBtiHit*>::iterator p;
00533     for(p=_hits[c].begin();p<_hits[c].end();p++){
00534       delete (*p);
00535     }
00536     _hits[c].clear();
00537   }
00538 
00539   vector<DTBtiTrig*>::iterator p1;
00540   for(int is=0;is<DTConfig::NSTEPL-DTConfig::NSTEPF+1;is++){
00541     for(p1=_trigs[is].begin();p1<_trigs[is].end();p1++){
00542       delete (*p1);
00543     }
00544     _trigs[is].clear();
00545   }
00546 }
00547 
00548 void
00549 DTBtiChip::init() {
00550 
00551   if(config()->debug()>3)
00552     cout << "DTBtiChip::init() -> initializing bti chip" << endl;
00553 
00554   _curStep=0;
00555   for(int i=0;i<25;i++) {
00556     _sums[i] = 1000;
00557     _difs[i] = 1000;
00558   } 
00559 
00560   for(int cell=0;cell<9;cell++) {
00561     int WEN = config()->WENflag(cell+1);
00562     if( WEN==1 ){
00563       _thisStepUsedHit[cell]=0;
00564       vector<const DTDigi*>::const_iterator p;
00565       for(p=_digis[cell].begin();p<_digis[cell].end();p++){
00566         DTBtiHit* hit = new DTBtiHit(*p,config());
00567         //int clockTime = (int)(fabs(((*p)->time()+config()->SetupTime())/12.5));
00568         //DTBtiHit* hit = new DTBtiHit(clockTime,config());
00569         _hits[cell].push_back(hit);
00570       }
00571 
00572       //debugging
00573       if(config()->debug()>2){
00574         vector<DTBtiHit*>::const_iterator p1;
00575         for(p1=_hits[cell].begin();p1<_hits[cell].end();p1++){
00576                 cout << " Filling hit in cell " << cell+1;
00577                 if((*p1)->curTime()!=4000) 
00578                   cout << " raw time in trigger: " << (*p1)->curTime() << endl;
00579                 cout << " time (clock units): " << (*p1)->clockTime() << endl; 
00580         }
00581       }
00582       // end debugging
00583     }
00584   }
00585 }
00586 
00587 
00588 void
00589 DTBtiChip::init_clock() {
00590 
00591   if(config()->debug()>3)
00592     cout << "DTBtiChip::init_clock() -> initializing bti chip" << endl;
00593 
00594   init_done = 1;
00595   _curStep=0;
00596 
00597   for(int i=0;i<25;i++) {
00598     _sums[i] = 1000;
00599     _difs[i] = 1000;
00600   } 
00601 
00602   for(int cell=0;cell<9;cell++) {
00603     int WEN = config()->WENflag(cell+1);
00604     if( WEN==1 ){
00605       _thisStepUsedHit[cell]=0;
00606     for(unsigned int i=0; i<_digis_clock[cell].size(); i++){
00607       const int clockTime = (_digis_clock[cell])[i];
00608       DTBtiHit* hit = new DTBtiHit(clockTime,config());
00609       _hits[cell].push_back(hit);
00610     }
00611         
00612     //debugging
00613     if(config()->debug()>2){
00614       vector<DTBtiHit*>::const_iterator p1;
00615       for(p1=_hits[cell].begin();p1<_hits[cell].end();p1++){
00616         cout << " Filling hit in cell " << cell+1;
00617         if((*p1)->curTime()!=4000) 
00618           cout << " time: " << (*p1)->curTime() << endl;
00619         else
00620           cout << " time (clock units): " << (*p1)->clockTime() << endl; 
00621         }
00622       }
00623       // end debugging
00624     }
00625   }
00626 }
00627 
00628 
00629 void 
00630 DTBtiChip::run() {
00631 
00632   // Debugging...
00633   if(config()->debug()>2){
00634     cout << "DTBtiChip::run: Processing BTI " << _id.bti() << endl;
00635     cout << " in SL " << _id.superlayer() << endl;
00636   }
00637   // End debugging
00638 
00639   if(_id.bti()<1 || _id.bti() >_geom->nCell(superlayer())) {
00640     if(config()->debug()>1)
00641       cout << "DTBtiChip::run : wrong BTI number: " << _id.bti() << endl;
00642     return;
00643   }
00644 
00645   // run algorithm
00646   if(!init_done)
00647     init();
00648   if( nCellHit()<3 ) return;   // check that at least 3 cell have hits
00649 
00650   for(int ints=0; ints<2*DTConfig::NSTEPL; ints++) { // 80 MHz 
00651     tick(); // Do a 12.5 ns step
00652 
00653     // In electronics equations are computed every 12.5 ns
00654     // but since triggers are searched for only every 25 ns, skip
00655     // also equation's computing at odd values of internal step
00656     if((currentIntStep()/2)*2!=currentIntStep())continue;
00657     //if((currentIntStep()/2)*2==currentIntStep())continue; 
00658 
00659 
00660     if(config()->debug()>2){
00661       cout << "DTBtiChip::run : internal step " << currentIntStep();
00662       cout << " number of JTRIG hits is " << _nStepUsedHits << endl;
00663     }
00664     if(currentStep()>=DTConfig::NSTEPF && _nStepUsedHits>2) { 
00665       // at least 3 good hits in this step -> run algorithm
00666       computeSums();
00667       computeEqs();
00668       findTrig();
00669     }
00670   }
00671   if( config()->LTS()>0 ) doLTS(); // low trigger suppression
00672 }
00673 
00674 void
00675 DTBtiChip::tick() {
00676   //
00677   // fills the DTBtiChip registers ( _thisStepUsedHit[cell] )
00678   // for a given clock (Syncronizer and Shaper functionalities)
00679   //
00680 
00681   _curStep++; // increase internal step (12.5 ns --> 80 MHz)
00682 
00683   // debugging
00684   if(config()->debug()>2){
00685     cout << "DTBtiChip::tick: internal step is now " << currentIntStep()<< endl; 
00686   }
00687   // end debugging
00688 
00689   // Loop on cells
00690   _nStepUsedHits=0;
00691   for(int cell=0;cell<9;cell++) {
00692 
00693     // decrease drift time by 12.5 ns for each hit
00694     vector<DTBtiHit*>::const_iterator p;
00695     for(p=_hits[cell].begin();p<_hits[cell].end();p++){
00696       (*p)->stepDownTime();
00697     }
00698 
00699     // loop on hits
00700     _thisStepUsedHit[cell]=0;
00701     for(p=_hits[cell].begin();p<_hits[cell].end();p++){
00702       if       ( (*p)->isDrifting() ) { // hit is drifting
00703         break;                          //   --> don't consider others
00704       } else if( (*p)->isInsideReg() ) {  // hit is already in registers
00705         _thisStepUsedHit[cell]=(*p);
00706         _nStepUsedHits++;
00707         // debugging
00708         if(config()->debug()>2){
00709           if((*p)->curTime() != 4000)
00710             cout << "DTBtiChip::tick: hit in register: time=" << (*p)->curTime();
00711           else
00712             cout << "DTBtiChip::tick: hit in register! " << endl;
00713           cout <<                           " jtrig=" << (*p)->jtrig() << endl;
00714 
00715         }
00716         // end debugging
00717         break;                          //   --> don't consider other triggers
00718       }
00719       // hit is not drifting and not in registers: it is gone out of register, but
00720       // jtrig value is still=ST ; save in array and consider next one if exists
00721     } // end loop on cell hits
00722 
00723     // debugging...
00724     if(config()->debug()>2){
00725       if(_thisStepUsedHit[cell]!=0){
00726         cout << "int. step=" << currentIntStep() << " cell=" << cell+1;
00727         cout << " jtrig=" << _thisStepUsedHit[cell]->jtrig();
00728         if( _thisStepUsedHit[cell]->curTime() != 4000 )  
00729           cout << " (time=" << _thisStepUsedHit[cell]->curTime() << ")" << endl;
00730         else 
00731           cout << endl;
00732       }
00733     } 
00734     // end debugging
00735 
00736   } // end loop on cells
00737 
00738 }
00739 
00740 void
00741 DTBtiChip::doLTS() {
00742  
00743   if(config()->debug()>2)
00744     cout<<"Do LTS"<<endl;
00745   int lts = config()->LTS();
00746   int nbxlts = config()->SET();
00747 
00748   // Do LTS only on the requested SL
00749   //if (superlayer()==2 && lts==1) return;
00750   //if (superlayer()!=2 && lts==2) return;
00751   //new DTConfig: do LTS only is LTS!=0  --> somewhat redundant !
00752   if (lts==0) return;
00753 
00754   // loop on steps
00755   for(int is=DTConfig::NSTEPF; is<=DTConfig::NSTEPL; is++) {
00756     if(nTrig(is)>0) { // non empty step
00757       if( trigger(is,1)->code()==8 ) { // HTRIG at this step
00758         // do LTS on nbxLTS[superlayer] following steps
00759         for(int js=is+1;(js<=is+nbxlts&&js<=DTConfig::NSTEPL);js++){
00760           if(nTrig(js)>0) { // non empty step
00761             DTBtiTrig* tr = trigger(js,1);
00762             if( tr->code()<8 && (lts==1 || lts==3)) {
00763               if(config()->debug()>3)
00764                 cout<<"LTS: erasing trigger!"<<endl; 
00765               eraseTrigger(js,1); // delete trigger
00766             }
00767           }
00768         }
00769         // do LTS on previous step
00770         if(is>DTConfig::NSTEPF && nTrig(is-1)>0) { // non empty step
00771           DTBtiTrig* tr = trigger(is-1,1);
00772           if( tr->code()<8 && (lts==2 || lts==3) ) {
00773             if(config()->debug()>3)
00774                 cout<<"LTS: erasing trigger!"<<endl;                                
00775             eraseTrigger(is-1,1); // delete trigger
00776           }
00777         }
00778 
00779       }
00780     }
00781   }
00782 }
00783 
00784 int
00785 DTBtiChip::store(const int eq, const int code, const int K, const int X, 
00786                      float KeqAB, float KeqBC, float KeqCD, 
00787                      float KeqAC, float KeqBD, float KeqAD) {
00788 
00789   // remove negative position triggers
00790   if(X<0)return 0;
00791 
00792   
00793   // accept in range triggers (acceptances defined in constructor)
00794   if(K>=_MinKAcc && K<=_MaxKAcc) 
00795   {
00796     int trig_step = currentStep();
00797 
00798 /*
00799     //SV test 27/I/2003 1-clock delay for critical patterns in default ACx configuration 
00800     int AC1 = config()->AccPattAC1(); //default 0
00801     int AC2 = config()->AccPattAC2(); //default 3
00802     int ACH = config()->AccPattACH(); //default 1
00803     int ACL = config()->AccPattACL(); //default 2     
00804 
00805     if(AC1==0 && AC2==3 && ACH==1 && ACL==2){
00806       if(eq==1 || eq==4 || eq==7 || eq==8 || eq==9 || eq==12 || eq==15
00807         || eq==19 || eq==22 || eq==23 || eq==24 || eq==25 || eq==27 )
00808            trig_step = currentStep()+1;
00809     }
00810 */     
00811     //store strobe: SV no strobe defined for this setup SV 15/I/2007
00812     int strobe=-1;
00813 
00814     // create a new trigger
00815     float Keq[6] = {KeqAB,KeqBC,KeqCD,KeqAC,KeqBD,KeqAD};
00816     //DTBtiTrig* trg = new DTBtiTrig(this,code,K,X,currentStep(),eq);
00817     DTBtiTrig* trg = new DTBtiTrig(this,code,K,X,trig_step,eq,strobe,Keq);
00818 
00819     // store also the digis
00820     for(int c=0; c<9; c++) {
00821       if(_thisStepUsedHit[c]) {
00822         const DTDigi* digi = _thisStepUsedHit[c]->hitDigi();
00823         if(digi)
00824           trg->addDigi(digi);
00825       }
00826     }
00827 
00828     //addTrig(currentStep(),trg);
00829     addTrig(trig_step,trg);
00830 
00831     // Debugging...
00832     if(config()->debug()>1)
00833       trg->print();
00834     // end debugging
00835     
00836     return 1;
00837   }
00838   else{
00839     // remove out of range triggers (acceptances defined in constructor)
00840     if(config()->debug()>2){
00841       cout << "DTBtiChip::store, REJECTED TRIGGER at step "<< currentStep();
00842       cout << " allowed K range in theta view is: [";
00843       cout << _MinKAcc << ","<< _MaxKAcc << "]";
00844       cout << "K value is " << K << endl; 
00845     }
00846     return 0;
00847   }//end else
00848 }//end store
00849 
00850 
00851 void
00852 DTBtiChip::setSnap(){
00853 
00854  //set the internally calculated drift velocity parameters
00855   ST43 = config()->ST43();
00856   RE43 = config()->RE43();
00857   ST23 = int(double(ST43)/2.);
00858   RE23 = (RE43==1) ? 2 : int(double(RE43)/2.);
00859 
00860 
00861   ST =  int(  double(ST43) * 3./4. + double(RE43) * 1./4.     );
00862   ST2 = int( (double(ST43) * 3./4. + double(RE43) * 1./4.)*2. );
00863   ST3 = int( (double(ST43) * 3./4. + double(RE43) * 1./4.)*3. );
00864   ST4 = int( (double(ST43) * 3./4. + double(RE43) * 1./4.)*4. );
00865   ST5 = int( (double(ST43) * 3./4. + double(RE43) * 1./4.)*5. );
00866   ST7 = int( (double(ST43) * 3./4. + double(RE43) * 1./4.)*7. );
00867 
00868   if(config()->debug()>3){
00869     cout << "Snap register dump: " << endl;
00870     cout << "ST43 = " << ST43 << endl;
00871     cout << "RE43 = " << RE43 << endl;
00872     cout << "ST23 = " << ST23 << endl;
00873     cout << "RE23 = " << RE23 << endl;
00874     cout << "ST = " << ST << endl;
00875     cout << "ST2 = " << ST2 << endl;
00876     cout << "ST3 = " << ST3 << endl;
00877     cout << "ST4 = " << ST4 << endl;
00878     cout << "ST5 = " << ST5 << endl;
00879     cout << "ST7 = " << ST7 << endl;
00880   }
00881 }
00882