00001
00002
00003
00004
00005
00006
00007
00009 #include "MuCalFitterModule.h"
00010 #include "SpectraFit.C"
00011 #include "MuCalTrunc.h"
00012 #include <cstdio>
00013 #include <fstream>
00014 #include <cmath>
00015
00016 #include "MessageService/MsgService.h"
00017 #include "MinosObjectMap/MomNavigator.h"
00018 #include "CandData/CandRecord.h"
00019 #include "CandDigit/CandDigitListHandle.h"
00020 #include "CandDigit/CandDigitHandle.h"
00021 #include "RecoBase/CandClusterHandle.h"
00022 #include "RecoBase/CandShowerHandle.h"
00023 #include "CandTrackSR/CandTrackSRHandle.h"
00024 #include "CandTrackSR/CandTrackSRListHandle.h"
00025 #include "RawData/RawDigit.h"
00026 #include "RawData/RawHeader.h"
00027 #include "RawData/RawRecord.h"
00028 #include "RawData/RawChannelId.h"
00029 #include "RawData/RawDaqSnarlHeader.h"
00030 #include "RawData/RawDaqHeaderBlock.h"
00031 #include "RawData/RawDigitDataBlock.h"
00032 #include "RecoBase/CandShowerListHandle.h"
00033 #include "UgliGeometry/UgliGeomHandle.h"
00034 #include "UgliGeometry/UgliStripHandle.h"
00035 #include "Validity/VldContext.h"
00036 #include "Conventions/Munits.h"
00037 #include "JobControl/JobCommand.h"
00038 #include "JobControl/JobCModuleRegistry.h"
00039 #include "Calibrator/CalADCtoPE.h"
00040 #include "DatabaseInterface/DbiResultPtr.h"
00041 #include "Calibrator/Calibrator.h"
00042
00043 CVSID("$Id: MuCalFitterModule.cxx,v 1.12 2006/12/01 19:15:58 rhatcher Exp $");
00044 JOBMODULE(MuCalFitterModule,"MuCalFitterModule","Histogram and Fit Muon Energy Spectra for each Strip End");
00045
00046
00047
00048 MuCalFitterModule::MuCalFitterModule() :
00049
00050 fWriteHistos(true),fwhichfit(0)
00051
00052 {
00053
00054 ffile = new TFile("muonfits.root","RECREATE");
00055
00056 char histname[256];
00057 for(int i=0;i<150;i++){
00058 for(int j=0;j<192;j++){
00059 for(int k=0;k<2;k++){
00060 sprintf(histname,"muhist%i_%i_%i",i,j,k);
00061 fHistoArray[i][j][k] = new TH1F(histname,histname,120,-0.5,19.5);
00062 fgain[i][j][k]=-1;
00063 fspewidth[i][j][k]=-1;
00064 }
00065 }
00066 }
00067
00068
00069 fhistoraw0 = new TH1F("historaw0","historaw0",1400,-0.5,139.5);
00070 fhistoraw1 = new TH1F("historaw1","historaw1",1400,-0.5,139.5);
00071 fhistoang0 = new TH1F("histoang0","histoang0",1400,-0.5,139.5);
00072 fhistoang1 = new TH1F("histoang1","histoang1",1400,-0.5,139.5);
00073 fhistoatt0 = new TH1F("histoatt0","histoatt0",1400,-0.5,139.5);
00074 fhistoatt1 = new TH1F("histoatt1","histoatt1",1400,-0.5,139.5);
00075
00076 }
00077
00078
00079
00080 void MuCalFitterModule::HandleCommand(JobCommand* cmd)
00081 {
00082
00083
00084
00085
00086
00087 if (cmd->HaveCmd()) {
00088 string sc = cmd->PopCmd();
00089 if (sc == "WriteHistos") {
00090 fWriteHistos = true;
00091 return;
00092 }
00093
00094 else if (sc == "NoFit") {
00095 fwhichfit = 0;
00096 return;
00097 }
00098
00099 else if (sc == "SimpleFit") {
00100 fwhichfit = 1;
00101 return;
00102 }
00103
00104 else if (sc == "ComplexFit") {
00105 fwhichfit = 2;
00106 return;
00107 }
00108
00109
00110 MSG("Demo",Msg::kWarning) << "Don't understand '" << sc.c_str() << "'\n";
00111 }
00112 }
00113
00114
00115
00116 JobCResult MuCalFitterModule::Ana(const MomNavigator *mom)
00117 {
00118
00119 JobCResult result(JobCResult::kPassed);
00120
00121 int vetoplane[500];
00122 for(int i=0;i<500;i++) vetoplane[i]=0;
00123
00124 CandRecord* candrec = dynamic_cast<CandRecord*>
00125 (mom->GetFragment("CandRecord", "PrimaryCandidateRecord"));
00126 if (candrec==0) {
00127 result.SetWarning().SetFailed();
00128 return result;
00129 }
00130
00131 CandTrackListHandle *tracklist = dynamic_cast<CandTrackListHandle*>
00132 (candrec->FindCandHandle("CandTrackListHandle"));
00133 if (!tracklist) {
00134 return result;
00135 }
00136
00137 CandShowerListHandle *showerlist = dynamic_cast<CandShowerListHandle*>
00138 (candrec->FindCandHandle("CandShowerListHandle"));
00139
00140
00141
00142
00143
00145
00146 RawRecord *rr = dynamic_cast<RawRecord *>(mom->GetFragment("RawRecord"));
00147 if (rr == 0) {
00148 return result;
00149 }
00150
00151 VldContext vc = rr->GetRawHeader()->GetVldContext();
00152
00153 const RawDigitDataBlock *rddb = dynamic_cast<const RawDigitDataBlock *>
00154 (rr->FindRawBlock("RawDigitDataBlock"));
00155 if (rddb == 0) {
00156 return result;
00157 }
00158
00160
00161 if(showerlist!=0){
00162
00163 TIter showerItr(showerlist->GetDaughterIterator());
00164
00165 while (CandShowerHandle *shower = dynamic_cast<CandShowerHandle*>(showerItr())) {
00166 CandShowerHandle *showersr = 0;
00167 if(shower->InheritsFrom("CandShowerHandle")) {
00168 showersr = dynamic_cast<CandShowerHandle*>(shower);
00169 }
00170
00171 for(int i=0;i<=shower->GetLastCluster();i++){
00172 const CandClusterHandle *cluster = shower->GetCluster(i);
00173 for(int j=cluster->GetBegPlane();j<=cluster->GetEndPlane();j++) vetoplane[j] = 1;
00174 }
00175 }
00176 }
00177
00179
00180 TIter trackItr(tracklist->GetDaughterIterator());
00181
00182 while (CandTrackHandle *track = dynamic_cast<CandTrackHandle*>(trackItr())) {
00183 CandTrackSRHandle *tracksr = 0;
00184 if (track->InheritsFrom("CandTrackSRHandle")) {
00185 tracksr = dynamic_cast<CandTrackSRHandle*>(track);
00186 }
00187
00188 TIter stripItr(track->GetDaughterIterator());
00189
00190 while (CandStripHandle *strip = dynamic_cast<CandStripHandle*>(stripItr())) {
00191 if(strip->GetNDigit(StripEnd::kWhole)==2 &&
00192 vetoplane[strip->GetPlane()]==0){
00193
00194 TIter digitItr(strip->GetDaughterIterator());
00195 PlexStripEndId *plexstripendid=0;
00196 UgliGeomHandle *ugh = 0;
00197 PlexSEIdAltLItem *plexstripenditem=0;
00198
00199 int cur_strip = strip->GetStrip();
00200 int cur_plane = strip->GetPlane();
00201
00202 while (CandDigitHandle *digit = dynamic_cast<CandDigitHandle*>(digitItr())) {
00203
00204 plexstripendid =
00205 new PlexStripEndId(digit->GetPlexSEIdAltL().GetBestSEId());
00206
00207 ugh = new UgliGeomHandle(*digit->GetVldContext());
00208 UgliStripHandle striphandle = ugh->GetStripHandle(*plexstripendid);
00209
00210 plexstripenditem =
00211 new PlexSEIdAltLItem(digit->GetPlexSEIdAltL().GetBestItem());
00212
00213 DbiResultPtr<CalADCtoPE> resptr;
00214 resptr.NewQuery(vc,0);
00215 const CalADCtoPE* dpgc = resptr.GetRowByIndex(plexstripendid->BuildPlnStripEndKey());
00216 if(dpgc ==0) {
00217 MSG("MuCal",Msg::kWarning)
00218 << "No database row for StripEnd " << *plexstripendid
00219 << " (indexed as " << plexstripendid->BuildPlnStripEndKey()
00220 << " )\n";
00221 }
00222
00223 Calibrator::Instance().Reset(vc);
00224
00225 float WLSLen = 0;
00226 float ToZero = 0;
00227 double plcor = 0;
00228 double attcor = 0;
00229
00230 switch (digit->GetPlexSEIdAltL().GetBestSEId().GetEnd()) {
00231 case StripEnd::kNegative: case StripEnd::kWhole: case StripEnd::kUnknown:
00232
00233 if(fgain[cur_plane][cur_strip][0]==-1) {
00234 fgain[cur_plane][cur_strip][0]= dpgc->GetGain();
00235 fspewidth[cur_plane][cur_strip][0] = dpgc->GetSPEWidth();
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245 WLSLen = striphandle.GetHalfLength()
00246 + striphandle.WlsPigtail(StripEnd::kNegative);
00247
00248 if (strip->GetPlaneView()==PlaneView::kU) {
00249 ToZero = -track->GetV(cur_plane);
00250 WLSLen += ToZero;
00251 attcor = TMath::Exp(ToZero/6.);
00252 plcor = 4.050 / (4.050 + fabs(tracksr->GetDirCosU(cur_plane)
00253 /tracksr->GetDirCosZ(cur_plane)));
00254 }
00255
00256 else if (strip->GetPlaneView()==PlaneView::kV) {
00257 ToZero = track->GetU(cur_plane);
00258 WLSLen += ToZero;
00259 attcor = TMath::Exp(ToZero/6.);
00260 plcor = 4.050 / (4.050 + fabs(tracksr->GetDirCosV(cur_plane)
00261 /tracksr->GetDirCosZ(cur_plane)));
00262 }
00263
00264
00265 fhistoraw0->Fill(plexstripenditem->GetPE());
00266
00267 fhistoang0->Fill(plexstripenditem->GetPE()
00268 *tracksr->GetDirCosZ(cur_plane)/plcor);
00269
00270 fhistoatt0->Fill(plexstripenditem->GetPE()
00271 *attcor*tracksr->GetDirCosZ(cur_plane)/plcor);
00272
00273 fHistoArray[cur_plane][cur_strip][0]
00274 ->Fill(plexstripenditem->GetPE()
00275 *attcor*tracksr->GetDirCosZ(cur_plane)/plcor);
00276
00277 break;
00278
00279
00280 case StripEnd::kPositive:
00281
00282 if(fgain[cur_plane][cur_strip][1]==-1) {
00283 fgain[cur_plane][cur_strip][1]= dpgc->GetGain();
00284 fspewidth[cur_plane][cur_strip][1] = dpgc->GetSPEWidth();
00285 }
00286
00287 WLSLen = striphandle.GetHalfLength()
00288 + striphandle.WlsPigtail(StripEnd::kPositive);
00289
00290 if (strip->GetPlaneView()==PlaneView::kU) {
00291 ToZero = track->GetV(cur_plane);
00292 WLSLen += ToZero;
00293 attcor = TMath::Exp(ToZero/6.);
00294 plcor = 4.050 / (4.050 + fabs(tracksr->GetDirCosU(cur_plane)
00295 /tracksr->GetDirCosZ(cur_plane)));
00296 }
00297
00298 else if (strip->GetPlaneView()==PlaneView::kV) {
00299 ToZero = -track->GetU(cur_plane);
00300 WLSLen += ToZero;
00301 attcor = TMath::Exp(ToZero/6.);
00302 plcor = 4.050 / (4.050 + fabs(tracksr->GetDirCosV(cur_plane)
00303 /tracksr->GetDirCosZ(cur_plane)));
00304 }
00305
00306 fhistoraw1->Fill(plexstripenditem->GetPE());
00307
00308 fhistoang1->Fill(plexstripenditem->GetPE()
00309 *tracksr->GetDirCosZ(cur_plane)/plcor);
00310
00311 fhistoatt1->Fill(plexstripenditem->GetPE()
00312 *attcor*tracksr->GetDirCosZ(cur_plane)/plcor);
00313
00314 fHistoArray[cur_plane][cur_strip][1]
00315 ->Fill(plexstripenditem->GetPE()
00316 *attcor*tracksr->GetDirCosZ(cur_plane)/plcor);
00317
00318 break;
00319
00320 }
00321 }
00322
00323 delete plexstripendid;
00324 delete ugh;
00325
00326 }
00327 }
00328
00329 }
00330
00331 return result;
00332
00333 }
00334
00335
00336
00337 void MuCalFitterModule::EndJob()
00338 {
00339
00340 if(fwhichfit==1 || fwhichfit==2)
00341 MSG("User",Msg::kInfo) << "Starting to Fit... " << endl;
00342
00343 int numpar;
00344 if(fwhichfit==0) numpar = 0;
00345 else if(fwhichfit==1) numpar = 10;
00346 else if(fwhichfit==2) numpar = 12;
00347 else {
00348 numpar=0;
00349 MSG("User",Msg::kInfo) << "Not Fitting! No fit type specified" << endl;
00350 }
00351
00352 ofstream mudat;
00353 mudat.open("muonfits.dat");
00354
00355 TH1F *gains = new TH1F("gains","gains",250,-50,200);
00356
00357 ffile->cd();
00358
00359 for(int i=0;i<50;i++){
00360 for(int j=0;j<192;j++) {
00361 for(int k=0;k<2;k++) {
00362
00363 gains->Fill(fgain[i][j][k]);
00364
00365 if(fHistoArray[i][j][k]->GetEntries()>400){
00366
00367 MuCalTrunc trunc(fHistoArray[i][j][k]);
00368 double *trunc_info = trunc.GetTruncMean();
00369
00370 double *fit_info = 0;
00371 if(fwhichfit==1) fit_info =
00372 SpectraFit(fHistoArray[i][j][k],fgain[i][j][k]);
00373 else if(fwhichfit==2)
00374 fit_info = BigSpectraFit(fHistoArray[i][j][k],
00375 fgain[i][j][k],fspewidth[i][j][k]);
00376
00377 if (fWriteHistos) fHistoArray[i][j][k]->Write();
00378
00379 mudat << i << " " << j << " " << k << " ";
00380 for(int i=0;i<numpar;i++) mudat << fit_info[i] << " ";
00381 mudat << trunc_info[0] << " " << trunc_info[1] << endl;
00382
00383 delete fit_info;
00384 delete [] trunc_info;
00385 }
00386
00387 }
00388 }
00389 }
00390
00391 gains->Write();
00392 delete gains;
00393
00394 for(int i=0;i<2;i++){
00395
00396 double *fit_info = 0;
00397
00398 MuCalTrunc trunc;
00399
00400 if(i==0) trunc.SetHisto(fhistoang0);
00401 else trunc.SetHisto(fhistoang1);
00402
00403 double *trunc_info = trunc.GetTruncMean();
00404
00405 if(i==0) {
00406 if(fwhichfit==1) fit_info = SpectraFit(fhistoraw0,1);
00407 else if(fwhichfit==2)
00408 fit_info = BigSpectraFit(fhistoraw0,1,0.5);
00409 }
00410
00411 else {
00412 if(fwhichfit==1) fit_info = SpectraFit(fhistoraw1,1);
00413 else if(fwhichfit==2)
00414 fit_info = BigSpectraFit(fhistoraw1,1,0.5);
00415 }
00416
00417 mudat << "-1 " << "-1 " << i << " ";
00418 for(int i=0;i<numpar;i++) mudat << fit_info[i] << " ";
00419 mudat << trunc_info[0] << " " << trunc_info[1] << endl;
00420
00421 delete trunc_info;
00422 delete fit_info;
00423
00424 }
00425
00426 if(fWriteHistos) {
00427 fhistoraw0->Write();
00428 fhistoraw1->Write();
00429 fhistoang0->Write();
00430 fhistoang1->Write();
00431 fhistoatt0->Write();
00432 fhistoatt1->Write();
00433 }
00434
00435 mudat.close();
00436 ffile->Close();
00437
00438 }
00440
00441
00442
00443