- Timestamp:
- 10/01/04 11:55:29 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.cc
r5152 r5155 60 60 #include "TString.h" 61 61 62 #include <fstream> 63 62 64 ClassImp(MExtractTimeAndChargeDigitalFilter); 63 65 64 66 using namespace std; 65 67 66 const Byte_t MExtractTimeAndChargeDigitalFilter::fgHiGainFirst = 0; 67 const Byte_t MExtractTimeAndChargeDigitalFilter::fgHiGainLast = 14; 68 const Byte_t MExtractTimeAndChargeDigitalFilter::fgLoGainFirst = 3; 69 const Byte_t MExtractTimeAndChargeDigitalFilter::fgLoGainLast = 14; 70 const Int_t MExtractTimeAndChargeDigitalFilter::fgHiGainWindowSize = 6; 71 const Int_t MExtractTimeAndChargeDigitalFilter::fgLoGainWindowSize = 6; 72 const Int_t MExtractTimeAndChargeDigitalFilter::fgBinningResolution = 10; 68 const Byte_t MExtractTimeAndChargeDigitalFilter::fgHiGainFirst = 0; 69 const Byte_t MExtractTimeAndChargeDigitalFilter::fgHiGainLast = 14; 70 const Byte_t MExtractTimeAndChargeDigitalFilter::fgLoGainFirst = 3; 71 const Byte_t MExtractTimeAndChargeDigitalFilter::fgLoGainLast = 14; 72 const Int_t MExtractTimeAndChargeDigitalFilter::fgWindowSizeHiGain = 6; 73 const Int_t MExtractTimeAndChargeDigitalFilter::fgWindowSizeLoGain = 6; 74 const Int_t MExtractTimeAndChargeDigitalFilter::fgBinningResolutionHiGain = 10; 75 const Int_t MExtractTimeAndChargeDigitalFilter::fgBinningResolutionLoGain = 10; 73 76 // -------------------------------------------------------------------------- 74 77 // … … 92 95 SetBinningResolution(); 93 96 94 SetWeightsFile("");97 ReadWeightsFile(""); 95 98 } 96 99 … … 108 111 { 109 112 113 if (windowh != fgWindowSizeHiGain) 114 *fLog << warn << GetDescriptor() 115 << ": ATTENTION!!! If you are not Hendrik Bartko, do NOT use a different window size than the default." << endl; 116 if (windowl != fgWindowSizeLoGain) 117 *fLog << warn << GetDescriptor() 118 << ": ATTENTION!!! If you are not Hendrik Bartko, do NOT use a different window size than the default" << endl; 119 110 120 fWindowSizeHiGain = windowh; 111 121 fWindowSizeLoGain = windowl; … … 162 172 // If filenname is empty, then all weights will be set to 1. 163 173 // 164 void MExtractTimeAndChargeDigitalFilter::SetWeightsFile(TString filename)165 { 166 167 fAmpWeightsHiGain .Set(fBinningResolution *fWindowSizeHiGain);168 fAmpWeightsLoGain .Set(fBinningResolution *fWindowSizeLoGain);169 fTimeWeightsHiGain.Set(fBinningResolution *fWindowSizeHiGain);170 fTimeWeightsLoGain.Set(fBinningResolution *fWindowSizeLoGain);174 Bool_t MExtractTimeAndChargeDigitalFilter::ReadWeightsFile(TString filename) 175 { 176 177 fAmpWeightsHiGain .Set(fBinningResolutionHiGain*fWindowSizeHiGain); 178 fAmpWeightsLoGain .Set(fBinningResolutionLoGain*fWindowSizeLoGain); 179 fTimeWeightsHiGain.Set(fBinningResolutionHiGain*fWindowSizeHiGain); 180 fTimeWeightsLoGain.Set(fBinningResolutionLoGain*fWindowSizeLoGain); 171 181 172 182 if (filename.IsNull()) … … 182 192 fTimeWeightsLoGain[i] = 1.; 183 193 } 184 return; 185 } 186 187 TFile *f = new TFile(filename); 188 TH1F *hw_amp = (TH1F*)f->Get("hw_amp"); 189 TH1F *hw_time = (TH1F*)f->Get("hw_time"); 190 191 if (!hw_amp) 192 { 193 *fLog << warn << GetDescriptor() 194 << ": Could not find hw_amp in " << filename << endl; 195 return; 196 } 197 198 if (!hw_time) 199 { 200 *fLog << warn << GetDescriptor() 201 << ": Could not find hw_time in " << filename << endl; 202 return; 203 } 204 205 for (UInt_t i=0; i<fAmpWeightsHiGain.GetSize(); i++) 206 { 207 fAmpWeightsHiGain [i] = hw_amp->GetBinContent(i+1); 208 fTimeWeightsHiGain[i] = hw_time->GetBinContent(i+1); 209 } 210 for (UInt_t i=0; i<fAmpWeightsLoGain.GetSize(); i++) 211 { 212 fAmpWeightsLoGain [i] = hw_amp->GetBinContent(i+1); 213 fTimeWeightsLoGain[i] = hw_time->GetBinContent(i+1); 214 } 215 // cout << "w_amp[" << i << "] = " << fw_amp[i] << endl; 216 delete f; 194 return kTRUE; 195 } 196 197 ifstream fin(filename.Data()); 198 199 if (!fin) 200 { 201 *fLog << err << GetDescriptor() 202 << ": No weights file found: " << filename << endl; 203 return kFALSE; 204 } 205 206 Int_t len = 0; 207 Int_t cnt = 0; 208 Bool_t hi = kFALSE; 209 Bool_t lo = kFALSE; 210 211 TString str; 212 213 while (1) 214 { 215 216 str.ReadLine(fin); 217 if (!fin) 218 break; 219 220 221 if (str.Contains("# High Gain Weights:")) 222 { 223 str.ReplaceAll("# High Gain Weights:",""); 224 sscanf(str.Data(),"%2i%2i",&fWindowSizeHiGain,&fBinningResolutionHiGain); 225 *fLog << inf << "Found number of High Gain slices: " << fWindowSizeHiGain 226 << " and High Gain resolution: " << fBinningResolutionHiGain << endl; 227 len = fBinningResolutionHiGain*fWindowSizeHiGain; 228 fAmpWeightsHiGain .Set(len); 229 fTimeWeightsHiGain.Set(len); 230 hi = kTRUE; 231 continue; 232 } 233 234 if (str.Contains("# Low Gain Weights:")) 235 { 236 str.ReplaceAll("# Low Gain Weights:",""); 237 sscanf(str.Data(),"%2i%2i",&fWindowSizeLoGain,&fBinningResolutionLoGain); 238 *fLog << inf << "Found number of Low Gain slices: " << fWindowSizeLoGain 239 << " and Low Gain resolution: " << fBinningResolutionLoGain << endl; 240 len = fBinningResolutionLoGain*fWindowSizeHiGain; 241 fAmpWeightsLoGain .Set(len); 242 fTimeWeightsLoGain.Set(len); 243 lo = kTRUE; 244 continue; 245 } 246 247 if (str.Contains("#")) 248 continue; 249 250 if (len == 0) 251 continue; 252 253 sscanf(str.Data(),"\t%f\t%f",lo ? &fAmpWeightsLoGain [cnt] : &fAmpWeightsHiGain [cnt], 254 lo ? &fTimeWeightsLoGain[cnt] : &fTimeWeightsHiGain[cnt]); 255 256 if (++cnt == len) 257 { 258 len = 0; 259 cnt = 0; 260 } 261 } 262 263 if (cnt != len) 264 { 265 *fLog << err << GetDescriptor() 266 << ": Size mismatch in weights file " << filename << endl; 267 return kFALSE; 268 } 269 270 if (!hi) 271 { 272 *fLog << err << GetDescriptor() 273 << ": No correct header found in weights file " << filename << endl; 274 return kFALSE; 275 } 276 277 return kTRUE; 278 } 279 280 //---------------------------------------------------------------------------- 281 // 282 // Read a pre-defined weights file into the class. 283 // This is mandatory for the extraction 284 // 285 Bool_t MExtractTimeAndChargeDigitalFilter::WriteWeightsFile(TString filename) 286 { 287 288 289 Float_t amp[60]; 290 Float_t tim[60]; 291 292 ofstream fn(filename.Data()); 293 294 fn << "# High Gain Weights: 6 10" << endl; 295 fn << "# (Amplitude) (Time) " << endl; 296 297 for (UInt_t i=0; i<60; i++) 298 { 299 fn << "\t" << amp[i] << "\t" << tim[i] << endl; 300 } 301 302 fn << "# Low Gain Weights: 6 10" << endl; 303 fn << "# (Amplitude) (Time) " << endl; 304 305 for (UInt_t i=0; i<60; i++) 306 { 307 fn << "\t" << amp[i] << "\t" << tim[i] << endl; 308 } 309 return kTRUE; 217 310 } 218 311 … … 238 331 239 332 fLoGainSignal.Set(range); 333 334 fTimeShiftHiGain = (Float_t)fHiGainFirst + 0.5 + 1./fBinningResolutionHiGain; 335 fTimeShiftLoGain = (Float_t)fLoGainFirst + 0.5 + 1./fBinningResolutionLoGain; 240 336 241 337 return kTRUE; … … 300 396 return; 301 397 302 #if 0303 if (maxpos < 2)304 {305 time = (Float_t)fHiGainFirst;306 return;307 }308 #endif309 310 398 Float_t time_sum = 0.; 311 399 Float_t fmax = 0.; … … 334 422 for (Int_t sample=0; sample < fWindowSizeHiGain; sample++) 335 423 { 336 const Int_t idx = fBinningResolution *sample+fBinningResolutionHalf;424 const Int_t idx = fBinningResolutionHiGain*sample+fBinningResolutionHalfHiGain; 337 425 const Float_t pex = fHiGainSignal[sample+i]-PedMean[(sample+i+abflag) & 0x1]; 338 426 sum += fAmpWeightsHiGain [idx]*pex; … … 351 439 { 352 440 ftime_max /= fmax; 353 Int_t t_iter = Int_t(ftime_max*fBinningResolution );441 Int_t t_iter = Int_t(ftime_max*fBinningResolutionHiGain); 354 442 Int_t sample_iter = 0; 355 443 356 while ( t_iter > fBinningResolutionHalf -1 || t_iter < -fBinningResolutionHalf)357 { 358 if (t_iter > fBinningResolutionHalf -1)444 while ( t_iter > fBinningResolutionHalfHiGain-1 || t_iter < -fBinningResolutionHalfHiGain ) 445 { 446 if (t_iter > fBinningResolutionHalfHiGain-1) 359 447 { 360 t_iter -= fBinningResolution ;448 t_iter -= fBinningResolutionHiGain; 361 449 max_p--; 362 450 sample_iter--; 363 451 } 364 if (t_iter < -fBinningResolutionHalf )452 if (t_iter < -fBinningResolutionHalfHiGain) 365 453 { 366 t_iter += fBinningResolution ;454 t_iter += fBinningResolutionHiGain; 367 455 max_p++; 368 456 sample_iter++; … … 377 465 for (Int_t sample=0; sample < fWindowSizeHiGain; sample++) 378 466 { 379 const Int_t idx = fBinningResolution *sample + fBinningResolutionHalf+ t_iter;467 const Int_t idx = fBinningResolutionHiGain*sample + fBinningResolutionHalfHiGain + t_iter; 380 468 const Int_t ids = max_p + sample; 381 469 const Float_t pex = ids < 0 ? 0. : … … 383 471 sum += fAmpWeightsHiGain [idx]*pex; 384 472 time_sum += fTimeWeightsHiGain[idx]*pex; 385 // cout << idx << " " << fw_amp[idx] << " " << pex << " " << t_iter << " " << sum << " " << ((Int_t(p2-ptr)+abflag) & 0x1) << " " << PedMean[(Int_t(p2-ptr)+abflag) & 0x1] << endl;386 473 } 387 474 388 475 if (sum != 0.) 389 time = max_p + 1. + sample_iter 390 - ((Float_t)t_iter)/fBinningResolution - 0.4 - time_sum/sum 391 + (Float_t)fHiGainFirst; 476 time = max_p + fTimeShiftHiGain /* this shifts the time to the start of the rising edge */ 477 - ((Float_t)t_iter)/fBinningResolutionHiGain - time_sum/sum; 392 478 else 393 479 time = 0.; … … 404 490 { 405 491 406 const Byte_t *end = ptr + fLoGainLast - fLoGainFirst + 1; 407 408 Float_t time_sum = 0; 409 Float_t fmax = 0; 410 Float_t ftime_max = 0; 411 412 Float_t pedes = ped.GetPedestal(); 413 492 Int_t range = fLoGainLast - fLoGainFirst + 1; 493 const Byte_t *end = ptr + range; 494 Byte_t *p = ptr; 495 Byte_t maxpos = 0; 496 Byte_t max = 0; 497 Int_t count = 0; 498 499 // 500 // Check for saturation in all other slices 501 // 502 while (p<end) 503 { 504 505 fLoGainSignal[count++] = (Float_t)*p; 506 507 if (*p > max) 508 { 509 max = *p; 510 maxpos = p-ptr; 511 } 512 513 if (*p++ >= fSaturationLimit) 514 sat++; 515 } 516 517 Float_t time_sum = 0.; 518 Float_t fmax = 0.; 519 Float_t ftime_max = 0.; 520 Int_t max_p = 0; 521 522 const Float_t pedes = ped.GetPedestal(); 414 523 const Float_t ABoffs = ped.GetPedestalABoffset(); 415 524 … … 421 530 // Calculate the sum of the first fWindowSize slices 422 531 // 423 sat = 0; 424 Byte_t *p = ptr; 425 Byte_t *max_p = ptr; 426 427 while (p < end-fWindowSizeLoGain) 428 { 429 sum = 0; 430 time_sum = 0; 431 532 for (Int_t i=0;i<range-fWindowSizeLoGain;i++) 533 { 534 sum = 0.; 535 time_sum = 0.; 536 432 537 // 433 538 // Slide with a window of size fWindowSizeLoGain over the sample 434 539 // and multiply the entries with the corresponding weights 435 540 // 436 Byte_t *p1 = p++;437 541 for (Int_t sample=0; sample < fWindowSizeLoGain; sample++) 438 542 { 439 const Int_t idx = fBinningResolution*sample+fBinningResolutionHalf; 440 const Int_t adx = (Int_t(p1-ptr)+abflag) & 0x1; 441 sum += fAmpWeightsLoGain [idx]*(*p1-PedMean[adx]); 442 time_sum += fTimeWeightsLoGain[idx]*(*p1-PedMean[adx]); 443 p1++; 543 const Int_t idx = fBinningResolutionLoGain*sample+fBinningResolutionHalfLoGain; 544 const Float_t pex = fLoGainSignal[sample+i]-PedMean[(sample+i+abflag) & 0x1]; 545 sum += fAmpWeightsLoGain [idx]*pex; 546 time_sum += fTimeWeightsLoGain[idx]*pex; 444 547 } 445 548 … … 448 551 fmax = sum; 449 552 ftime_max = time_sum; 450 max_p = p;553 max_p = i; 451 554 } 452 } /* while (p<end-fWindowSizeLoGain) */453 454 555 } /* for (Int_t i=0;i<range-fWindowSizeLoGain;i++) */ 556 557 if (fmax!=0) 455 558 { 456 559 ftime_max /= fmax; 457 Int_t t_iter = Int_t(ftime_max*fBinningResolution );560 Int_t t_iter = Int_t(ftime_max*fBinningResolutionLoGain); 458 561 Int_t sample_iter = 0; 459 p = max_p; 460 461 while((t_iter)>fBinningResolutionHalf-1 || t_iter<-fBinningResolutionHalf) 462 { 463 if (t_iter>fBinningResolutionHalf-1) 562 563 while ( t_iter > fBinningResolutionHalfLoGain-1 || t_iter < -fBinningResolutionHalfLoGain ) 564 { 565 if (t_iter > fBinningResolutionHalfLoGain-1) 464 566 { 465 t_iter -= fBinningResolution ;466 p--;567 t_iter -= fBinningResolutionLoGain; 568 max_p--; 467 569 sample_iter--; 468 570 } 469 if (t_iter < -fBinningResolutionHalf )571 if (t_iter < -fBinningResolutionHalfLoGain) 470 572 { 471 t_iter += fBinningResolution ;472 p++;573 t_iter += fBinningResolutionLoGain; 574 max_p++; 473 575 sample_iter++; 474 576 } 475 577 } 476 578 477 sum = 0; 478 Byte_t *p2 = p; 479 579 sum = 0.; 480 580 // 481 581 // Slide with a window of size fWindowSizeLoGain over the sample … … 484 584 for (Int_t sample=0; sample < fWindowSizeLoGain; sample++) 485 585 { 486 const Int_t idx = fBinningResolution*sample+fBinningResolutionHalf+t_iter; 487 const Int_t adx = (Int_t(p2-ptr)+abflag) & 0x1; 488 sum += fAmpWeightsLoGain [idx]*(*p2-PedMean[adx]); 489 time_sum += fTimeWeightsLoGain[idx]*(*p2-PedMean[adx]); 490 p2++; 586 const Int_t idx = fBinningResolutionLoGain*sample + fBinningResolutionHalfLoGain + t_iter; 587 const Int_t ids = max_p + sample; 588 const Float_t pex = ids < 0 ? 0. : 589 ( ids > range ? 0. : fLoGainSignal[ids]-PedMean[(ids+abflag) & 0x1]); 590 sum += fAmpWeightsLoGain [idx]*pex; 591 time_sum += fTimeWeightsLoGain[idx]*pex; 491 592 } 492 593 493 if (sum != 0) 494 time = (Float_t)(max_p - ptr) + 1. + sample_iter 495 - (Float_t)t_iter/fBinningResolution - 0.4 - time_sum/sum 496 + (Float_t)fLoGainFirst; 594 if (sum != 0.) 595 time = max_p + fTimeShiftLoGain /* this shifts the time to the start of the rising edge */ 596 - ((Float_t)t_iter)/fBinningResolutionLoGain - time_sum/sum; 497 597 else 498 598 time = 0.; … … 527 627 if (IsEnvDefined(env, prefix, "BinningResolution", print)) 528 628 { 529 SetBinningResolution(GetEnvValue(env, prefix, "BinningResolution", fBinningResolution)); 629 SetBinningResolution(GetEnvValue(env, prefix, "BinningResolutionHiGain", fBinningResolutionHiGain), 630 GetEnvValue(env, prefix, "BinningResolutionLoGain", fBinningResolutionLoGain)); 530 631 rc = kTRUE; 531 632 } … … 536 637 } 537 638 639 Int_t MExtractTimeAndChargeDigitalFilter::PostProcess() 640 { 641 642 *fLog << endl; 643 *fLog << inf << "Used High Gain weights in the extractor: " << endl; 644 645 for (Int_t i=0;i<fBinningResolutionHiGain*fWindowSizeHiGain; i++) 646 *fLog << inf << fAmpWeightsHiGain[i] << " " << fTimeWeightsHiGain[i] << endl; 647 648 *fLog << endl; 649 *fLog << inf << "Used Low Gain weights in the extractor: " << endl; 650 for (Int_t i=0;i<fBinningResolutionLoGain*fWindowSizeLoGain; i++) 651 *fLog << inf << fAmpWeightsLoGain[i] << " " << fTimeWeightsLoGain[i] << endl; 652 653 return kTRUE; 654 }
Note:
See TracChangeset
for help on using the changeset viewer.