StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StGmtClusterMaker.cxx
1 //
2 // First Cluster Maker
3 // \class StGmtClusterMaker
4 // \authors K.S. Engle and Richard Witt (witt@usna.edu)
5 // based on StFgtClusterMaker
6 
7 // StRoot headers
8 #include "StGmtClusterMaker.h"
9 #include "StEvent/StEvent.h"
10 #include "StEvent/StGmtCollection.h"
11 #include "StEvent/StGmtHit.h"
12 #include "StGmtUtil/geometry/StGmtGeom.h"
13 #include "StMessMgr.h"
14 
15 // ROOT headers
16 #include "TSystem.h"
17 #include "TSpectrum.h"
18 
19 int StGmtClusterMaker::gmtStat = 0;
20 const unsigned int CLUS_BINS = 128;
21 const double CLUS_MIN = 0.0;
22 const double CLUS_MAX = 128*0.08;
23 const unsigned int MAX_PEAKS = 20;
24 
25 //_________________
26 inline Double_t MyGaus(Double_t x, Double_t mean, Double_t sigma, Double_t delta) {
27  return TMath::Freq((x-mean+delta/2)/sigma)-TMath::Freq((x-mean-delta/2)/sigma);
28 }
29 
30 //_________________
31 Double_t fpeaks(Double_t *x, Double_t *par) {
32  Double_t result=0.0;
33  for (UInt_t p=0;p<par[0];p++) {
34  Double_t norm = TMath::Exp(par[3*p+1]);
35  Double_t mean = par[3*p+2];
36  Double_t sigma = par[3*p+3];
37  result += norm*MyGaus(x[0],mean,sigma,0.08); //norm*TMath::Gaus(x[0],mean,sigma,1);
38  }
39  return result;
40 }
41 
42 //_________________
43 TF1* StGmtClusterMaker::FindPeaks(TH1F* hist) {
44  TSpectrum spect(MAX_PEAKS);
45  TF1 back("poly","pol0",CLUS_MIN,CLUS_MAX);
46  Double_t par[MAX_PEAKS*3+1];
47  spect.Search(hist,3);
48  auto xpeaks = spect.GetPositionX();
49 
50  hist->Fit(&back,"Q");
51  UInt_t npx=0;
52  UInt_t nfound = spect.GetNPeaks();
53  for(UInt_t i=0; i < nfound; i++) {
54  Double_t xp=xpeaks[i];
55  int bin=hist->GetXaxis()->FindBin(xp);
56  Double_t yp=hist->GetBinContent(bin);
57  Double_t err=hist->GetBinError(bin);
58  if(err<=0.0) continue;
59  if(bin<=1) continue;
60  if((yp-err*3) < back.GetParameter(0)) continue;
61  Double_t yp_left=hist->GetBinContent(bin-1);
62  Double_t yp_right=hist->GetBinContent(bin+1);
63  Double_t err_left=hist->GetBinError(bin-1);
64  Double_t err_right=hist->GetBinError(bin+1);
65  Double_t yp_sum=yp+yp_left+yp_right;
66  Double_t err_sum=TMath::Sqrt(err*err+err_left*err_left+err_right*err_right);
67  if((yp_sum-3*err_sum) < back.GetParameter(0)) continue;
68 
69  par[3*npx+1]=TMath::Log(yp);
70  par[3*npx+2]=xp;
71  par[3*npx+3]=3*0.08; // sigma
72  npx++;
73  }
74  if (Debug()) {LOG_INFO << hist->GetName() << " found " << nfound << " Accpeted " << npx << endm;}
75  if (! npx) return 0;
76 
77  TString funcName=Form("Func%s",hist->GetName());
78  TF1* fitFunc = nullptr;
79  fitFunc = (TF1*)gROOT->GetListOfFunctions()->FindObject(funcName);
80  if ( fitFunc ) delete fitFunc;
81  fitFunc = new TF1(funcName,fpeaks,CLUS_MIN,CLUS_MAX,3*npx+1);
82 
83  for(UInt_t i=0; i < npx; i++) {fitFunc->SetParLimits(3*i+3,0.08*0.5,10*0.08);}
84  fitFunc->SetParameters(par);
85  fitFunc->FixParameter(0,(double)npx);
86  fitFunc->SetNpx(1000);
87  fitFunc->SetLineColor(kGreen);
88 
89  TVirtualFitter::SetDefaultFitter("Fumili");
90  int isOk=hist->Fit(fitFunc);
91  if(isOk) isOk=hist->Fit(fitFunc);
92  if(isOk) return 0;
93 
94  return fitFunc;
95 }
96 
97 //_________________
98 void StGmtClusterMaker::ClusterBuilder(ULong_t events, UInt_t module, StGmtStripCollection& strips, StGmtHitCollection& hits) {
99  static TCanvas* canv=0;
100  static TH1F* histX=0;
101  static TH1F* histY=0;
102  static TProfile* profX[8]={0};
103  static TProfile* profY[8]={0};
104 
105  StGmtStrip* pStrip;
106  Double_t position;
107  UInt_t stripsNum=strips.getNumStrips();
108  int adc,adc_buf=0;
109  TString name, title;
110 // LOG_INFO << "STart TSpectrum" << endm;
111 // TSpectrum spectX(MAX_PEAKS); TSpectrum spectY(MAX_PEAKS);
112  TSpectrum spectX(); TSpectrum spectY();
113  LOG_INFO << "Created TSpectrum" << endm;
114  TH1F* histPointer;
115  TProfile* profPointer;
116 
117  if(!profX[module]) {
118  name="PedestalX_"; name += module;
119  profX[module]=new TProfile(name,name,CLUS_BINS,CLUS_MIN,CLUS_MAX,"s");
120  }
121  if(!profY[module]) {
122  name = "PedestalY_"; name += module;
123  profY[module]=new TProfile(name,name,CLUS_BINS,CLUS_MIN,CLUS_MAX,"s");
124  }
125  if(!histX) histX=new TH1F("ClusterX","ClusterX",CLUS_BINS,CLUS_MIN,CLUS_MAX);
126  if(!histY) histY=new TH1F("ClusterY","ClusterY",CLUS_BINS,CLUS_MIN,CLUS_MAX);
127 
128  if(Debug()>3) {
129  canv = (TCanvas *) gROOT->GetListOfCanvases()->FindObject("GmtClusters");
130  if(!canv) canv=new TCanvas("GmtClusters","GmtClusters",768,768);
131  else canv->Clear();
132  }
133 
134  histX->Reset(); histY->Reset();
135  TProfile profXold(*profX[module]); TProfile profYold(*profY[module]);
136  for(UInt_t iStrip=0;iStrip<stripsNum;iStrip++) {
137  adc=0;
138  pStrip=strips.getSortedStrip(iStrip);
139  if(!pStrip->isY()) { profPointer=profX[module]; histPointer=histX; }
140  else { profPointer=profY[module]; histPointer=histY; }
141  for(UInt_t iTimeBin=0;iTimeBin<kGmtNumTimeBins;iTimeBin++) {
142  adc_buf=pStrip->getAdc(iTimeBin);
143  if(adc_buf>-999) adc+=adc_buf;
144  }
145  position=pStrip->getPosition();
146  int bin=profPointer->Fill(position,adc);
147  Double_t error=TMath::Sqrt(adc);
148  histPointer->Fill(position,adc);
149  histPointer->SetBinError(bin,error);
150  }
151  if (events < 5) return;
152  histX->Add(&profXold,-1.0); histY->Add(&profYold,-1.0);
153 
154  TF1 *fitX=0, *fitY=0;
155  fitX = FindPeaks(histX); fitY = FindPeaks(histY);
156  UInt_t idx[MAX_PEAKS], idy[MAX_PEAKS];
157  UInt_t nClusX=0, nClusY=0;
158  if(fitX) {
159  for(UInt_t i=0; i<fitX->GetParameter(0); i++) {
160  if (fitX->GetParameter(3*i+3) > 0.4) continue;
161  if (fitX->GetParameter(3*i+1) < 5.0) continue;
162  idx[nClusX] = i;
163  nClusX++;
164  }
165  if (nClusX) *profX[module]=profXold;
166  if (Debug()) {LOG_INFO << "######XPEAKS found =" << fitX->GetParameter(0) << ", Clusters fitted =" << nClusX << endm;}
167  } else if (Debug()) {LOG_INFO << "######XNULL" << endm; }
168  if(fitY) {
169  for(UInt_t i=0; i<fitY->GetParameter(3*i+3); i++) {
170  if (fitY->GetParameter(3*i+3) > 0.4) continue;
171  if (fitY->GetParameter(3*i+1) < 5.0) continue;
172  idy[nClusY] = i;
173  nClusY++;
174  }
175  if (nClusY) *profY[module]=profYold;
176  if (Debug()) {LOG_INFO << "######YPEAKS found =" << fitY->GetParameter(0) << ", Clusters fitted =" << nClusY << endm;}
177  } else if (Debug()) {LOG_INFO << "######YNULL" << endm; }
178  for(UInt_t i=0; i < nClusX; i++) {
179  UInt_t nx = idx[i];
180  for(UInt_t j = 0; j < nClusY; j++) {
181  UInt_t ny = idy[j];
182  StGmtHit* newCluster = new StGmtHit(
183  hits.getHitVec().size(),
184  module,
185  TMath::Exp(fitX->GetParameter(3*nx+1)), // adcX
186  TMath::Exp(fitY->GetParameter(3*ny+1)), // adcY
187  fitX->GetParError(3*nx+1), // error(adcX)
188  fitY->GetParError(3*ny+1), // error(adcY)
189  fitX->GetParameter(3*nx+2), // meanX
190  fitY->GetParameter(3*ny+2), // meanY
191  fitX->GetParError(3*nx+2), // error(meanX)
192  fitY->GetParError(3*ny+2), // error(meanY)
193  fitX->GetParameter(3*nx+3), // sigmaX
194  fitY->GetParameter(3*ny+3), // sigmaY
195  fitX->GetParError(3*nx+3), // error(sigmaX)
196  fitY->GetParError(3*ny+3)); // error(sigmaY)
197  if (Debug()) newCluster->Print();
198  hits.getHitVec().push_back(newCluster);
199  }
200  }
201 
202  if (Debug()>3) {
203  canv->Divide(2,2);
204 
205  canv->cd(1);
206  profX[module]->Draw();
207  canv->cd(2);
208  profY[module]->Draw();
209 
210  canv->cd(3);
211  histX->Draw();
212  canv->cd(4);
213  histY->Draw();
214 
215  canv->Modified();
216  canv->Update();
217  canv->Draw();
218  if (nClusX || nClusY) {
219  while (!gSystem->ProcessEvents()){gSystem->Sleep(200);}
220  }
221  }
222 }
223 
224 //_________________
225 StGmtClusterMaker::StGmtClusterMaker( const Char_t* name ) : //StMaker(name),
226  StRTSBaseMaker( "clustser", name ) {
227  SetAttr("gmtCosmics" ,kFALSE);
228 }
229 
230 //_________________
232  LOG_INFO << "MAKE of StGmtClusterMaker" << endm;
233  Int_t ierr = kStOk;
234  static ULong_t nEvents=0;
235  //StEvent* eventPtr = 0;
236  //eventPtr = (StEvent*)GetInputDS("StEvent");
237  StEvent* eventPtr = (StEvent*) (GetInputDS("StEvent"));
238  //cout << "LLLLLLOOOOOOOOKKK!!!" << endl;
239  //cout << "TRACK NODES: " << endl;
240  //cout << eventPtr->trackNodes().size() << endl;
241  //cout << "TPC HIT COLLECTIONS!" << endl;
242  //cout << eventPtr->tpcHitCollection()->numberOfHits() << endl;
243  if(!eventPtr) {
244  LOG_ERROR << "Error getting pointer to StEvent from '" << ClassName() << "'" << endm;
245  return kStErr;
246  }
247  StGmtCollection* gmtCollectionPtr = eventPtr->gmtCollection();
248  if(!gmtCollectionPtr) {
249  LOG_WARN << "Error getting pointer to StGmtCollection from '" << ClassName() << "'" << endm;
250  return kStWarn;
251  }
252  UInt_t noModWithGMT = 0;
253  for(UInt_t moduleIdx=0; moduleIdx<gmtCollectionPtr->getNumModules(); moduleIdx++) {
254  if(Debug()) {
255  LOG_INFO << "module: " << moduleIdx << " has strips: \t" << gmtCollectionPtr->getNumStrips(moduleIdx) << endm;
256  LOG_INFO << "Collection =\t" << gmtCollectionPtr->getNumStrips() << "\t"
257  << gmtCollectionPtr->getNumHits() << '\t' << gmtCollectionPtr->getNumPoints() << '\t' << endm;
258  }
259  Int_t nelements = gmtCollectionPtr->getNumStrips(moduleIdx);
260  if(nelements < kGmtNumStripsPerModule) {
261  if(Debug()) {
262  LOG_WARN <<"StClusterMaker::Make(): no data for module " << moduleIdx << endm;
263  }
264  continue;
265  }
266  StGmtStripCollection *stripCollectionPtr = gmtCollectionPtr->getStripCollection(moduleIdx);
267  StGmtHitCollection *hitCollectionPtr = gmtCollectionPtr->getHitCollection(moduleIdx);
268  ClusterBuilder(nEvents,moduleIdx,*stripCollectionPtr,*hitCollectionPtr);
269  noModWithGMT++;
270  if(stripCollectionPtr && hitCollectionPtr && hitCollectionPtr->getHitVec().size()) {
271  if(Debug()) {
272  LOG_INFO << "Cluster " << stripCollectionPtr->getNumStrips() << "strips\tin module" << stripCollectionPtr->getModule() << endm;
273  }
274  }
275  }
276  if (noModWithGMT) nEvents++;
277 
278  if(Debug()) {
279  LOG_INFO << "End of gmt-clust-maker, print all strips & clusters: " << endm;
280  LOG_INFO <<" gmtCollnumModule=" << gmtCollectionPtr->getNumModules()<<", tot strip=" <<gmtCollectionPtr->getNumStrips()
281  <<" totClust=" << gmtCollectionPtr->getNumHits() <<endm;
282  }
283  if (IAttr("gmtCosmics")) {
284  if (! gmtCollectionPtr->getNumHits()) return kStERR;
285  }
286  if (Debug()) {
287  UShort_t NumModules = gmtCollectionPtr->getNumModules();
288  for (UShort_t m = 0; m < NumModules; m++) {
289  const StGmtHitCollection *coll = gmtCollectionPtr->getHitCollection(m);
290  if (! coll) continue;
291  const StSPtrVecGmtHit &hits = coll->getHitVec();
292  UInt_t NoHits = hits.size();
293  for (UInt_t l = 0; l < NoHits; l++) {
294  const StGmtHit *hit = hits[l];
295  if (hit) {
296  hit->Print("");
297  }
298  }
299  }
300  }
301  return ierr;
302 }
303 
304 //_________________
305 Int_t StGmtClusterMaker::Init() {
306  LOG_INFO << "INTI of StGmtClusterMaker" << endm;
307  if (IAttr("gmtCosmics")) SetAttr(".Privilege",kTRUE);
308  return kStOk;
309 }
Holds collections of GMT data.
StGmtHitCollection * getHitCollection(unsigned short moduleIdx)
Pointer to the GMT hit collection in the i-th module.
Class StRTSBaseMaker - is an abstract StMaker to define the interface to access the DAQ data from the...
size_t getNumStrips() const
Number total number of strips.
Int_t isY() const
Is it a pad?
Definition: StGmtStrip.h:45
Holds collection of GMT hits in the module.
Holds data for the hit in GMT.
Definition: StGmtHit.h:23
size_t getNumModules() const
Number of modules.
void Print(Option_t *option="") const
Print hit information (parameters)
Definition: StGmtHit.cxx:47
size_t getNumHits() const
Number total number of hits.
Float_t getPosition() const
Coordinate position relative to local origin (in module)
Definition: StGmtStrip.h:49
size_t getNumPoints() const
Number of points.
Definition: Stypes.h:42
StGmtStripCollection * getStripCollection(unsigned short moduleIdx)
Pointer to the GMT strip collection in the i-th module.
Short_t getAdc(Int_t tb=-1) const
ADC in a strip for a given time bin.
Definition: StGmtStrip.h:51
Holds data for the strip in GMT.
Definition: StGmtStrip.h:21
StSPtrVecGmtHit & getHitVec()
Vector of hits that belong to the module.
Definition: Stypes.h:44
Definition: Stypes.h:41
Definition: Stypes.h:45