ZddReconAlg Class Reference

#include <ZddReconAlg.h>

List of all members.

Public Member Functions

 ZddReconAlg (const std::string &name, ISvcLocator *pSvcLocator)
StatusCode initialize ()
StatusCode execute ()
StatusCode finalize ()

Private Member Functions

double getEK (int chId)
int zddDataStat (const Event::ZddEvent *zddEvt, int evtId)
int calFragEandT (const ZddFragment &frag, int &efrag, int &tfrag)

Private Attributes

bool m_errStat
bool m_errQuit


Detailed Description

Definition at line 8 of file ZddReconAlg.h.


Constructor & Destructor Documentation

ZddReconAlg::ZddReconAlg ( const std::string name,
ISvcLocator *  pSvcLocator 
)

Definition at line 13 of file ZddReconAlg.cxx.

References m_errQuit.

00013                                                                         :
00014     Algorithm(name, pSvcLocator),
00015     m_errStat(false)
00016 {
00017     declareProperty("BreakWithError", m_errQuit = true);
00018 }


Member Function Documentation

int ZddReconAlg::calFragEandT ( const ZddFragment frag,
int &  efrag,
int &  tfrag 
) [private]

Definition at line 284 of file ZddReconAlg.cxx.

References genRecEmupikp::i, if(), ZddFragment::length, max, min, ZddFragment::sample, and ZddFragment::start_index.

00285 {
00286     int minIndex = 0;
00287     unsigned char min = 255, max = 0;
00288     for ( int i = 0; i < frag.length; ++i ) {
00289         unsigned char& sample = frag.sample[i];
00290         if ( sample < min ) {
00291             min = sample;
00292             minIndex = i;
00293         }
00294         if ( sample > max ) {
00295             max = sample;
00296         }
00297     }
00298 
00299     efrag = max - min;
00300     tfrag = 8160 - 2 * (minIndex + frag.start_index);
00301 
00302     return 0;
00303 }

StatusCode ZddReconAlg::execute (  ) 

Definition at line 30 of file ZddReconAlg.cxx.

References RecZddChannel::addFragment(), Bes_Common::DEBUG, calibUtil::ERROR, Bes_Common::FATAL, getEK(), genRecEmupikp::i, Bes_Common::INFO, m_errQuit, m_errStat, msgSvc(), EventModel::Recon::RecZddChannelCol, RecZddChannel::setBaseLine(), RecZddChannel::setChannelId(), RecZddChannel::setPhase(), RecZddChannel::setScanCode(), deljobs::start, Bes_Common::WARNING, and zddDataStat().

00031 {
00032     MsgStream log(msgSvc(), name());
00033     log << MSG::INFO << "in execute()" << endreq;
00034 
00035     // Part 1: Get the event header, print out event and run number
00036     SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event/EventHeader");
00037     if (!eventHeader) {
00038         log << MSG::FATAL << "Could not find Event Header" << endreq;
00039         return StatusCode::FAILURE;
00040     }
00041     log << MSG::DEBUG << "Retrieved event: " << eventHeader->eventNumber()
00042         << "  run: " << eventHeader->runNumber() << endreq;
00043 
00044     // Part 2: Register RecZddChannel to TDS
00045     DataObject* aRecEvent = 0;
00046     eventSvc()->findObject("/Event/Recon", aRecEvent);
00047     if ( aRecEvent == 0 ) {
00048         aRecEvent = new ReconEvent();
00049         StatusCode sc = eventSvc()->registerObject("/Event/Recon", aRecEvent);
00050         if ( sc.isFailure() ) {
00051             log << MSG::FATAL << "Could not register ReconEvent" << endreq;
00052             return StatusCode::FAILURE;
00053         }
00054     }
00055 
00056     RecZddChannelCol* recZddCol = new RecZddChannelCol;
00057     StatusCode sc = eventSvc()->registerObject(EventModel::Recon::RecZddChannelCol, recZddCol);
00058     if ( sc.isFailure() ) {
00059         log << MSG::FATAL << "Could not register RecZddChannelCol" << endreq;
00060         return StatusCode::FAILURE;
00061     }
00062 
00063     // Part 3: check the ZDD data
00064     // 3.1: some errors happened before, ignore the rest calculations
00065     if ( m_errStat ) return StatusCode::SUCCESS;
00066 
00067     // 3.2: check the status of the current ZDD event
00068     SmartDataPtr<Event::ZddEvent> zddEvt(eventSvc(),"/Event/ZddEvent");
00069     int zddCheck = zddDataStat(zddEvt.ptr(), eventHeader->eventNumber()+1);
00070 
00071     if ( zddCheck != 0 ) {
00072         if ( zddCheck < 0 ) {
00073             // serious error happened, ignore all of the rest ZDD data
00074             m_errStat = true;
00075 
00076             if ( m_errQuit ) { // whether to break the data processing
00077                 return StatusCode::FAILURE;
00078             }
00079         }
00080         // else:
00081         // problems in the current event, only ignore this event
00082         return StatusCode::SUCCESS;
00083     }
00084 
00085     // Part 4: Get event T0
00086     double bes3_t0 = -10000.0;
00087     SmartDataPtr<RecEsTimeCol> evTimeCol(eventSvc(), "/Event/Recon/RecEsTimeCol");
00088     if ( !evTimeCol || evTimeCol->size() == 0 ) {
00089         log << MSG::WARNING << " Could not find RecEsTimeCol" << endreq;
00090         //return StatusCode::SUCCESS;
00091     }
00092     else {
00093         RecEsTimeCol::iterator iter_evt = evTimeCol->begin();
00094         if (iter_evt != evTimeCol->end()) {
00095             bes3_t0 = (*iter_evt)->getTest();
00096             //std::cout << "BESIII t0: " << bes3_t0 << std::endl; //wangyadi
00097             bes3_t0 = 6400 - bes3_t0;  // T_L1 - T_collision
00098         }
00099     }
00100 
00101     // Part 5: ZDD data processing
00102     const Event::ZddEvent::Channels& chs = zddEvt->channels();
00103     //cout << "ZDD has n channel: " << chs.size() << endl;
00104     // loop the channels
00105     Event::ZddEvent::Channels::const_iterator end_ch = chs.end();
00106     for ( Event::ZddEvent::Channels::const_iterator it = chs.begin(); it != end_ch; ++it ) {
00107         const ZddChannel* pch = *it;
00108         const ZddChannel::Fragments& frags = pch->fragments();
00109         //cout << "Channel " << pch->getChId() << "  has " << frags.size() << " fragments: " << endl;
00110 
00111         RecZddChannel* recZddCh = new RecZddChannel;
00112         recZddCh->setChannelId(pch->getChId());
00113         recZddCh->setScanCode(pch->getScanCode());
00114         double e_K = getEK(pch->getChId());
00115 
00116         // loop the fragments, rebuild the original channel waveform into one buffer
00117         int maxSamples = 800;                                         // This value may change!
00118         unsigned char waveform[800];
00119         memset(waveform, 0, maxSamples);
00120         ZddChannel::Fragments::const_iterator end_fg = frags.end();
00121         bool quit_event = false;
00122         int start = 0;
00123         for ( ZddChannel::Fragments::const_iterator jt = frags.begin(); jt != end_fg; ++jt) {
00124             const ZddFragment& frag = *jt;
00125             //cout << "\tindex from " << frag.start_index << ",\t length " << frag.length << ":\t ";
00126             //for ( int i = 0; i < frag.length; ++i ) {
00127             //    cout << int(frag.sample[i]) << " ";
00128             //}
00129             //cout << endl;
00130             start = frag.start_index;
00131 
00132             // check for CAEN data corruption ( absent from 2013 onward ?? )
00133             if ( start+frag.length > maxSamples ) {
00134                 recZddCh->setScanCode( 2*10 + pch->getScanCode() );
00135                 MsgStream log(msgSvc(), name());
00136                 log << MSG::ERROR << "ZDD BAD DATA: CAEN corruption problem" << endreq;
00137                 quit_event = true; // abandon this event...
00138                 break;             // ...starting from this channel
00139             }
00140 
00141             for ( int i = 0; i < frag.length; ++i ) waveform[start++] = frag.sample[i];
00142         }
00143 
00144         // then reclusterize with a slightly higher threshold
00145         unsigned char threshold = 20;                                 // threshold for reclustering
00146         unsigned char rephaseThreshold = 40;                          // threshold for waveform rephasing
00147         unsigned char minSample = 255, maxSample = -1;
00148         int maxTime = -1;
00149         bool closed = true;                                           // no cluster is running yet
00150         int phases[4] = {-1,-1,-1,-1};                                // a 4-channel histogram
00151         for ( int pt=0; pt<maxSamples; pt++ ) {
00152             bool notZero = waveform[pt]>0;
00153             bool smaller = waveform[pt] < minSample;
00154             if ( notZero && smaller ) minSample = waveform[pt];       // find baseline for whole channel
00155             if ( waveform[pt] > threshold ) {
00156                 if ( closed ) {                                       // start a new cluster, initialize max and index
00157                   closed    = false;
00158                   maxSample = waveform[pt];
00159                   maxTime   = pt;
00160                 } else {                                              // continue the old cluster, update max and index
00161                   if ( waveform[pt] > maxSample ) {
00162                       maxSample = waveform[pt];
00163                       maxTime   = pt;
00164                   }
00165                 }
00166             } else {
00167                 if ( ! closed ) {                                     // close the old cluster and store it
00168                     closed = true;
00169                     double tNsec = 2.*maxTime;
00170                     recZddCh->addFragment(tNsec, maxSample*e_K);        // time relative to start of FADC window
00171                     // std::cout << "       ZDD E & T: " << int(maxSample) << ", " << maxTime << std::endl;
00172                     if ( maxSample > rephaseThreshold ) {             // most peaks are at multiples of 8ns
00173                         int phase = maxTime%4;                        // only one of these bins will be most used
00174                         phases[phase]++;                              // but we do not know which one yet
00175                     }
00176                 }
00177             }
00178         }
00179         if ( ! closed ) {                                             // close and store the last cluster if still running
00180             closed = true;
00181             double tNsec = 2.*maxTime;        // time relative to start of FADC window
00182             recZddCh->addFragment(tNsec, maxSample*e_K);
00183             // std::cout << "       ZDD E & T: " << int(maxSample) << ", " << maxTime << std::endl;
00184             if ( maxSample > rephaseThreshold ) {
00185                 int phase = maxTime%4;
00186                 phases[phase]++;
00187             }
00188         }
00189 
00190         // Compute channel phase
00191         int mostProb = -1;
00192         int chPhase  = -1;
00193         for (int ph=0; ph<4; ph++) {
00194             if ( phases[ph] > mostProb ) {
00195                 mostProb = phases[ph];
00196                 chPhase = ph;
00197             }
00198         }
00199 
00200         if ( chPhase==-1 ) {// mark channel as not rephasable (no samples above rephaseThreshold)
00201           recZddCh->setScanCode( 3*10 + pch->getScanCode() );
00202           chPhase = -2;
00203         }
00204 
00205         recZddCh->setBaseLine(minSample);
00206         recZddCh->setPhase(chPhase);
00207         recZddCol->push_back(recZddCh);
00208 
00209 /* Previous code, left for reference
00210         // loop the fragments
00211         ZddChannel::Fragments::const_iterator end_fg = frags.end();
00212         for ( ZddChannel::Fragments::const_iterator jt = frags.begin(); jt != end_fg; ++jt) {
00213             const ZddFragment& frag = *jt;
00214             int efrag = -1, tfrag = -1;
00215             calFragEandT(frag, efrag, tfrag);
00216             recZdd->addFragment(bes3_t0-tfrag, efrag*e_K);
00217         }
00218 */
00219 
00220         if ( quit_event ) break; // abandon the event entirely after a CAEN data corruption instance
00221 
00222     } // end of loop on channels
00223     return StatusCode::SUCCESS;
00224 }

StatusCode ZddReconAlg::finalize (  ) 

Definition at line 227 of file ZddReconAlg.cxx.

References Bes_Common::INFO, and msgSvc().

00228 {
00229     MsgStream log(msgSvc(), name());
00230     log << MSG::INFO << "in finalize()" << endreq;
00231 
00232     return StatusCode::SUCCESS;
00233 }

double ZddReconAlg::getEK ( int  chId  )  [private]

Definition at line 235 of file ZddReconAlg.cxx.

Referenced by execute().

00236 {
00237     return 1.0/16.0;
00238 }

StatusCode ZddReconAlg::initialize (  ) 

Definition at line 21 of file ZddReconAlg.cxx.

References Bes_Common::INFO, and msgSvc().

00022 {
00023     MsgStream log(msgSvc(), name());
00024     log << MSG::INFO << "in initialize()" << endreq;
00025 
00026     return StatusCode::SUCCESS;
00027 }

int ZddReconAlg::zddDataStat ( const Event::ZddEvent zddEvt,
int  evtId 
) [private]

Definition at line 240 of file ZddReconAlg.cxx.

References Event::ZddEvent::boards(), Bes_Common::FATAL, and msgSvc().

Referenced by execute().

00241 {
00242     MsgStream log(msgSvc(), name());
00243 
00244     if ( !zddEvt ) {
00245         log << MSG::FATAL << "Could not find ZddEvent" << endreq;
00246         return 1; /*-1;*/
00247     }
00248 
00249     // check the ZDD trigger number
00250     const std::vector<ZddBoard*>& boards = zddEvt->boards();
00251     if ( boards.size() != 2 ) {
00252         log << MSG::FATAL << "incomplete ZDD data, no more ZDD data this run!" << endreq;
00253         return -2;
00254     }
00255 
00256     if ( boards[0]->getCounter() != evtId || boards[1]->getCounter() != evtId )
00257     {
00258         log << MSG::FATAL << "missaligned ZDD triggers, no more ZDD data this run!" << endreq;
00259         return -3;
00260     }
00261 
00262 /* This check is not useful anymore
00263     // check the Trigger Time Tag
00264     static unsigned int bd1_tt = boards[0]->getTimeTag() - 1026;
00265     static unsigned int bd2_tt = boards[1]->getTimeTag() - 1026;
00266 
00267     unsigned int tt0 = bd1_tt;
00268     unsigned int tt1 = bd2_tt;
00269     bd1_tt = boards[0]->getTimeTag();
00270     bd2_tt = boards[1]->getTimeTag();
00271 
00272     if ( tt0 > bd1_tt ) tt0 -= 0x80000000;
00273     if ( tt1 > bd2_tt ) tt1 -= 0x80000000;
00274 
00275     if ( (bd1_tt-tt0) < 1026 || (bd2_tt-tt1) < 1026 ) {
00276         log << MSG::INFO << "overlaped ZDD triggers" << endreq;
00277         return 1;
00278     }
00279 */
00280     return 0;
00281 }


Member Data Documentation

bool ZddReconAlg::m_errQuit [private]

Definition at line 25 of file ZddReconAlg.h.

Referenced by execute(), and ZddReconAlg().

bool ZddReconAlg::m_errStat [private]

Definition at line 24 of file ZddReconAlg.h.

Referenced by execute().


Generated on Tue Nov 29 23:36:22 2016 for BOSS_7.0.2 by  doxygen 1.4.7