00001 #include <TMath.h>
00002 #include "Conventions/Munits.h"
00003 #include "MessageService/MsgService.h"
00004 #include "Digitization/DigiSignal.h"
00005 #include "SimQieElectronics.h"
00006 #include "SimElecMaker.h"
00007 #include "Calibrator/Calibrator.h"
00008
00009
00010 SimElecMakerProxy<SimQieElectronics> gSimQieElec("SimQieElectronics");
00011
00012 CVSID("$Id: SimQieElectronics.cxx,v 1.21 2008/10/15 16:46:34 tagg Exp $");
00013
00014 ClassImp(SimQieElectronics)
00015
00016 SimQieElectronics::SimQieElectronics( VldContext context, TRandom* random )
00017 : SimElectronics( context, random ),
00018 fClock(context),
00019 fQieDoLookup(1),
00020 fQieDacCharge(1.4*Munits::fC),
00021 fQieDacPerRangeZeroAdc(1.92),
00022 fQiePedestalWidthAdc(1.5),
00023 fQieAdcRms(0),
00024 fQiePedestalDac(50),
00025 fQieSparsifyThresh(70),
00026 fDoLookupNonlinearity(0)
00027 {
00028 SetupTables();
00029 }
00030
00031 void SimQieElectronics::Reset( const VldContext& newContext )
00032 {
00033 fClock.Reset(newContext);
00034 SimElectronics::Reset(newContext);
00035 }
00036
00037
00038 void SimQieElectronics::ReadoutDetector( SimPmtList& pmtList )
00039 {
00040 PlexHandle plex(fContext);
00041 SimPmtList::iterator it;
00042 for(it = pmtList.begin(); it!= pmtList.end(); it++) {
00043 SimPmt* pmt = it->second;
00044 if(pmt) {
00045 RawChannelId rcid = plex.GetRawChannelId(pmt->GetPixelSpotId(1));
00046
00047 if ( (rcid.GetElecType()==ElecType::kQIE) ) {
00048 ReadoutPmt( pmt );
00049 }
00050 }
00051 }
00052
00053 }
00054
00055 void SimQieElectronics::ReadoutPmt( SimPmt* pmt )
00056 {
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 MSG("DetSim",Msg::kDebug) << "SimQieElectronics::ReadoutPmt "
00076 << pmt->GetTubeId().AsString()
00077 << " type " << pmt->GetType()
00078 << endl;
00079
00080 PlexHandle plex(fContext);
00081
00082
00083 AddDigitsAfterFETrigger( pmt->GetTotalHitPixels(true) );
00084 AddAdcsAfterFETrigger( pmt->GetTotalCharge() * fQieDacCharge );
00085
00086
00087 std::map<UInt_t,std::vector<UInt_t> > channels;
00088 std::map<UInt_t,std::vector<UInt_t> >::iterator chItr;
00089
00090 for(int pixel = 1; pixel<= pmt->GetNumberOfPixels(); pixel++) {
00091 PlexPixelSpotId psid = pmt->GetPixelSpotId(pixel);
00092 RawChannelId rcid = plex.GetRawChannelId(psid);
00093 channels[rcid.GetEncoded()].push_back(pixel);
00094 }
00095
00096
00097 SimPmtBucketIterator bIt( *pmt );
00098 for( ; !bIt.End(); bIt.Next() ) {
00099
00100 Int_t bucketID = bIt.BucketId();
00101 SimPmtTimeBucket& bucket = bIt.Bucket();
00102
00103 for( chItr = channels.begin(); chItr != channels.end(); chItr++) {
00104 Float_t totalCharge = 0;
00105 DigiSignal* totalSignal = new DigiSignal();
00106 AddSignal(totalSignal);
00107 RawChannelId rcid(chItr->first);
00108 PlexPixelSpotId psid;
00109
00110 for(UInt_t ipix = 0; ipix < chItr->second.size(); ipix++) {
00111 Int_t pixel = chItr->second[ipix];
00112 psid = pmt->GetPixelSpotId(pixel);
00113 SimPixelTimeBucket& pixBucket = bucket.GetPixelBucket(pixel);
00114
00115 DigiSignal* signal = pixBucket.CreateSignal();
00116 Float_t charge = pixBucket.GetCharge();
00117
00118 totalSignal->Merge(*signal);
00119 totalCharge+=charge;
00120
00121 delete signal;
00122 }
00123
00124
00125 if(!(rcid.IsNull())) {
00126 if((totalCharge<-1000*Munits::fC)||(totalCharge>1e6*Munits::fC)) {
00127 MSG("DetSim",Msg::kError) << "Abnormal charge " << totalCharge/Munits::fC
00128 << " fC in pmt " << pmt->GetTubeId().AsString() << endl;
00129 pmt->Print();
00130 }
00131 SimDigit d( psid,
00132 rcid,
00133 GenSimulatedADC( rcid, psid, totalCharge, bucketID ),
00134 bucketID,
00135 totalSignal,
00136 0
00137 );
00138 MSG("DetSim",Msg::kVerbose) << "SimQieElectronics::ReadoutPmt: " << d.AsString() << std::endl;
00139
00140
00141 if(d.GetADC() > fQieSparsifyThresh) {
00142 AddDigit(d);
00143
00144
00145 AddDigitsAfterSpars(1);
00146 AddAdcsAfterSpars(d.GetADC());
00147 }
00148 }
00149 }
00150 }
00151 }
00152
00153
00154 Bool_t SimQieElectronics::DynodeTrigger( SimPmt* )
00155 {
00156
00157 return true;
00158 }
00159
00160
00161
00162 void SimQieElectronics::SetupTables( void )
00163 {
00164
00165 fAdcDacSlope[0] = 1./fQieDacPerRangeZeroAdc;
00166
00167
00168 fAdcDacSlope[1] = fAdcDacSlope[0]*0.5;
00169 fAdcDacSlope[2] = fAdcDacSlope[1]*0.5;
00170 fAdcDacSlope[3] = fAdcDacSlope[2]*0.5;
00171 fAdcDacSlope[4] = fAdcDacSlope[3]*0.5;
00172 fAdcDacSlope[5] = fAdcDacSlope[4]*0.5;
00173 fAdcDacSlope[6] = fAdcDacSlope[5]*0.5;
00174 fAdcDacSlope[7] = fAdcDacSlope[6]*0.5;
00175
00176
00177
00178
00179
00180 fFlipLowAdc[0] = 0;
00181 fFlipLowAdc[1] = 23.7;
00182 fFlipLowAdc[2] = 25.6;
00183 fFlipLowAdc[3] = 26.5;
00184 fFlipLowAdc[4] = 27.1;
00185 fFlipLowAdc[5] = 27.0;
00186 fFlipLowAdc[6] = 27.2;
00187 fFlipLowAdc[7] = 29;
00188
00189 fFlipHighAdc[0] = 185;
00190 fFlipHighAdc[1] = 190;
00191 fFlipHighAdc[2] = 196;
00192 fFlipHighAdc[3] = 199;
00193 fFlipHighAdc[4] = 192;
00194 fFlipHighAdc[5] = 200;
00195 fFlipHighAdc[6] = 177;
00196 fFlipHighAdc[7] = 255;
00197
00198
00199
00200
00201 fFlipPoint[0] = 0;
00202 for(int i=1;i<9;i++)
00203 fFlipPoint[i] = fFlipPoint[i-1] + (fFlipHighAdc[i-1]-fFlipLowAdc[i-1])/fAdcDacSlope[i-1];
00204 }
00205
00206
00207
00208
00209 int SimQieElectronics::GenSimulatedADC( RawChannelId,
00210 PlexPixelSpotId psid,
00211 double inCharge,
00212 int tdc)
00213 {
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 double true_dacs = inCharge / fQieDacCharge;
00224
00225
00226 if (fDoLookupNonlinearity) {
00227 PlexHandle plex(fContext);
00228 PlexStripEndId seid = plex.GetStripEndId(psid);
00229 if(seid.IsValid())
00230 true_dacs = Calibrator::Instance().DecalLinearity(true_dacs, seid);
00231 }
00232
00233
00234 double dacs = fRandom->Gaus(true_dacs,
00235 fQiePedestalWidthAdc * fQieDacPerRangeZeroAdc);
00236
00237
00238
00239 int range = 0;
00240 if (dacs < fFlipPoint[1]) range = 0;
00241 else if(dacs < fFlipPoint[2]) range = 1;
00242 else if(dacs < fFlipPoint[3]) range = 2;
00243 else if(dacs < fFlipPoint[4]) range = 3;
00244 else if(dacs < fFlipPoint[5]) range = 4;
00245 else if(dacs < fFlipPoint[6]) range = 5;
00246 else if(dacs < fFlipPoint[7]) range = 6;
00247 else range = 7;
00248
00249
00250
00251 double adc = fFlipLowAdc[range] + fAdcDacSlope[range]*(dacs - fFlipPoint[range]);
00252
00253
00254
00255
00256
00257 if(fQieAdcRms>0)
00258 adc = fRandom->Gaus(adc,fQieAdcRms);
00259
00260
00261
00262
00263
00264 if(adc>255) adc=255.;
00265 if(adc<0) adc=0.;
00266 double iadc = TMath::Nint(adc);
00267
00268 if(fQieDoLookup==0) {
00269
00270
00271 int adc8bits = (int)iadc;
00272 int capid = tdc%4;
00273 int dataword = (adc8bits & 0xFF) | (range << 8) | (capid << 11);
00274 return dataword;
00275 }
00276
00277
00278 double outdac = (iadc-fFlipLowAdc[range]) / fAdcDacSlope[range] + fFlipPoint[range];
00279
00280
00281 outdac += fQiePedestalDac;
00282
00283
00284
00285
00286 int final_dac = (int) ( floor(outdac) );
00287
00288 if(final_dac < 0) final_dac = 0;
00289
00290 if(final_dac > 0xffff) final_dac = 0xffff;
00291 MSG("DetSim",Msg::kVerbose)
00292 << "QIE: " << inCharge/Munits::fC << "fC -> "
00293 << " True DACs " << true_dacs
00294 << " Range: " << range
00295 << " ADC: " << adc
00296 << " -> DAC: " << final_dac << endl;
00297 return final_dac;
00298 }
00299
00300
00301 void
00302 SimQieElectronics::Print( Option_t* ) const
00303 {
00304 printf("SimQieElectronics: qieDoLookup %s\n",(fQieDoLookup)?("true"):("false"));
00305 printf(" qieDacCharge %.2f fC\n", fQieDacCharge/Munits::fC);
00306 printf(" qieDacPerRangeZeroAdc %.2f DACs\n",fQieDacPerRangeZeroAdc);
00307 printf(" qiePedestalWidthAdc %.2f Range-zero ADCs\n",fQiePedestalWidthAdc);
00308 printf(" qiePedestalDac %d DAC\n",fQiePedestalDac);
00309 printf(" qieSparsifyThresh %d DAC\n",fQieSparsifyThresh);
00310 printf(" doLookupNonlinearity %s \n",fDoLookupNonlinearity?("on"):("off") );
00311 }
00312
00313 void
00314 SimQieElectronics::Config( Registry& config )
00315 {
00316
00317
00318
00319 config.Get("qieDoLookup",fQieDoLookup);
00320 config.Get("qieDacCharge",fQieDacCharge);
00321 config.Get("qieDacPerRangeZeroAdc",fQieDacPerRangeZeroAdc);
00322 config.Get("qiePedestalWidthAdc",fQiePedestalWidthAdc);
00323 config.Get("qieAdcRms",fQieAdcRms);
00324 config.Get("qiePedestalDac",fQiePedestalDac);
00325 config.Get("qieSparsifyThresh",fQieSparsifyThresh);
00326 config.Get("doLookupNonlinearity",fDoLookupNonlinearity);
00327
00328 if(fQieSparsifyThresh<fQiePedestalDac)
00329 MSG("DetSim",Msg::kWarning) << "WARNING: The QIE sparsification threshold " << std::endl
00330 << " is less than the digital offset!" << std::endl
00331 << " This turns off all sparsification!" << std::endl;
00332
00333
00334 SetupTables();
00335 }