CREAM TEA -- ROOT MCP

McpTarget.cxx

00001 #include "McpTarget.h"
00002 #include "TargetData.h"
00003 
00004 #include <iostream>
00005 #include <fstream>
00006 
00007 #include "TFile.h"
00008 #include "TTree.h"
00009 
00010 #include "stdUSB.h"
00011 
00012 stdUSB fTheUsb;
00013 
00014 McpTarget::McpTarget(int offlineMode) 
00015   :fOfflineMode(offlineMode),fExtTrigMode(0),fEventNumber(0),fNumPedEvents(100)
00016 {
00017   for(int chip=0;chip<NUM_TARGETS;chip++) {
00018     for(int chan=0;chan<NUM_CHANNELS;chan++) {
00019       for(int row=0;row<NUM_ROWS;row++) {
00020         for(int col=0;col<NUM_COLS;col++) {
00021           for(int samp=0;samp<SAMPLES_PER_COL;samp++) {
00022             fPedestalValues[chip][chan][row][col][samp]=2000;
00023           }
00024         }
00025       }
00026     }
00027   }
00028   fSoftTrigMode=0;
00029   loadDnlLookUpTable();
00030   loadPedestal();
00031   fTargetDataPtr=0;
00032   fRawTargetDataPtr=0;
00033   fTheOutputFile=0;
00034   fTheOutputTree=0;
00035   fOutputMode=0;
00036 
00037   if(!fOfflineMode) {
00038     useSyncUsb(0);
00039     setTermValue(0,1,0);
00040     enablePedestal(false);
00041     setPedRowCol(0,0);
00042     setTrigPolarity(false); //Falling edge
00043     setWbias(1000); 
00044     setTrigThresh(1639); 
00045   }
00046 }
00047 
00048 McpTarget::~McpTarget()
00049 {
00050   saveOutputFile();
00051 }
00052 
00053 void McpTarget::saveOutputFile()
00054 {
00055   if(fOutputMode) {
00056     if(fTheOutputTree)
00057       fTheOutputTree->AutoSave();
00058   }
00059 }
00060 
00061 Int_t McpTarget::justReadBuffer()
00062 {
00063   if(fOfflineMode) {
00064     std::cerr << "Running in offline mode can't read buffer\n";
00065     return 0;
00066   }
00067   //This only read the data from the USB thingy and sticks it into
00068   // fReadoutBuffer
00069   
00070   //Zero fReadoutBuffer
00071   memset(fReadoutBuffer,0,BUFFERSIZE*sizeof(unsigned short));
00072 
00073   int bytesRead=0;
00074   if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00075     std::cerr << "USB failed to initalize.\n";
00076     exit(0);
00077   }
00078   bool retVal=fTheUsb.readData(fReadoutBuffer, BUFFERSIZE, &bytesRead);
00079   if(retVal!=stdUSB::SUCCEED) { 
00080     sleep(1);
00081     retVal=fTheUsb.readData(fReadoutBuffer, BUFFERSIZE, &bytesRead);
00082   }
00083   fTheUsb.freeHandles();
00084 
00085   if(fDumpRawHexData) {
00086     ofstream HexDump("eventHexDump.txt");
00087     for(int i=0;i<BUFFERSIZE;i++) {
00088       HexDump << std::dec << i << "\t" << std::hex << "\t" << fReadoutBuffer[i] << "\n";
00089     }
00090   }
00091 
00092   if(bytesRead<BUFFERSIZE) {    
00093     std::cerr << "Only read " << bytesRead << " of " << BUFFERSIZE << "\n";
00094     return -1;
00095   }
00096 
00097 
00098   return 0;
00099 }
00100 
00101 
00102 void McpTarget::generatePedestals() 
00103 {
00104 
00105   if(fOfflineMode) {
00106     std::cerr << "Running in offline mode can't generate pedestals\n";
00107     return;
00108   }
00109   Double_t tempValues[NUM_TARGETS][NUM_CHANNELS][NUM_ROWS][NUM_COLS][SAMPLES_PER_COL]={{{{{0}}}}};
00110   Int_t countStuff[NUM_ROWS][NUM_COLS]={{0}};
00111   
00112   Int_t row,col;
00113   TFile *fpTemp = new TFile("pedFile.root","RECREATE");
00114   TTree *pedTree = new TTree("pedTree","Tree of pedestal thingies");    
00115   pedTree->Branch("target","RawTargetData",&fRawTargetDataPtr);
00116   pedTree->Branch("values",&fReadoutBuffer[0],"values[4140]/s");
00117   enablePedestal(1);
00118   
00119   for(row=0;row<NUM_ROWS;row++) {
00120     for(col=0;col<NUM_COLS;col++) {     
00121       setPedRowCol(row,col);      
00122       
00123       for(int event=0;event<fNumPedEvents;event++) {
00124         //Send software trigger
00125         std::cerr << "*";
00126         sendSoftTrig();
00127         usleep(1000);
00128         Int_t retVal=justReadBuffer();
00129         if(retVal<0) continue;
00130         //Now unpack the data into a more useful structure
00131         TargetData targetData(this->fReadoutBuffer);
00132         RawTargetData rawTargetData(this->fReadoutBuffer);
00133         fTargetDataPtr=&targetData;
00134         fRawTargetDataPtr=&rawTargetData;
00135         pedTree->Fill();        
00136         countStuff[row][col]++;
00137         for(int chip=0;chip<NUM_TARGETS;chip++) {
00138           for(int chan=0;chan<NUM_CHANNELS;chan++) {
00139             for(int samp=0;samp<SAMPLES_PER_COL;samp++) {
00140               Float_t value=(Float_t)targetData.data[chip][chan][samp];
00141               tempValues[chip][chan][row][col][samp]+=value;
00142             }
00143           }
00144         }
00145       }
00146         std::cerr << "\n";
00147     }
00148     std::cout << "Read " << fNumPedEvents << " events from " << row << "\n";
00149   }
00150         
00151   pedTree->AutoSave();
00152   delete fpTemp;
00153   
00154   std::ofstream PedFile("pedestal.txt");
00155   for(int chip=0;chip<NUM_TARGETS;chip++) {
00156     for(int chan=0;chan<NUM_CHANNELS;chan++) {
00157       for(int row=0;row<NUM_ROWS;row++) {
00158         for(int col=0;col<NUM_COLS;col++) {
00159           for(int samp=0;samp<SAMPLES_PER_COL;samp++) {
00160             if(countStuff[row][col]>0) {
00161               tempValues[chip][chan][row][col][samp]/=countStuff[row][col];
00162               fPedestalValues[chip][chan][row][col][samp]=(Int_t)tempValues[chip][chan][row][col][samp];
00163             }
00164             PedFile << fPedestalValues[chip][chan][row][col][samp] << "\n";
00165           }
00166         }
00167       }
00168     } 
00169   }
00170 }
00171 
00172 int McpTarget::readEvent()
00173 {
00174 
00175   if(fOfflineMode) {
00176     std::cerr << "Running in offline mode can't read event\n";
00177     return 0;
00178   }
00179   //Read data from USB class (stdUSB.cpp) since we dont have the device yet 
00180   //then we are going to read data from test data file  
00181   static int counter=0; 
00182     
00183 
00184   if(counter<2) {
00185     useSyncUsb(0);
00186     setTermValue(0,1,0);
00187     enablePedestal(false);
00188     setPedRowCol(0,0);
00189     setTrigPolarity(false); //Falling edge
00190     setWbias(1000); 
00191     setTrigThresh(1639); 
00192   }
00193 
00194   if(fSoftTrigMode) {
00195     sendSoftTrig();
00196   }
00197     
00198   counter++;
00199   Int_t retVal=justReadBuffer();
00200   if(retVal<0) {
00201     return -1;
00202   }
00203   
00204   if(fTargetDataPtr)
00205     delete fTargetDataPtr;  
00206   fTargetDataPtr = new TargetData(fReadoutBuffer);
00207   if(fOutputMode) {
00208     if(fRawTargetDataPtr) 
00209       delete fRawTargetDataPtr;
00210     fRawTargetDataPtr = new RawTargetData(fReadoutBuffer);
00211     fTheOutputTree->Fill();
00212   }
00213 
00214   //Do the pedestal subtraction and voltage conversion
00215   fillVoltageArray(fTargetDataPtr);
00216 
00217   //  std::cout << "fPedSubbedBuffer[0][0] " << fPedSubbedBuffer[0][0] << "\n";
00218 
00219   fEventNumber++;
00220    
00221   return 1;
00222 }
00223 
00224 void McpTarget::fillVoltageArray(TargetData *targetDataPtr)
00225 {
00226 
00227   for(int chip=0;chip<NUM_TARGETS;chip++) {
00228     for(int chan=0;chan<NUM_CHANNELS;chan++) {
00229       for(int samp=0;samp<SAMPLES_PER_COL;samp++)  {
00230 //      std::cerr << chip << "\t" << chan << "\t" << samp << "\t"
00231 //                << targetDataPtr->data[chip][chan][samp] << "\n";
00232           
00233 //      Double_t value = fDnlLUT[targetDataPtr->data[chip][chan][samp]];
00234         Double_t value=targetDataPtr->data[chip][chan][samp];
00235 //      std::cerr << chip << "\t" << chan << "\t" << samp << "\t"
00236 //                << targetDataPtr->data[chip][chan][samp] << "\t"
00237 //                << value << "\t" << targetDataPtr->rowLoc[chip]
00238 //                <<"\t" << targetDataPtr->colLoc[chip] << "\n";
00239         Int_t row=targetDataPtr->rowLoc[chip];
00240         Int_t col=targetDataPtr->colLoc[chip];
00241 //      std::cerr << row << "\t" << col << "\t" << NUM_ROWS << "\t"
00242 //                << NUM_COLS << "\n";
00243 //      std::cerr << fPedestalValues[chip][chan][row][col][samp] << "\n";
00244         value-=fPedestalValues[chip][chan][row][col][samp]; 
00245         fVoltBuffer[chip][chan][samp]=value;
00246         targetDataPtr->fVoltBuffer[chip][chan][samp]=value;
00247         //      exit(0);
00248         //DNL_LUT.txt 
00249       }
00250     }
00251   }
00252 }
00253 
00254 // void McpTarget::setExtTrigMode(int mode)
00255 // {
00256 //   if(fExtTrigMode==mode) return;
00257 //   fExtTrigMode=mode;
00258 //   if(fExtTrigMode) {
00259 
00260 //  if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00261 //    std::cerr << "USB failed to initalize.\n";
00262 //    exit(0);
00263 //  }
00264 //     fTheUsb.sendData(0x10001);
00265 // fTheUsb.freeHandles();
00266 //   }
00267 //   else {
00268 
00269 //  if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00270 //    std::cerr << "USB failed to initalize.\n";
00271 //    exit(0);
00272 //  }
00273 //     fTheUsb.sendData(0x00001);
00274 
00275 //  fTheUsb.freeHandles();
00276 //     // do some dummy readouts before we continue
00277 //     unsigned short int tmpData[5];
00278 //     int read;
00279 //     for(int i=0; i < 5; i++) {
00280 //       usleep(10000);
00281 //       fTheUsb.sendData(0x00002);
00282 //       usleep(10000);
00283 //       fTheUsb.readData(tmpData, 2, &read);
00284 //     }
00285 //   }
00286 
00287 // }
00288 
00289 TGraph *McpTarget::getChannel(int chip, int channel)
00290 {
00291   if(chip<0 || chip>=NUM_TARGETS) return NULL;
00292   if(channel<0 || channel>=NUM_CHANNELS) return NULL;
00293   
00294   Double_t timeVals[SAMPLES_PER_COL]={0};
00295   Double_t voltVals[SAMPLES_PER_COL]={0};
00296   for(int i=0;i<SAMPLES_PER_COL;i++) {
00297      timeVals[i]=Double_t(i); //Should multiple by delta t here
00298      voltVals[i]=fVoltBuffer[chip][channel][i];
00299   }
00300   TGraph *gr = new TGraph(SAMPLES_PER_COL,timeVals,voltVals);
00301   return gr;
00302 }
00303 
00304 
00305 void McpTarget::loadPedestal() 
00306 {
00307   Int_t value;
00308   std::ifstream PedFile;
00309   PedFile.open("pedestal.txt");
00310   if(!PedFile.is_open()) {
00311     PedFile.open("defaultPed.txt");
00312   }
00313     
00314   if(PedFile.is_open()) {
00315     for(int chip=0;chip<NUM_TARGETS;chip++) {
00316       for(int chan=0;chan<NUM_CHANNELS;chan++) {
00317         for(int row=0;row<NUM_ROWS;row++) {
00318           for(int col=0;col<NUM_COLS;col++) {
00319             for(int samp=0;samp<SAMPLES_PER_COL;samp++) {
00320               PedFile >> value;
00321               fPedestalValues[chip][chan][row][col][samp]=value;
00322             }
00323           }
00324         }
00325       }
00326     }
00327     PedFile.close();
00328   }
00329   
00330   
00331 }
00332 
00333 
00334 void McpTarget::setPedRowCol(Int_t row, Int_t col)
00335 {
00336 
00337   if(fOfflineMode) {
00338     std::cerr << "Running in offline mode can't set ped row col\n";
00339     return;
00340   }
00341   UInt_t dataVal=0;
00342   dataVal = PED_ROW_COL_MASK;
00343   dataVal |= (row << PED_ROW_SHIFT);
00344   dataVal |= (col << PED_COL_SHIFT);
00345   if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00346     std::cerr << "USB failed to initalize.\n";
00347     exit(0);
00348   }
00349   fTheUsb.sendData(dataVal); 
00350   fTheUsb.freeHandles();
00351 }
00352 
00353 void McpTarget::enablePedestal(Int_t flag)
00354 {
00355 
00356   if(fOfflineMode) {
00357     std::cerr << "Running in offline mode can't enable pedestal\n";
00358     return;
00359   }
00360 
00361   if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00362     std::cerr << "USB failed to initalize.\n";
00363     exit(0);
00364   }
00365   if(flag==1) {
00366     fTheUsb.sendData(ENABLE_PED_MASK);
00367   }
00368   else {
00369     fTheUsb.sendData(DISABLE_PED_MASK);
00370   }
00371   fTheUsb.freeHandles();
00372 }
00373 
00374 
00375 void McpTarget::setWbias(UInt_t value)
00376 {
00377 
00378   if(fOfflineMode) {
00379     std::cerr << "Running in offline mode can't set WBIAS\n";
00380     return;
00381   }
00382   UInt_t dataVal = WBIAS_MASK;
00383   dataVal |= (value << WBIAS_SHIFT);
00384   if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00385     std::cerr << "USB failed to initalize.\n";
00386     exit(0);
00387   }
00388   fTheUsb.sendData(dataVal);
00389   fTheUsb.freeHandles();
00390 }
00391 
00392 
00393 void McpTarget::setTrigThresh(UInt_t value)
00394 {
00395 
00396   if(fOfflineMode) {
00397     std::cerr << "Running in offline mode can't set trigger threshold\n";
00398     return;
00399   }
00400   UInt_t dataVal=TRIG_THRESH_MASK;
00401   dataVal |= (value&0xffff) << TRIG_THRESH_SHIFT;
00402   if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00403     std::cerr << "USB failed to initalize.\n";
00404     exit(0);
00405   }
00406   fTheUsb.sendData(dataVal);
00407   fTheUsb.freeHandles();
00408 }
00409 
00410 void McpTarget::setTermValue(Int_t f100, Int_t f1k, Int_t f10k) 
00411 {
00412 
00413   if(fOfflineMode) {
00414     std::cerr << "Running in offline mode can't set TERM value\n";
00415     return;
00416   }
00417   UInt_t dataVal=TERM_MASK;
00418   dataVal |= (f100&0x1) << TERM_100_OHMS_SHIFT;
00419   dataVal |= (f1k&0x1) << TERM_1K_OHMS_SHIFT;
00420   dataVal |= (f10k&0x1) << TERM_10K_OHMS_SHIFT;
00421 
00422   if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00423     std::cerr << "USB failed to initalize.\n";
00424     exit(0);
00425   }
00426   fTheUsb.sendData(dataVal);
00427   fTheUsb.freeHandles();
00428 
00429 }
00430 
00431 
00432 void McpTarget::setTrigPolarity(Int_t flag)
00433 {
00434 
00435   if(fOfflineMode) {
00436     std::cerr << "Running in offline mode can't send trigger polarity\n";
00437     return;
00438   }
00439   if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00440     std::cerr << "USB failed to initalize.\n";
00441     exit(0);
00442   }
00443   if(flag==1) {
00444     fTheUsb.sendData(TRIG_POLARITY_NEG);
00445   }
00446   else {
00447     fTheUsb.sendData(TRIG_POLARITY_POS);
00448   }
00449   fTheUsb.freeHandles();
00450 }
00451 
00452 
00453 void McpTarget:: useSyncUsb(Int_t flag)
00454 {
00455 
00456   if(fOfflineMode) {
00457     std::cerr << "Running in offline mode can't send USB sync\n";
00458     return;
00459   }
00460   if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00461     std::cerr << "USB failed to initalize.\n";
00462     exit(0);
00463   }
00464   if(flag==1) {
00465     fTheUsb.sendData(ENABLE_SYNC_USB_MASK);
00466   }
00467   else {
00468     fTheUsb.sendData(DISABLE_SYNC_USB_MASK);
00469   }
00470   fTheUsb.freeHandles();
00471 }
00472 
00473 void McpTarget:: sendSoftTrig()
00474 {
00475 
00476   if(fOfflineMode) {
00477     std::cerr << "Running in offline mode can't send software trigger\n";
00478     return;
00479   }
00480   if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00481     std::cerr << "USB failed to initalize.\n";
00482     exit(0);
00483   }
00484   fTheUsb.sendData(SOFT_TRIG_MASK);
00485   fTheUsb.freeHandles();
00486 }
00487 
00488 
00489 void McpTarget::loadDnlLookUpTable()
00490 {
00491   std::ifstream DNL("DNL_LUT.txt");
00492   int sampVal;
00493   double lutVal;
00494   for(int i=0;i<4096;i++) {
00495     DNL >> sampVal >> lutVal;
00496     fDnlLUT[sampVal]=lutVal;
00497   }
00498 }
00499 
00500 // void McpTarget::getMemAddress(UInt_t memAddrSpace, UInt_t &rowLoc, UInt_t &colLoc,
00501 //                         UInt_t &pixLoc)
00502 // {
00503 //   const unsigned int MASK_COL = 0x000001F0;
00504 //   const unsigned int MASK_ROW = 0x00000E00;
00505 
00506 //   colLoc = memAddrSpace & MASK_COL;
00507 //   rowLoc = memAddrSpace & MASK_ROW;
00508     
00509 //   pixLoc = colLoc;
00510 //   colLoc = colLoc >> 4;
00511 //   rowLoc = rowLoc >> 9;
00512 
00513 // }
00514 
00515 
00516 void McpTarget::rawSendInt(unsigned int value)
00517 {
00518 
00519   if(fOfflineMode) {
00520     std::cerr << "Running in offline mode can't sent int\n";
00521     return;
00522   }
00523   if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00524     std::cerr << "USB failed to initalize.\n";
00525     exit(0);
00526   }
00527   bool retVal=fTheUsb.sendData(value);
00528   fTheUsb.freeHandles();
00529   std::cout << "Got " << retVal << " for sending " << value << "\n";
00530 }
00531 
00532 
00533 void McpTarget::rawReadInts(int numInts, unsigned short buffer[])
00534 {
00535 
00536   if(fOfflineMode) {
00537     std::cerr << "Running in offline mode can't read buffer\n";
00538     return;
00539   }
00540   if (fTheUsb.createHandles() != stdUSB::SUCCEED) {
00541     std::cerr << "USB failed to initalize.\n";
00542     exit(0);
00543   }
00544   int numRead=0;
00545   int retVal=fTheUsb.readData(buffer,numInts,&numRead);
00546   std::cout << "Got " << retVal << " for reading " << numRead 
00547             << " of " << numInts << " values " << "\n";
00548   if(numRead>0) {
00549     for(int i=0;i<numRead;i++) {
00550       std::cout << i << "\t" << buffer[i] << "\n";
00551     }
00552   }
00553   fTheUsb.freeHandles();
00554 }
00555 
00556 
00557 void McpTarget::openOutputFile(char fileName[180])
00558 {
00559   fOutputMode=1;
00560   fTheOutputFile = new TFile(fileName,"RECREATE");
00561   fTheOutputTree = new TTree("mcpTree","Target Output Tree");
00562   fTheOutputTree->Branch("target","RawTargetData",&fRawTargetDataPtr);
00563 }

Generated on Tue Nov 24 19:05:00 2009 for CREAM TEA -- MCP/TARGET Readout by doxygen 1.3.9.1