00001 #include "TCBMFirstProc.h"
00002
00003 #include "TGo4Log.h"
00004 #include "TH1.h"
00005 #include "TH2.h"
00006 #include "TObjArray.h"
00007
00008 #include "TGo4WinCond.h"
00009 #include "TGo4MbsEvent.h"
00010 #include "TGo4MbsSubEvent.h"
00011
00012 #include "TCBMEvent.h"
00013
00014 #include "TGo4EventErrorException.h"
00015
00016
00017
00018
00019 TString TCBMFirstProc::fDfltSetupScript = "setup.C";
00020
00021
00022 TCBMFirstProc::TCBMFirstProc():
00023 TGo4EventProcessor(),
00024 base::ProcMgr()
00025 {
00026 }
00027
00028
00029 TCBMFirstProc::TCBMFirstProc(const char* name) :
00030 TGo4EventProcessor(name),
00031 base::ProcMgr()
00032 {
00033 TGo4Log::Info("Create TCBMFirstProc %s", name);
00034
00035 if (ExecuteScript(fDfltSetupScript.Data()) == -1) {
00036 TGo4Log::Error("Cannot setup analysis with %s script", fDfltSetupScript.Data());
00037 throw TGo4EventErrorException(this);
00038 }
00039
00040 fTotalDataSize = 0;
00041 fNumMbsBufs = 0;
00042 fNumCbmEvents = 0;
00043 }
00044
00045 TCBMFirstProc::~TCBMFirstProc()
00046 {
00047 TGo4Log::Info("Input %ld Output %ld Total processed size = %ld", fNumMbsBufs, fNumCbmEvents, fTotalDataSize);
00048 }
00049
00050 Bool_t TCBMFirstProc::BuildEvent(TGo4EventElement* outevnt)
00051 {
00052
00053
00054 TGo4MbsEvent* mbsev = (TGo4MbsEvent*) GetInputEvent();
00055 base::Event* cbmev = (TCBMEvent*) outevnt;
00056
00057 if ((mbsev==0) || (cbmev==0))
00058 throw TGo4EventErrorException(this);
00059
00060 if (!IsKeepInputEvent()) {
00061
00062
00063
00064
00065
00066 fTotalDataSize += mbsev->GetIntLen()*4;
00067 fNumMbsBufs++;
00068
00069 TGo4MbsSubEvent* psubevt = 0;
00070 mbsev->ResetIterator();
00071 while((psubevt = mbsev->NextSubEvent()) != 0) {
00072
00073
00074 base::Buffer buf;
00075
00076 buf.makecopyof(psubevt->GetDataField(), psubevt->GetByteLen());
00077
00078 buf().kind = psubevt->GetProcid();
00079 buf().boardid = psubevt->GetSubcrate();
00080 buf().format = psubevt->GetControl();
00081
00082
00083
00084 ProvideRawData(buf);
00085 }
00086
00087
00088
00089
00090 ScanNewData();
00091
00092 if (IsRawAnalysis()) {
00093
00094
00095
00096 SkipAllData();
00097
00098 } else {
00099
00100
00101
00102
00103 if (AnalyzeSyncMarkers()) {
00104
00105
00106 CollectNewTriggers();
00107
00108
00109 ScanDataForNewTriggers();
00110 }
00111 }
00112 } else {
00113
00114 }
00115
00116 if (ProduceNextEvent(cbmev)) {
00117
00118
00119
00120 SetKeepInputEvent(kTRUE);
00121 outevnt->SetValid(kTRUE);
00122
00123 fNumCbmEvents++;
00124
00125 return kTRUE;
00126 }
00127
00128 SetKeepInputEvent(kFALSE);
00129
00130 outevnt->SetValid(kFALSE);
00131
00132 return kFALSE;
00133 }
00134
00135 base::H1handle TCBMFirstProc::MakeH1(const char* name, const char* title, int nbins, double left, double right, const char* xtitle)
00136 {
00137
00138
00139
00140
00141
00142 TString newxtitle(xtitle ? xtitle : "");
00143 TString xbins;
00144
00145 TObjArray* arr = newxtitle.Length() > 0 ? newxtitle.Tokenize(";") : 0;
00146
00147 for (int n=0; n<= (arr ? arr->GetLast() : -1);n++) {
00148 TString part = arr->At(n)->GetName();
00149 if (part.Index("xbin:")==0) { xbins = part; xbins.Remove(0, 5); }
00150 else newxtitle = part;
00151 }
00152 delete arr;
00153
00154 TH1* histo1 = MakeTH1('I', name, title, nbins, left, right, newxtitle.Data());
00155
00156 if (xbins.Length()>0) {
00157 arr = xbins.Tokenize(",");
00158 for (int n=0; n<=(arr ? arr->GetLast() : -1);n++)
00159 histo1->GetXaxis()->SetBinLabel(1 + n, arr->At(n)->GetName());
00160 delete arr;
00161 }
00162
00163 return (base::H1handle) histo1;
00164 }
00165
00166 base::H2handle TCBMFirstProc::MakeH2(const char* name, const char* title, int nbins1, double left1, double right1, int nbins2, double left2, double right2, const char* options)
00167 {
00168
00169
00170
00171
00172
00173
00174 TString xbins, ybins, xtitle, ytitle;
00175
00176 TObjArray* arr = (options && *options) ? TString(options).Tokenize(";") : 0;
00177
00178 for (int n=0; n<= (arr ? arr->GetLast() : -1);n++) {
00179 TString part = arr->At(n)->GetName();
00180 if (part.Index("xbin:")==0) { xbins = part; xbins.Remove(0, 5); } else
00181 if (part.Index("ybin:")==0) { ybins = part; ybins.Remove(0, 5); } else
00182 if (xtitle.Length()==0) xtitle = part;
00183 else ytitle = part;
00184 }
00185 delete arr;
00186
00187 TH2* histo2 = MakeTH2('I', name, title, nbins1, left1, right1, nbins2, left2, right2, xtitle.Data(), ytitle.Data());
00188
00189 if (xbins.Length()>0) {
00190 arr = xbins.Tokenize(",");
00191 for (int n=0; n<=(arr ? arr->GetLast():-1); n++)
00192 histo2->GetXaxis()->SetBinLabel(1 + n, arr->At(n)->GetName());
00193 delete arr;
00194 }
00195
00196 if (ybins.Length()>0) {
00197 arr = ybins.Tokenize(",");
00198 for (int n=0; n<=(arr ? arr->GetLast():-1); n++)
00199 histo2->GetYaxis()->SetBinLabel(1 + n, arr->At(n)->GetName());
00200 delete arr;
00201 }
00202
00203 return (base::H2handle) histo2;
00204 }
00205
00206 void TCBMFirstProc::FillH1(base::H1handle h1, double x, double weight)
00207 {
00208 if (h1==0) return;
00209
00210 TH1* histo1 = (TH1*) h1;
00211
00212 if (weight!=1.)
00213 histo1->Fill(x, weight);
00214 else
00215 histo1->Fill(x);
00216 }
00217
00218 void TCBMFirstProc::FillH2(base::H1handle h2, double x, double y, double weight)
00219 {
00220 if (h2==0) return;
00221
00222 TH2* histo2 = (TH2*) h2;
00223
00224 if (weight!=1.)
00225 histo2->Fill(x, y, weight);
00226 else
00227 histo2->Fill(x, y);
00228 }
00229
00230
00231 base::C1handle TCBMFirstProc::MakeC1(const char* name, double left, double right, base::H1handle h1)
00232 {
00233 TH1* histo1 = (TH1*) h1;
00234
00235 TGo4WinCond* cond = MakeWinCond(name, left, right, histo1 ? histo1->GetName() : 0);
00236
00237 return (base::C1handle) cond;
00238 }
00239
00240 void TCBMFirstProc::ChangeC1(base::C1handle c1, double left, double right)
00241 {
00242 TGo4WinCond* cond = (TGo4WinCond*) c1;
00243 if (cond) cond->SetValues(left, right);
00244 }
00245
00246 int TCBMFirstProc::TestC1(base::C1handle c1, double value, double* dist)
00247 {
00248 TGo4WinCond* cond = (TGo4WinCond*) c1;
00249 if (dist) *dist = 0.;
00250 if (cond==0) return 0;
00251 if (value < cond->GetXLow()) {
00252 if (dist) *dist = value - cond->GetXLow();
00253 return -1;
00254 } else
00255 if (value > cond->GetXUp()) {
00256 if (dist) *dist = value - cond->GetXUp();
00257 return 1;
00258 }
00259 return 0;
00260 }
00261
00262 double TCBMFirstProc::GetC1Limit(base::C1handle c1, bool isleft)
00263 {
00264 TGo4WinCond* cond = (TGo4WinCond*) c1;
00265 if (cond==0) return 0.;
00266 return isleft ? cond->GetXLow() : cond->GetXUp();
00267 }