00001 #include "StMuEEmcCrateTimingMaker.h"
00002
00003 #include "StChain.h"
00004 #include "TF1.h"
00005
00006 #include "StMuDSTMaker/COMMON/StMuDstMaker.h"
00007 #include "StMuDSTMaker/COMMON/StMuDst.h"
00008 #include "StMuDSTMaker/COMMON/StMuEmcCollection.h"
00009
00010 #include "StMuDSTMaker/EZTREE/EztEventHeader.h"
00011 #include "StMuDSTMaker/EZTREE/EztEmcRawData.h"
00012
00013 ClassImp(StMuEEmcCrateTimingMaker)
00014
00015 StMuEEmcCrateTimingMaker::StMuEEmcCrateTimingMaker(StMuDstMaker* mudstmaker){
00016 mMuDstMaker = mudstmaker;
00017 assert(mMuDstMaker);
00018 mNsigma = -1.0;
00019 mNchannels = 25;
00020 mPhase = 0;
00021 mCycle = 16;
00022 mMinCounts = 0;
00023 }
00024
00025
00026 Int_t StMuEEmcCrateTimingMaker::Init() {
00027
00029 TString outputFile;
00030 outputFile = mDirectory + "/eemc" + mFlavor + "Crates-timeDelay";
00031 char samus[100];
00032 sprintf(samus,"%0.2f.root",mTimeDelay);
00033 outputFile += samus;
00034 mOutputFile = new TFile(outputFile.Data(),"RECREATE");
00035 mOutputTree = new TTree("ints","Integrals of Each Channel");
00036 kludge = MxMapmtFeeCh;
00037
00040 mOutputTree->Branch("kludge",&kludge,"kludge/I");
00041 mOutputTree->Branch("chanint",totalIntegral,"chanint[kludge]/F");
00042 mOutputTree->Branch("chanerr",totalError,"chanerr[kludge]/F");
00043 mOutputTree->Branch("delay",&mTimeDelay,"delay/F");
00044 mOutputTree->Branch("channelIds",channelIds,"channelIds[kludge]/I");
00045 mOutputTree->Branch("crateIds",crateIds,"crateIds[kludge]/I");
00046 mOutputTree->Branch("mPhase",&mPhase,"mPhase/I");
00047 mOutputTree->Branch("mCycle",&mCycle,"mCycle/I");
00048 mOutputFile->cd();
00049 TString tmpstr = "histograms for " + mFlavor + " crate channels";
00050
00052 cratehist = new TH2F("cratehistogram",tmpstr.Data(),MxMapmtFeeCh,-0.5,MxMapmtFeeCh + 0.5,500,0,500);
00053 cratehist->GetXaxis()->SetTitle("crate channel id");
00054 cratehist->GetYaxis()->SetTitle("adc");
00055
00057 for(int i=0; i<MxMapmtFeeCh; i++) {
00058 totalIntegral[i] = 0;
00059 totalError[i]=0;
00060 channelIds[i]=0;
00061 }
00062
00063 return 0;
00064 }
00065
00066
00067 Int_t StMuEEmcCrateTimingMaker::Make(){
00068
00069
00070 EztEventHeader* header= mMuDstMaker->muDst()->eztHeader();
00071 if(header==0)
00072 return kStOK;
00073
00075 int token= header->getToken();
00076 EztEmcRawData* eE;
00077 int lenCount;
00078 if(mFlavor == "tower") {
00079 eE=mMuDstMaker->muDst()->eztETow();
00080 lenCount = 4+160;
00081 } else if(mFlavor == "mapmt") {
00082 eE=mMuDstMaker->muDst()->eztESmd();
00083 lenCount = 4+192;
00084 }
00085 else {
00086 std::cout << "Unknown flavor you fool! " << mFlavor << std::endl << std::flush;
00087 return kStWarn;
00088 }
00089
00090 assert(eE);
00091
00092
00093
00094 int errFlag=0;
00095 int trigComm=0x4;
00096
00097 int nOK=0;
00098 for(int icr=0;icr<eE->getNBlocks();icr++) {
00099 if(eE->isCrateVoid(icr)) continue;
00100 if(eE->purgeCrateOFF(icr)) continue;
00101 int crID=icr+1;
00102 if (mFlavor=="mapmt") crID=64+icr;
00103 eE->tagHeadValid(icr,token, crID,lenCount,trigComm,errFlag);
00104 UShort_t isSick=eE->getCorruption(icr);
00105 if(isSick) continue;
00106 nOK++;
00107 }
00108
00109 if((mFlavor == "mapmt" && nOK!=MaxMapmtCrates) ||
00110 (mFlavor == "tower" && nOK!=MaxTwCrates)) {
00111 cout << "SICK EVENTS IN THIS RUN" << endl;
00112 return kStOK;
00113 }
00114
00115
00116 totalIntegral[MxMapmtFeeCh-1]++;
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00139
00140
00141
00143 for(int icr=0; icr < eE->getNBlocks(); icr++ ) {
00144
00145 if(eE->isCrateVoid(icr)) continue;
00146 const UShort_t* data=eE->data(icr);
00147 int j = icr*MaxMapmtCrateCh/16;
00148
00152 for(int i=mPhase;i<eE->sizeData(icr);i+=mCycle,j++) {
00153
00154 if(i>=MaxMapmtCrateCh) continue;
00155 float adc=data[i];
00156 int k=(icr*MaxMapmtCrateCh+i)/16;
00157 assert(k>=0 && k<MxMapmtFeeCh);
00158 cratehist->Fill(j,adc);
00162 channelIds[j]=i;
00163 crateIds[j]=icr;
00164
00165 }
00166
00167 }
00168 return kStOK;
00169 }
00170
00171
00172
00173 Int_t StMuEEmcCrateTimingMaker::Finish() {
00174
00175
00179
00180
00182 TString outputFile = mDirectory + "/eemc" + mFlavor + "Crates-timeDelay";
00183 char samus[100];
00184 sprintf(samus,"%0.2f.txt",mTimeDelay);
00185 outputFile += samus;
00186 ofstream ofs(outputFile.Data());
00187 ofs << "-1" << "\t" << setprecision(5) << totalIntegral[MxMapmtFeeCh-1] << endl;
00188
00189 for (Int_t i = 2; i <= cratehist->GetXaxis()->GetNbins(); i++) {
00190 Int_t chanId = static_cast<Int_t>(cratehist->GetXaxis()->GetBinCenter(i));
00191
00192 TH1D* proj = cratehist->ProjectionY("projTemp",i,i);
00193 if (proj) {
00194
00197 if ( proj -> GetEntries() < mMinCounts )
00198 continue;
00199
00200 Int_t maxBin = 0;
00201 Float_t maxValue = -1;
00202 for (Int_t j = 1; j < proj->GetXaxis()->GetNbins(); j++) {
00203 if (proj->GetBinContent(j) > maxValue) {
00204 maxBin = j;
00205 maxValue = proj->GetBinContent(j);
00206 }
00207 }
00208 Float_t pedMean = proj->GetXaxis()->GetBinCenter(maxBin);
00209
00210 if ( !(( i - mPhase )%mCycle) ) {
00211
00212 TString myname="channel"; myname+=chanId;
00213 TH1D *cl=(TH1D*)proj->Clone(myname);
00214 cl->Write();
00215
00216 }
00217
00218
00219
00220 TF1* gaus = new TF1("gaus","gaus");
00221 gaus->SetParameter(0,maxValue);
00222 gaus->SetParameter(1,pedMean);
00223 gaus->SetParameter(2,3.5);
00224 gaus->SetRange(pedMean-10,pedMean+10);
00225 proj->Fit(gaus,"0RQ");
00226 Float_t pedestalmean = gaus->GetParameter(1);
00227 Float_t pedestalwidth = gaus->GetParameter(2);
00228
00230 Float_t xmin = pedestalmean;
00231 if ( mNsigma > 0. )
00232 xmin += mNsigma * pedestalwidth;
00233 else
00234 xmin += mNchannels;
00235 Int_t minBin = proj->GetXaxis()->FindFixBin( xmin );
00236
00237 maxBin = proj->GetXaxis()->GetNbins() - 1;
00238 Stat_t nHitsAbovePedestal = proj->Integral(minBin,maxBin);
00239
00241 Float_t delR = 0.;
00242 if ( nHitsAbovePedestal > 0. && totalIntegral[MxMapmtFeeCh-1] > 0. ) {
00243 delR = 1.0 / nHitsAbovePedestal + 1.0 / totalIntegral[MxMapmtFeeCh-1];
00244 }
00245
00246 totalIntegral[chanId] = nHitsAbovePedestal/totalIntegral[MxMapmtFeeCh-1];
00247 totalError[chanId] = delR * totalIntegral[chanId];
00248
00249 ofs << chanId << "\t" << setprecision(5) << nHitsAbovePedestal/totalIntegral[MxMapmtFeeCh-1] << endl;
00250 }
00251
00252 }
00253 ofs.close();
00254
00255 if (mOutputFile) {
00256 mOutputTree->Fill();
00257 mOutputFile->Write();
00258 delete mOutputFile;
00259 }
00260 return kStOk;
00261 }