Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

TDragonRun.cxx

Go to the documentation of this file.
00001 //  TDragonRun.cxx.
00020 #include <iostream>
00021 #include <iomanip>
00022 
00023 #include <TMySQLServer.h>
00024 #include <TMySQLResult.h>
00025 #include <TMySQLRow.h>
00026 
00027 #include "TDragonRun.h"
00028 #include "TMidasEvent.h"
00029 #include "TDragonEvent.h"
00030 #include "TScalerEvent.h"
00031 #include "TEpicsEvent.h"
00032 
00033 using std::cout;
00034 using std::cerr;
00035 using std::endl;
00036 using std::hex;
00037 using std::dec;
00038 using std::ios_base;
00039 using std::setw;
00040 using std::setfill;
00041 using std::vector;
00042 using std::stringstream;
00043 
00044 TDragonRun::TDragonRun() : fFeraHeaderBit(0x8000)
00045 {
00050   fDatabase    = "mysql://localhost:3306/Online";
00051   fRootFile    = 0;
00052   fMidasFile   = 0;
00053   fDragonTree  = 0;
00054   fMidasEvent  = new TMidasEvent();
00055   fDragonEvent = new TDragonEvent();
00056   fScalerEvent = new TScalerEvent();
00057   fEpicsEvent  = new TEpicsEvent();
00058   fAdcSlotsH   = 0;
00059   fTdcSlotsH   = 0;
00060   fAdcSlotH    = 0;
00061   fTdcSlotH    = 0; 
00062   fAdcSlotsG   = 0;
00063   fTdcSlotsG   = 0;
00064   fAdcSlotG    = 0;  
00065   fTdcSlotG    = 0;
00066   fHeaderWordG = 0;
00067   fHeaderWordH = 0;
00068   fDebug       = 0;
00069 }
00070 
00071 TDragonRun::TDragonRun(const TDragonRun &rhs) : fFeraHeaderBit(0x8000)
00072 {
00075 }
00076 
00077 TDragonRun::~TDragonRun()
00078 {
00081   for (vector<THiDetector *>::iterator it = fDetectorsH.begin(),
00082          end = fDetectorsH.end(); it < end; it++)
00083     delete *it;
00084   fDetectorsH.clear();
00085   for (vector<TGrDetector *>::iterator it = fDetectorsG.begin(),
00086          end = fDetectorsG.end(); it < end; it++)
00087     delete *it;
00088   fDetectorsG.clear();
00089   if (fMidasFile) delete fMidasFile;
00090   if (fAdcSlotH)  delete [] fAdcSlotH;
00091   if (fTdcSlotH)  delete [] fTdcSlotH;
00092   if (fAdcSlotG)  delete [] fAdcSlotG;
00093   if (fTdcSlotG)  delete [] fTdcSlotG;
00094   delete fMidasEvent;
00095   delete fDragonEvent;
00096   delete fScalerEvent;
00097   delete fEpicsEvent;
00098 }
00099 
00100 TDragonRun & TDragonRun::operator=(const TDragonRun &)
00101 {
00105   return *this;
00106 }
00107 
00108 void TDragonRun::SetHiModules(const UShort_t *adcSlot, const UInt_t &adcN,
00109                               const UShort_t *tdcSlot, const UInt_t &tdcN)
00110 {
00125   if (fAdcSlotH) delete [] fAdcSlotH;
00126   if (fTdcSlotH) delete [] fTdcSlotH;
00127   fAdcSlotH = SetModules(adcSlot, adcN, fAdcSlotsH);
00128   fTdcSlotH = SetModules(tdcSlot, tdcN, fTdcSlotsH);
00129   if (adcN && tdcN)
00130     fHeaderWordH = fFeraHeaderBit | fTdcSlotH[0];
00131 }
00132 
00133 void TDragonRun::SetGrModules(const UShort_t *adcSlot, const UInt_t &adcN,
00134                               const UShort_t *tdcSlot, const UInt_t &tdcN)
00135 {
00150   if (fAdcSlotG) delete [] fAdcSlotG;
00151   if (fTdcSlotG) delete [] fTdcSlotG;
00152   fAdcSlotG = SetModules(adcSlot, adcN, fAdcSlotsG);
00153   fTdcSlotG = SetModules(tdcSlot, tdcN, fTdcSlotsG);
00154   if (adcN && tdcN)
00155     fHeaderWordG = fFeraHeaderBit | fTdcSlotG[0];
00156 }
00157 
00158 UInt_t * TDragonRun::SetModules(const UShort_t *slots,
00159                                 UInt_t slotN, UInt_t &slotBitMap)
00160 {
00173   UInt_t *slotArray = 0;
00174   if (slotN) {
00175     slotArray = new UInt_t[slotN];
00176     for (UInt_t i = 0; i < slotN; i++) {
00177       slotBitMap |= 1 << slots[i] - 1;
00178       slotArray[i] = slots[i];
00179     }
00180   } else {
00181     cerr << "SetModules: ERROR - cannot set slots with size 0," << endl
00182          << "            data will be sorted into empty events" << endl;
00183   }
00184   return slotArray;
00185 }
00186 
00187 Bool_t TDragonRun::HasDetector(const Char_t *name) const
00188 {
00197   for (UInt_t i = 0; i < fDetectorsH.size(); i++)
00198     if (fDetectorsH[i]->IsName(name)) {
00199       cerr << "HasDetector: Detector " << name
00200            << " already added to run"  << endl;
00201       return true;
00202     }
00203   for (UInt_t i = 0; i < fDetectorsG.size(); i++)
00204     if (fDetectorsG[i]->IsName(name)) {
00205       cerr << "HasDetector: Detector " << name
00206            << " already added to run"  << endl;
00207       return true;
00208     }
00209   return false;
00210 }
00211 
00212 void TDragonRun::AddDetector(THiDetector *detector)
00213 {
00221   cout << "AddDetector: Adding detector \""
00222        << detector->GetName() << "\" to run" << endl;
00223   fDetectorsH.push_back(detector);
00224 }
00225 
00226 void TDragonRun::AddDetector(TGrDetector *detector)
00227 {
00235   cout << "AddDetector: Adding detector \""
00236        << detector->GetName() << "\" to run" << endl;
00237   fDetectorsG.push_back(detector);
00238 }
00239 
00240 void TDragonRun::OpenRootFile(const Char_t *rootFile)
00241 {
00249   fRootFile = new TFile(rootFile, "RECREATE");
00250   cout << "OpenRootFile: Opened ROOT output file \""
00251        << rootFile << "\"" << endl;
00252   SetTree();
00253 }
00254 
00255 void TDragonRun::OpenMidasFile(const Char_t *midasFile)
00256 {
00260   fMidasFile = new TMidasFile();
00261   if (! fMidasFile->Open(midasFile)) {
00262     cerr << "OpenMidasFile: Could not open \"" << midasFile << "\"" << endl;
00263     exit(0);
00264   } else {
00265     cout << "OpenMidasFile: Opened MIDAS input file \""
00266          << midasFile << "\"" << endl;
00267   }
00268 }
00269 
00270 void TDragonRun::SetTree()
00271 {
00277   fDragonTree = new TTree("dragonTree", "DragonTree");
00278   fDragonTree->Branch("fDragonEvent", "TDragonEvent", &fDragonEvent, 64000, 99);
00279   fDragonTree->Branch("fScalerEvent", "TScalerEvent", &fScalerEvent, 64000, 99);
00280   fDragonTree->Branch("fEpicsEvent",  "TEpicsEvent",  &fEpicsEvent,  64000, 99);
00281 }
00282 
00283 void TDragonRun::GetModules(vector<UShort_t> *slots,
00284                             const Char_t *usage, const Char_t *type)
00285 {
00294   TMySQLServer connect(fDatabase.c_str(), "dragon", "dragonTail");
00295   TMySQLResult *result = 0;
00296   string sql("SELECT * FROM Modules WHERE `usage` = ");
00297   sql += usage;
00298   sql += " && `type` = ";
00299   sql += type;
00300   sql += ";";
00301   result = static_cast<TMySQLResult *>(connect.Query(sql.c_str()));
00302   if (! result) {
00303     cerr << "GetModules: ERROR - cannot perform query " << sql
00304          << " without database table" << endl;
00305   } else {
00306     for (Int_t i = 0; i < result->GetRowCount(); i++) {
00307       Int_t value;
00308       stringstream field;
00309       TMySQLRow *row = static_cast<TMySQLRow *>(result->Next());
00310       field << row->GetField(1);
00311       field >> value;
00312       slots->push_back(value);
00313       delete row;
00314     }
00315     delete result;
00316   }
00317 }
00318 
00319 TTree * TDragonRun::ReadDragonTree(const Char_t *rootFileName) const
00320 {
00337   TTree *tree = 0;
00338   if (rootFileName != 0) {
00339     TFile *file = new TFile(rootFileName, "READ");
00340     tree = (TTree *)file->Get("dragonTree");
00341   } else
00342     cerr << "ReadDragonTree: ERROR - specify a root file" << endl;
00343   return tree;
00344 }
00345 
00346 void TDragonRun::TestDragonTree(const Char_t *rootFileName) const
00347 {
00367   if (! rootFileName)
00368     rootFileName = fRootFile->GetName();
00369   TTree *tree = ReadDragonTree(rootFileName);
00370   if (tree != 0) {
00371     cout << "TestDragonTree: Testing file \""
00372          << rootFileName << "\"..." << endl;
00373     TDragonEvent *de = new TDragonEvent();
00374     TScalerEvent *se = new TScalerEvent();
00375     TEpicsEvent  *ee = new TEpicsEvent();
00376     tree->SetBranchAddress("fDragonEvent", &de);
00377     tree->SetBranchAddress("fScalerEvent", &se);
00378     tree->SetBranchAddress("fEpicsEvent",  &ee);
00379     Int_t eventsN = static_cast<Int_t>(tree->GetEntries());
00380     Int_t bytesN = 0;
00381     for (Int_t i = 0; i < eventsN; i++) {
00382       bytesN += tree->GetEntry(i);
00383       if (fDebug) {
00384         de->Print();
00385         se->Print();
00386         ee->Print();
00387       }
00388     }
00389     delete de;
00390     delete se;
00391     delete ee;
00392     cout << "TestDragonTree: Tree structure seems OK" << endl;
00393   } else
00394     cout << "TestDragonTree: Could not find root tree" << endl;
00395 }
00396 
00397 Bool_t TDragonRun::UnpackDragonEvent()
00398 {
00407 
00408   //
00409   // number of unsigned shorts in the subevent
00410   //
00411   UShort_t *dataBank = 0;
00412   Short_t triggerMask = fMidasEvent->GetTriggerMask();
00413   fMidasEvent->SwapBytes(0);
00414 
00415   if ((triggerMask & kTRIG_DRAGON_HMEM) == kTRIG_DRAGON_HMEM) {
00416     //
00417     // HMEM subevents --> any number of heavy-ions
00418     //
00419     Int_t bankLen =
00420       fMidasEvent->LocateBank("HMEM", reinterpret_cast<void *>(&dataBank));
00421     if (bankLen > 0 &&
00422         static_cast<UInt_t>(bankLen) < fMidasEvent->GetDataSize()) {
00423       Int_t wordsN = 0;
00424       Int_t startIndex = 0;
00425       TSubEvent subEvent;
00426       do {
00427         if (fDebug)
00428           wordsN = DumpSubEventH(startIndex, dataBank, bankLen);
00429         wordsN = UnpackSubEventH(startIndex, dataBank, bankLen, subEvent);
00430         fDragonEvent->AddHmemSubEvent(subEvent);
00431         startIndex += wordsN;
00432       }
00433       while (wordsN > 0 && startIndex < bankLen - 1);
00434     } else {
00435       cerr << "UnpackDragonEvent: ERROR - HMEM bank length " << dec  << bankLen
00436            << " with event data-size " << fMidasEvent->GetDataSize() << endl;
00437       return false;
00438     }
00439   }
00440   if ((triggerMask & kTRIG_DRAGON_GMEM) == kTRIG_DRAGON_GMEM) {
00441     //
00442     // GMEM subevents --> any number of gamma-rays
00443     //
00444     Int_t bankLen =
00445       fMidasEvent->LocateBank("GMEM", reinterpret_cast<void *>(&dataBank));
00446     if (bankLen > 0 &&
00447         static_cast<UInt_t>(bankLen) < fMidasEvent->GetDataSize()) {
00448       Int_t wordsN = 0;
00449       Int_t startIndex = 0;
00450       TSubEvent subEvent;
00451       do {
00452         if (fDebug)
00453           wordsN = DumpSubEventG(startIndex, dataBank, bankLen);
00454         wordsN = UnpackSubEventG(startIndex, dataBank, bankLen, subEvent);
00455         fDragonEvent->AddGmemSubEvent(subEvent);
00456         startIndex += wordsN;
00457       }
00458       while (wordsN > 0 && startIndex < bankLen - 1);
00459     } else {
00460       cerr << "UnpackDragonEvent: ERROR - GMEM bank length " << dec  << bankLen
00461            << " with event data-size " << fMidasEvent->GetDataSize() << endl;
00462       return false;
00463     }
00464   }
00465   if ((triggerMask & kTRIG_DRAGON_CHME) == kTRIG_DRAGON_CHME) {
00466     //
00467     // CHME subevents --> any number of heavy-ions
00468     //
00469     Int_t bankLen =
00470       fMidasEvent->LocateBank("CHME", reinterpret_cast<void *>(&dataBank));
00471     if (bankLen > 0 &&
00472         static_cast<UInt_t>(bankLen) < fMidasEvent->GetDataSize()) {
00473       Int_t wordsN = 0;
00474       Int_t startIndex = 0;
00475       TSubEvent subEvent;
00476       do {
00477         if (fDebug)
00478           wordsN = DumpSubEventH(startIndex, dataBank, bankLen);
00479         wordsN = UnpackSubEventH(startIndex, dataBank, bankLen, subEvent);
00480         fDragonEvent->AddChmeSubEvent(subEvent);
00481         startIndex += wordsN;
00482       }
00483       while (wordsN > 0 && startIndex < bankLen - 1);
00484     } else {
00485       cerr << "UnpackDragonEvent: ERROR - CHME bank length " << dec  << bankLen
00486            << " with event data-size " << fMidasEvent->GetDataSize() << endl;
00487       return false;
00488     }
00489   }
00490   if ((triggerMask & kTRIG_DRAGON_CGME) == kTRIG_DRAGON_CGME) {
00491     //
00492     // CGME subevents --> any number of gamma-rays
00493     //
00494     Int_t bankLen =
00495       fMidasEvent->LocateBank("CGME", reinterpret_cast<void *>(&dataBank));
00496     if (bankLen > 0 &&
00497         static_cast<UInt_t>(bankLen) < fMidasEvent->GetDataSize()) {
00498       Int_t wordsN = 0;
00499       Int_t startIndex = 0;
00500       TSubEvent subEvent;
00501       do {
00502         if (fDebug)
00503           wordsN = DumpSubEventG(startIndex, dataBank, bankLen);
00504         wordsN = UnpackSubEventG(startIndex, dataBank, bankLen, subEvent);
00505         fDragonEvent->AddCgmeSubEvent(subEvent);
00506         startIndex += wordsN;
00507       }
00508       while (wordsN > 0 && startIndex < bankLen - 1);
00509     } else {
00510       cerr << "UnpackDragonEvent: ERROR - CGME bank length " << dec  << bankLen
00511            << " with event data-size " << fMidasEvent->GetDataSize() << endl;
00512       return false;
00513     }
00514   }
00515   if (triggerMask > 0x000f) {
00516     cerr << "UnpackDragonEvent: ERROR - unknown trigger mask = "
00517          << triggerMask << endl;
00518     return false;
00519   }
00520   return true;
00521 }
00522 
00523 Int_t TDragonRun::UnpackSubEventH(Int_t startIndex, const UShort_t *data,
00524                                   Int_t bankLen, TSubEvent &subEvent) const
00525 {
00540 
00541   //
00542   // statements like if (i + 1 < bankLen) ... i++;
00543   // should be read as "if the next word exists ... move to it"
00544   //
00545   // each subevent starts with a TDC header regardless - i.e. the 3377's
00546   // _always_ give a header on each readout (this is set in a register)
00547   //
00548   Int_t i;
00549   vector<TDataItem<UShort_t> > adcs;
00550   vector<TDataItem<UShort_t> > tdcs;
00551 
00552   for (i = startIndex; (data[i] & 0x80ff) != fHeaderWordH && i < bankLen; i++)
00553     if (i + 1 == bankLen) {
00554       cerr << "UnpackSubEventH: ERROR - no header found" << endl;
00555       return 0;
00556     }
00557   do {
00558     if (i < bankLen && (data[i] & fFeraHeaderBit)) {
00559       UShort_t vsn = data[i] & 0x00ff;
00560       if (fTdcSlotsH & 1 << (vsn - 1)) {
00561         //
00562         // stuff remaining in header word
00563         //      
00564         UShort_t dWordInd = data[i] >> 14 & 0x0001;
00565         //
00566         // we check bankLen and dwd just to make sure we don't have
00567         // a broken event; this is why we also check for second data-word
00568         //
00569         if (i + 1 < bankLen && dWordInd == 1 &&
00570             ! (data[i + 1] & fFeraHeaderBit)) {
00571           do {
00572             //
00573             // loop till end of TDC data, this is stuff from first data word
00574             //
00575             i++;
00576             UShort_t chan1 = data[i] >> 10 & 0x001f;
00577             UShort_t val1  = data[i] & 0x00ff;
00578             //
00579             // stuff from second data word
00580             //
00581             if (i + 1 < bankLen) {
00582               i++;
00583               UShort_t chan2 = data[i] >> 10 & 0x001f;
00584               UShort_t val2  = data[i] & 0x00ff;
00585               if (chan1 == chan2) {
00586                 UShort_t chan = chan1 + (vsn - fTdcSlotH[0]) * 32;
00587                 UShort_t cont = (val1 << 8) | val2;
00588                 tdcs.push_back(TDataItem<UShort_t>(chan, cont));
00589               } else
00590                 cerr << "UnpackSubEventH: ERROR - "
00591                      << "TDC double-word channels unequal" << endl;
00592             }
00593           }
00594           while (i + 1 < bankLen &&
00595                  (data[i + 1] & fFeraHeaderBit) != fFeraHeaderBit);
00596         } else if (! data[i] & fHeaderWordH)
00597           cerr << "UnpackSubEventH: ERROR - "
00598                << "TDC header-word but no double-word data found" << endl;
00599       } else if (fAdcSlotsH & 1 << (vsn - 1)) {
00600         UShort_t vdc = data[i] >> 8 & 0x000f;
00601 
00602         i++;
00603         if (i + 1 < bankLen) {
00604           do {
00605             i++;
00606             UShort_t sub  = data[i] >> 12 & 0x0007;
00607             UShort_t cont = data[i] & 0x0fff;
00608             UShort_t chan = sub + (vsn - fAdcSlotH[0]) * 8;
00609             adcs.push_back(TDataItem<UShort_t>(chan, cont));
00610             vdc--;
00611           }
00612           while (i + 1 < bankLen && vdc > 0 &&
00613                  (data[i + 1] & fFeraHeaderBit) != fFeraHeaderBit);
00614         } else
00615           cerr << "UnpackSubEventH: ERROR - "
00616                << "ADC header-word but no data word found" << endl;
00617       } else if (! ((fAdcSlotsH | fTdcSlotsH) & 1 << (vsn - 1)))
00618         cerr << "UnpackSubEventH: ERROR - "
00619              << "illegal vsn " << dec << vsn << endl;
00620     } else
00621       cerr << "UnpackSubEventH: ERROR - attempting to unpack at end of bank "
00622            << endl << "       or no heavy-ion header-word tag in subevent, "
00623            << endl << "       skipping word to next header..." << endl;
00624     //
00625     // the data[] & 0x80ff makes sure we do one subevent at a time
00626     // this is denoted by the occurence of a TDC header from first TDC module
00627     //
00628   }
00629   while (i + 1 < bankLen &&
00630          (data[++i] & (fFeraHeaderBit | 0x00ff)) != fHeaderWordH);
00631   subEvent.Set(adcs, tdcs);
00632   //
00633   // return word length of the event
00634   //
00635   return i - startIndex;
00636 }
00637 
00638 Int_t TDragonRun::UnpackSubEventG(Int_t startIndex, const UShort_t *data,
00639                                   Int_t bankLen, TSubEvent &subEvent) const
00640 {
00655   Int_t i;
00656   vector<TDataItem<UShort_t> > adcs;
00657   vector<TDataItem<UShort_t> > tdcs;
00658 
00659   for (i = startIndex; (data[i] & 0x80ff) != fHeaderWordG && i < bankLen; i++)
00660     if (i + 1 == bankLen) {
00661       cerr << "UnpackSubEventG: ERROR - no header found" << endl;
00662       return 0;
00663     }
00664   //
00665   // each subevent starts with a TDC header regardless - i.e. the 3377's
00666   // _always_ give a header on each readout (this is set in a register)
00667   //
00668   do {
00669     if (i < bankLen && (data[i] & fFeraHeaderBit)) {
00670       // 
00671       // we have a header word so unpack it
00672       //
00673       UShort_t vsn = data[i] & 0x00ff;
00674       if (fTdcSlotsG & 1 << (vsn - 1)) {
00675         //
00676         // stuff remaining in header word
00677         //
00678         UShort_t dWordInd = data[i] >> 14 & 0x0001;
00679         if (i + 1 < bankLen && dWordInd == 1 &&
00680             ! (data[i + 1] & fFeraHeaderBit)) {
00681           do {
00682             //
00683             // stuff from first data word
00684             //
00685             i++;
00686             UShort_t chan1 = data[i] >> 10 & 0x001f;
00687             UShort_t val1  = data[i] & 0x00ff;
00688             //
00689             // stuff from second data word, make sure it's there first
00690             //
00691             if (i + 1 < bankLen) {
00692               i++;
00693               UShort_t chan2 = data[i] >> 10 & 0x001f;
00694               UShort_t val2  = data[i] & 0x00ff;
00695               if (chan1 == chan2) {
00696                 UShort_t chan = chan1 + (vsn - fTdcSlotG[0]) * 32;
00697                 UShort_t cont = (val1 << 8) | val2;
00698                 if (fTdcSlotsG & 1 << (vsn - 1))
00699                   tdcs.push_back(TDataItem<UShort_t>(chan, cont));
00700               } else {
00701                 cerr << "UnpackSubEventG: ERROR - "
00702                      << "TDC double-word channels unequal" << endl;
00703               }
00704             } else {
00705               cerr << "UnpackSubEventG: ERROR - "
00706                    << "second of TDC double-word data missing" << endl;
00707             }
00708           }
00709           while (i + 1 < bankLen &&
00710                  (data[i + 1] & fFeraHeaderBit) != fFeraHeaderBit);
00711         } else if (! data[i] & fHeaderWordG)
00712           cerr << "DumpG: ERROR - found TDC header-word = 0x"
00713                << hex << data[i] << " but no data" << endl;
00714       } else if ((fAdcSlotsG & 1 << (vsn - 1)) &&
00715                  ! (data[i + 1] & fFeraHeaderBit)) {
00716         //
00717         // stuff remaining in header word.  This crazy piece of jiggery-pokery
00718         // is to make sure we get correct wc, as wc = 0 means 16 words!
00719         //
00720         UShort_t wc = (! (data[i] & 0x7800)) ? 16 : data[i] >> 11 & 0x000f;
00721         if (i + 1 < bankLen) {
00722           do {
00723             //
00724             // stuff from data word
00725             //
00726             i++;
00727             UShort_t chan = data[i] >> 11 & 0x000f;
00728             UShort_t cont = data[i] & 0x07ff;
00729             chan += (vsn - fAdcSlotG[0]) * 16;
00730             adcs.push_back(TDataItem<UShort_t>(chan, cont));
00731             wc--;
00732           }
00733           while (i + 1 < bankLen && wc > 0 &&
00734                  (data[i + 1] & fFeraHeaderBit) != fFeraHeaderBit);
00735         } else
00736           cerr << "UnpackSubEventG: ERROR - "
00737                << "ADC header-word but no data found" << endl;
00738       } else if (! ((fAdcSlotsG | fTdcSlotsG) & 1 << (vsn - 1)))
00739         cerr << "UnpackSubEventG: ERROR - " << "illegal vsn "
00740              << dec << vsn << endl;
00741     } else
00742       cerr << "UnpackSubEventG: ERROR - attempting to unpack at end of bank "
00743            << endl << "       or no gamma-ray header-word tag in subevent, "
00744            << endl << "       skipping word to next header..." << endl;
00745   }
00746   while (i + 1 < bankLen &&
00747          (data[++i] & (fFeraHeaderBit | 0x00ff)) != fHeaderWordG);
00748   subEvent.Set(adcs, tdcs);
00749   //
00750   // return word length of the event
00751   //
00752   return i - startIndex;
00753 }
00754 
00755 Int_t TDragonRun::DumpSubEventH(Int_t startIndex, const UShort_t *data,
00756                                 Int_t bankLen) const
00757 {
00769   Int_t i;
00770   const Char_t *name = reinterpret_cast<const Char_t *>(data);
00771   cout << "DumpSubEventH: " << name[-12] << name[-11] 
00772        << name[-10]         << name[-9]  << endl;
00773   if (startIndex == 0) {
00774     cout << "DumpSubEventH: Event data:" << endl;
00775     cout.setf(ios_base::hex, ios_base::basefield);
00776     cout.setf(ios_base::showbase | ios_base::internal);
00777     cout.fill('0');
00778     for (i = 0; i < bankLen; i++) {
00779       cout << setw(6) << data[i] << " ";
00780       if (i % 11 == 10)
00781         cout << endl;
00782     }
00783     cout << endl;
00784   }
00785   cout.setf(ios_base::dec);
00786   for (i = startIndex; (data[i] & 0x80ff) != fHeaderWordH && i < bankLen; i++)
00787     if (i + 1 == bankLen) {
00788       cerr << "DumpSubEventH: ERROR - no header found" << endl;
00789       return 0;
00790     }
00791   do {
00792     if (i < bankLen && (data[i] & fFeraHeaderBit)) {
00793       UShort_t vsn = data[i] & 0x00ff;
00794       if (fTdcSlotsH & 1 << (vsn - 1)) {
00795         UShort_t dWordInd = data[i] >> 14 & 0x0001;
00796         UShort_t bufEvNo  = data[i] >> 11 & 0x0007;
00797         cout  << "LeCroy 3377 TDC hw " 
00798               << hex     << data[i]     << ":"       << endl
00799               << dec     << "dWordInd=" << dWordInd  << " bufEvNo="
00800               << bufEvNo << " vsn="     << vsn       << endl;
00801         if (i + 1 < bankLen && dWordInd == 1 &&
00802             ! (data[i + 1] & fFeraHeaderBit)) {
00803           do {
00804             i++;
00805             UShort_t chan1 = data[i] >> 10 & 0x001f;
00806             UShort_t edge1 = data[i] >> 9 & 0x0001;
00807             UShort_t wdNo1 = data[i] >> 8 & 0x0001;
00808             UShort_t val1  = data[i] & 0x00ff;
00809             cout << "word1="  << hex   << data[i]   << dec
00810                  << " chan1=" << chan1 << " edge1=" << edge1
00811                  << " wdNo1=" << wdNo1 << " val1="  << val1  << endl;
00812             if (i + 1 < bankLen) {
00813               i++;
00814               UShort_t chan2 = data[i] >> 10 & 0x001f;
00815               UShort_t edge2 = data[i] >> 9 & 0x0001;
00816               UShort_t wdNo2 = data[i] >> 8 & 0x0001;
00817               UShort_t val2  = data[i] & 0x00ff;
00818               cout << "word2="  << hex   << data[i]   << dec
00819                    << " chan2=" << chan2 << " edge2=" << edge2
00820                    << " wdNo2=" << wdNo2 << " val2="  << val2   << endl;
00821               if (chan1 == chan2) {
00822                 UShort_t chan = chan1 + (vsn - fTdcSlotH[0]) * 32;
00823                 UShort_t cont = (val1 << 8) | val2;
00824                 cout << "chan="  << dec  << chan
00825                      << " cont=" << cont << endl;
00826               } else
00827                 cerr << "DumpSubEventH: ERROR - TDC double-word "
00828                      << "channels unequal" << endl;
00829             }
00830           }
00831           while (i + 1 < bankLen &&
00832                  (data[i + 1] & fFeraHeaderBit) != fFeraHeaderBit);
00833         } else if (! data[i] & fHeaderWordH)
00834           cerr << "DumpSubEventH: ERROR - found TDC header-word = 0x"
00835                << hex << data[i] << ", but no data" << endl;
00836       } else if (fAdcSlotsH & 1 << (vsn - 1)) {
00837         UShort_t vdc = data[i] >> 8 & 0x000f;
00838         i++;
00839         UShort_t patWd = data[i] & 0x00ff;
00840         cout << "Silena 4418V ADC hw "    << hex  << data[i - 1]
00841              << dec          << ":"       << endl << "vdc=" << vdc << " vsn="
00842              << vsn          << " patWd=" << hex  << setw(4)
00843              << setfill('0') << patWd     << endl;
00844         if (i + 1 < bankLen) {
00845           do {
00846             i++;
00847             UShort_t ovf  = data[i] >> 15 & 0x0001;
00848             UShort_t sub  = data[i] >> 12 & 0x0007;
00849             UShort_t val  = data[i] & 0x0fff;
00850             UShort_t chan = sub + (vsn - fAdcSlotH[0]) * 8;
00851             UShort_t cont = val;
00852             vdc--;
00853             cout << "word=" << hex  << setw(4)
00854                  << setfill('0')    << data[i]  << dec
00855                  << " ovf=" << ovf  << " sub="  << sub
00856                  << " val=" << val  << endl
00857                  << "chan=" << chan << " cont=" << cont << endl;
00858           }
00859           while (i + 1 < bankLen && vdc > 0 &&
00860                  (data[i + 1] &  fFeraHeaderBit) != fFeraHeaderBit );
00861         } else
00862           cerr << "DumpSubEventH: ERROR - found ADC header-word = 0x"
00863                << hex << data[i] << ", but no data" << endl;
00864       } else if (! ((fAdcSlotsH | fTdcSlotsH) & 1 << (vsn - 1)))
00865         cerr << "DumpSubEventH: ERROR - illegal vsn = "     << dec     << vsn
00866              << " from header-word " << i << " = 0x" << hex << data[i] << endl;
00867     } else
00868       cerr << "DumpSubEventH: ERROR - attempting to unpack at end of bank "
00869            << endl
00870            << "       or no heavy-ion header-word tag in subevent, " << endl
00871            << "       skipping word " << dec << i <<" = 0x" << hex << data[i]
00872            << " to next header..." << endl;
00873   }
00874   while (i + 1 < bankLen  &&
00875          (data[++i] & (fFeraHeaderBit | 0x00ff)) != fHeaderWordH);
00876   return i - startIndex;
00877 }
00878 
00879 Int_t TDragonRun::DumpSubEventG(Int_t startIndex, const UShort_t *data,
00880                                 Int_t bankLen) const
00881 {
00893   Int_t i;
00894   const Char_t *name = reinterpret_cast<const Char_t *>(data);
00895   cout << "DumpSubEventG: "  << name[-12] << name[-11]
00896        << name[-10]          << name[-9]  << endl;
00897   if (startIndex == 0) {
00898     cout << "DumpSubEventG: Event data:" << endl;
00899     cout.setf(ios_base::hex, ios_base::basefield);
00900     cout.setf(ios_base::showbase | ios_base::internal);
00901     cout.fill('0');
00902     for (i = 0; i < bankLen; i++) {
00903       cout << setw(6) << data[i] << " ";
00904       if (i % 11 == 10)
00905         cout << endl;
00906     }
00907     cout << endl;
00908   }
00909   cout.setf(ios_base::dec);
00910   for (i = startIndex; (data[i] & 0x80ff) != fHeaderWordG && i < bankLen; i++)
00911     if (i + 1 == bankLen) {
00912       cerr << "DumpSubEventG: ERROR - no header found" << endl;
00913       return 0;
00914     }
00915   do {
00916     if (i < bankLen && (data[i] & fFeraHeaderBit)) {
00917       UShort_t vsn = data[i] & 0x00ff;
00918       if (fTdcSlotsG & 1 << (vsn - 1)) {
00919         UShort_t dWordInd = data[i] >> 14 & 0x0001;
00920         UShort_t bufEvNo  = data[i] >> 11 & 0x0007;
00921         cout  << "LeCroy 3377 TDC hw " 
00922               << hex     << data[i]     << ":"      << endl
00923               << dec     << "dWordInd=" << dWordInd << " bufEvNo="
00924               << bufEvNo << " vsn="     << vsn      << endl;
00925         if (i + 1 < bankLen && dWordInd == 1 &&
00926             ! (data[i + 1] & fFeraHeaderBit)) {
00927           do {
00928             i++;
00929             UShort_t chan1 = data[i] >> 10 & 0x001f;
00930             UShort_t edge1 = data[i] >>  9 & 0x0001;
00931             UShort_t wdNo1 = data[i] >>  8 & 0x0001;
00932             UShort_t val1  = data[i] & 0x00ff;
00933             cout << hex       << "word1=" << data[i]   << dec
00934                  << " chan1=" << chan1    << " edge1=" << edge1
00935                  << " wdNo1=" << wdNo1    << " val1="  << val1  << endl;
00936             if (i + 1 < bankLen) {
00937               i++;
00938               UShort_t chan2 = data[i] >> 10 & 0x001f;
00939               UShort_t edge2 = data[i] >>  9 & 0x0001;
00940               UShort_t wdNo2 = data[i] >>  8 & 0x0001;
00941               UShort_t val2  = data[i] & 0x00ff;
00942               cout << hex       << "word2=" << data[i]   << dec
00943                    << " chan2=" << chan2    << " edge2=" << edge2
00944                    << " wdNo2=" << wdNo2    << " val2="  << val2  << endl;
00945               if (chan1 == chan2) {
00946                 UShort_t chan = chan1;
00947                 UShort_t cont = (val1 << 8) | val2;
00948                 cout << dec      << "chan=" << chan
00949                      << " cont=" << cont    << endl;
00950               } else
00951                 cerr << "DumpSubEventG: ERROR - TDC double-word "
00952                      << "channels unequal" << endl;
00953             } else
00954               cerr << "DumpSubEventG: ERROR - second of TDC "
00955                    << "double-word data missing" << endl;
00956           }
00957           while (i +1 < bankLen &&
00958                  (data[i + 1] & fFeraHeaderBit) != fFeraHeaderBit);
00959         } else if (! data[i] & fHeaderWordG)
00960           cerr << "DumpSubEventG: ERROR - found TDC header-word = 0x"
00961                << hex << data[i] << " but no data" << endl;
00962       } else if ((fAdcSlotsG & 1 << (vsn - 1)) &&
00963                  ! (data[i + 1] & fFeraHeaderBit)) {
00964         UShort_t wc = (! (data[i] & 0x7800)) ? 16 : data[i] >> 11 & 0x000f;
00965         cout  << "LeCroy 4300B ADC hw "    << hex 
00966               << data[i] << ":" << endl    << dec
00967               << "wc="   << wc  << " vsn=" << vsn << endl;
00968         if (i + 1 < bankLen) {
00969           do {
00970             i++;
00971             UShort_t chan = data[i] >> 11 & 0x000f;
00972             UShort_t cont = data[i]  & 0x07ff;
00973             cout << hex      << "word=" << data[i]  << dec
00974                  << " chan=" << chan    << " cont=" << cont << endl;
00975             wc--;
00976           }
00977           while (i + 1 < bankLen && wc > 0 &&
00978                  (data[i + 1] & fFeraHeaderBit) != fFeraHeaderBit);
00979         } else
00980           cerr << "DumpSubEventG: ERROR - found ADC header-word = 0x"
00981                << hex << data[i] << ", but no data" << endl;
00982       } else if (! ((fAdcSlotsG | fTdcSlotsG) & 1 << (vsn - 1)))
00983         cerr << "DumpSubEventG: ERROR - illegal vsn = "     << dec     << vsn
00984              << " from header-word " << i << " = 0x" << hex << data[i] << endl;
00985     } else
00986       cerr << "DumpSubEventG: ERROR - attempting to unpack at end of bank "
00987            << endl
00988            << "       or no gamma-ray header-word tag in subevent, " << endl
00989            << "       skipping word " << dec << i <<" = 0x" << hex << data[i]
00990            << " to next header..." << endl;
00991   }
00992   while (i + 1 < bankLen  &&
00993          (data[++i] & (fFeraHeaderBit | 0x00ff)) != fHeaderWordG);
00994   return i - startIndex;
00995 }
00996 
00997 Bool_t TDragonRun::UnpackScalerEvent()
00998 {
01002   UInt_t *dataBank = 0;
01003   Short_t triggerMask = fMidasEvent->GetTriggerMask();
01004   fMidasEvent->SwapBytes(0);
01005 
01006   if ((triggerMask & kTRIG_SCALER) == kTRIG_SCALER) {
01007     //
01008     // Scaler data
01009     //
01010     Int_t wordsN = 0;
01011     Int_t bankLen =
01012       fMidasEvent->LocateBank("SCLR", reinterpret_cast<void *>(&dataBank));
01013     if (bankLen > 0 &&
01014         static_cast<UInt_t>(bankLen) < fMidasEvent->GetDataSize()) {
01015       if (fDebug)
01016         wordsN = DumpScaler(dataBank, bankLen);
01017       vector<TDataItem<UInt_t> > scalers;
01018       wordsN = UnpackScaler(dataBank, bankLen, scalers);
01019       fScalerEvent->AddScalers(scalers);
01020       wordsN = bankLen - 1;
01021     } else {
01022       cerr << "UnpackScalerEvent: ERROR - scaler bank length " << dec << bankLen
01023            << " with event data-size " << fMidasEvent->GetDataSize()  << endl;
01024     }
01025     if (wordsN != bankLen - 1) {
01026       cerr << "UnpackScalerEvent: ERROR - unpacked " << dec << wordsN
01027            << " words from bank length " << bankLen  << endl;
01028       return false;
01029     }
01030   }
01031   if (triggerMask > kTRIG_SCALER) {
01032     cerr << "UnpackScalerEvent: ERROR - unknown triggerMask "
01033          << triggerMask << endl;
01034     return false;
01035   }
01036   return true;
01037 }
01038 
01039 Int_t TDragonRun::UnpackScaler(const UInt_t *data, Int_t bankLen,
01040                                vector<TDataItem<UInt_t> > &scalers) const
01041 {
01048   UShort_t i;
01049   for (i = 0; i < bankLen; i++)
01050     scalers.push_back(TDataItem<UInt_t>(i, data[i]));
01051   return i;
01052 }
01053 
01054 Int_t TDragonRun::DumpScaler(const UInt_t *data, Int_t bankLen) const
01055 {
01061   Int_t i;
01062   const Char_t *name = reinterpret_cast<const Char_t *>(data);
01063   cout << "DumpScaler: " << name[-8] << name[-7]
01064        << name[-6]       << name[-5] << endl;
01065   cout << "DumpScaler: Event data:" << endl;
01066   cout.setf(ios_base::hex, ios_base::basefield);
01067   cout.setf(ios_base::showbase | ios_base::internal);
01068   cout.fill('0');
01069   for (Int_t j = 0; j < bankLen; j++) {
01070     cout << setw(10) << data[j] << " ";
01071     if (j % 7 == 6)
01072       cout << endl;
01073   }
01074   cout << endl;
01075   cout << "LeCroy scaler data:" << endl;
01076   for (i = 0; i < bankLen; i++)
01077     cout << "word=" << dec << i << " data=" << data[i] << endl;
01078   return i;
01079 }
01080 
01081 Bool_t TDragonRun::UnpackEpicsEvent()
01082 {
01086   Float_t *dataBank = 0;
01087   Short_t triggerMask = fMidasEvent->GetTriggerMask();
01088   fMidasEvent->SwapBytes(0);
01089 
01090   if ((triggerMask & kTRIG_EPICS) <= kTRIG_EPICS) {
01091     //
01092     // Epics data
01093     //
01094     Int_t wordsN = 0;
01095     Int_t bankLen =
01096       fMidasEvent->LocateBank("EPCS", reinterpret_cast<void *>(&dataBank));
01097     if (bankLen > 0 &&
01098         static_cast<UInt_t>(bankLen) < fMidasEvent->GetDataSize()) {
01099       if (fDebug)
01100         wordsN = DumpEpics(dataBank, bankLen);
01101       vector<TDataItem<Float_t> > epics;
01102       wordsN = UnpackEpics(dataBank, bankLen, epics);
01103       fEpicsEvent->AddEpics(epics);
01104       wordsN = bankLen - 1;
01105     } else {
01106       cerr << "UnpackEpicsEvent: ERROR - epics bank length " << dec  << bankLen
01107            << " with event data-size " << fMidasEvent->GetDataSize() << endl;
01108     }
01109     if (wordsN != bankLen - 1) {
01110       cerr << "UnpackEpicsEvent: ERROR - unpacked " << dec << wordsN
01111            << " words from bank length " << bankLen << endl;
01112       return false;
01113     }
01114   }
01115   if (triggerMask > kTRIG_EPICS) {
01116     cerr << "UnpackEpicsEvent: ERROR - unknown triggerMask "
01117          << triggerMask << endl;
01118     return false;
01119   }
01120   return true;
01121 }
01122 
01123 Int_t TDragonRun::UnpackEpics(const Float_t *data, Int_t bankLen,
01124                               vector<TDataItem<Float_t> > &epics) const
01125 {
01132   UShort_t i;
01133   for (i = 0; i < bankLen; i+=3) {
01134     UShort_t channel = static_cast<UShort_t>(data[i + 1]);
01135     epics.push_back(TDataItem<Float_t>(channel, data[i + 2]));
01136   }
01137   return i;
01138 }
01139 
01140 Int_t TDragonRun::DumpEpics(const Float_t *data, Int_t bankLen) const
01141 {
01147   Int_t i;
01148   const Char_t *name = reinterpret_cast<const Char_t *>(data);
01149   cout << "DumpEpics: " << name[-8] << name[-7]
01150        << name[-6]      << name[-5] << endl;
01151   cout << "DumpEpics: Event data:" << endl;
01152   for (Int_t j = 0; j < bankLen; j++) {
01153     cout << data[j] << " ";
01154     if (j % 7 == 6)
01155       cout << endl;
01156   }
01157   cout << endl;
01158   cout << "Epics data:" << endl;
01159   for (i = 0; i < bankLen; i+=3)
01160     cout << "word="  << dec << data[i] << " channel=" << data[i + 1]
01161          << " data=" << data[i + 2]    << endl;
01162   return i;
01163 }

Generated on Mon Jan 8 11:54:31 2007 for DragonRoot by  doxygen 1.3.9.1