StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
makeMuDstQA.C
1 //Xianglei Zhu, created on May 26, 2009
2 //General MuDST QA macro with StMuDSTMaker
3 //Run it with the wrapper in ACLIC mode, CINT mode for debug ONLY
4 
5 #ifndef __CINT__
6 #include "TROOT.h"
7 #include "TSystem.h"
8 #include <iostream>
9 #include "TH1.h"
10 #include "TH2.h"
11 #include "TH3.h"
12 #include "TFile.h"
13 #include "TTree.h"
14 #include "TChain.h"
15 #include "TTreeHelper.h"
16 #include "TDatime.h"
17 #include "StarRoot/TUnixTime.h"
18 #include "StChain.h"
19 #include "StMessMgr.h"
20 #include "StMuDSTMaker/COMMON/StMuDstMaker.h"
21 #include "StMuDSTMaker/COMMON/StMuDst.h"
22 #include "StMuDSTMaker/COMMON/StMuEvent.h"
23 #include "StMuDSTMaker/COMMON/StMuTrack.h"
24 #include "StMuDSTMaker/COMMON/StMuPrimaryVertex.h"
25 #include "StBTofHeader.h"
26 #include "StThreeVectorF.hh"
27 #include "StPhysicalHelixD.hh"
28 #endif
29 
30 void makeMuDstQA(TString InputFileList, Int_t nFiles = 1, Int_t nEvents = 0, TString OutputDir = "output/" );
31 
32 void makeMuDstQA(TString InputFileList, Int_t nFiles, Int_t nEvents, TString OutputDir )
33 {
34 
35  // Load libraries for CINT mode
36 #ifdef __CINT__
37  gROOT -> Macro("loadMuDst.C");
38 #endif
39 
40  // List of member links in the chain
41  StChain* chain = new StChain ;
42 
43  StMuDstMaker* muDstMaker = new StMuDstMaker(0,0,"",InputFileList,"MuDst",nFiles) ;
44 
45  // ---------------- modify here according to your QA purpose --------------------------
46  // Turn off everything but Primary tracks in order to speed up the analysis and eliminate IO
47  muDstMaker -> SetStatus("*",0) ; // Turn off all branches
48  muDstMaker -> SetStatus("MuEvent",1) ; // Turn on the Event data (esp. Event number)
49  muDstMaker -> SetStatus("PrimaryVertices",1) ; // Turn on the primary track data
50  muDstMaker -> SetStatus("PrimaryTracks",1) ; // Turn on the primary track data
51  muDstMaker -> SetStatus("GlobalTracks",1) ; // Turn on the global track data
52  muDstMaker -> SetStatus("CovGlobTrack",1); // to fix the refmult in Run14!!!
53  muDstMaker -> SetStatus("BTofHeader",1) ; // Turn on the btof data
54  muDstMaker -> SetDebug(0) ; // Turn off Debug information
55 
56  if ( nEvents == 0 ) nEvents = 10000000 ; // Take all events in nFiles if nEvents = 0
57 
58  // ---------------- modify here according to your QA purpose --------------------------
59  //book histograms or trees if you need
60  TString oFile(muDstMaker->GetFile());
61  TString oChopFile;
62  int fileBeginIndex = oFile.Index("st_",0);
63  oFile.Remove(0,fileBeginIndex);
64  short indx1 = oFile.First('.');
65  short indx2 = oFile.Last('.');
66  if (indx1!=indx2) oFile.Remove(indx1+1,(indx2-indx1));
67  oChopFile=oFile;
68  oFile.Insert(indx1+1,"moretags.");
69  oFile.Prepend(OutputDir);
70  oChopFile.Insert(indx1+1,"chopper.");
71  oChopFile.ReplaceAll("root","txt");
72  oChopFile.Prepend(OutputDir);
73 
74  ofstream chop_output(oChopFile);
75 
76  TFile *tags_output = new TFile( oFile, "recreate" ) ;
77  tags_output->cd();
78 
79  //TH1F *hPhi = new TH1F("hPhi","Phi of proton",200,-TMath::Pi(),TMath::Pi());
80  //TH2F *hPhiFirstZ = new TH2F("hPhiFirstZ","Phi vs. FirstZ",200,-150,150,200,-TMath::Pi(),TMath::Pi());
81 
82  //Prepare the output tree
83  Int_t mRunId, mEvtId;
84  Int_t mnRefMult, mnRefMult2, mnRefMult3, mnRefMult4, mngRefMult, mnTofMatch;
85  Double_t mVX, mVY, mVZ;
86  Double_t mVXsigma, mVYsigma, mVZsigma;
87  Float_t mVpdVz;
88  Float_t mPVRank;
89  Double_t mEvtTime, mProdTime, mmagField;
90  TTree *mMoreTagsTree = new TTree("MoreTags","MoreTags");
91  mMoreTagsTree->Branch("RunId",&mRunId,"RunId/I");
92  mMoreTagsTree->Branch("EvtId",&mEvtId,"EvtId/I");
93  mMoreTagsTree->Branch("EvtTime",&mEvtTime,"EvtTime/D");
94  mMoreTagsTree->Branch("ProdTime",&mProdTime,"ProdTime/D");
95  mMoreTagsTree->Branch("magField",&mmagField,"magField/D");
96  mMoreTagsTree->Branch("nRefMult",&mnRefMult,"nRefMult/I");
97  mMoreTagsTree->Branch("nRefMult2",&mnRefMult2,"nRefMult2/I");
98  mMoreTagsTree->Branch("nRefMult3",&mnRefMult3,"nRefMult3/I");
99  mMoreTagsTree->Branch("nRefMult4",&mnRefMult4,"nRefMult4/I");
100  mMoreTagsTree->Branch("ngRefMult",&mngRefMult,"ngRefMult/I");
101  mMoreTagsTree->Branch("nTofMatch",&mnTofMatch,"nTofMatch/I");
102  mMoreTagsTree->Branch("VX",&mVX,"VX/D");
103  mMoreTagsTree->Branch("VY",&mVY,"VY/D");
104  mMoreTagsTree->Branch("VZ",&mVZ,"VZ/D");
105  mMoreTagsTree->Branch("VXsigma",&mVXsigma,"VXsigma/D");
106  mMoreTagsTree->Branch("VYsigma",&mVYsigma,"VYsigma/D");
107  mMoreTagsTree->Branch("VZsigma",&mVZsigma,"VZsigma/D");
108  mMoreTagsTree->Branch("VpdVz",&mVpdVz,"VpdVz/F");
109  mMoreTagsTree->Branch("PVRank",&mPVRank,"PVRank/F");
110  mMoreTagsTree->SetAutoSave(10000000);
111 
112 
113  // ---------------- end of histogram and tree booking --------------------------------
114 
115  // Loop over the links in the chain
116  Int_t iInit = chain -> Init() ;
117  if (iInit) chain->FatalErr(iInit,"on init");
118 
119  // chain -> EventLoop(1,nEvents) ; //will output lots of useless debugging info.
120  Int_t istat = 0, i = 1;
121  while (i <= nEvents && istat != 2) {
122  if(i%10==0)cout << endl << "== Event " << i << " start ==" << endl;
123  chain->Clear();
124  istat = chain->Make(i);
125 
126  if (istat == 2)
127  cout << "Last event processed. Status = " << istat << endl;
128  if (istat == 3)
129  cout << "Error event processed. Status = " << istat << endl;
130  i++;
131 
132  if(istat != kStOK)continue; //skip those suspectible events
133 
134  // ---------------- modify here according to your QA purpose --------------------------
135  //let's do the QA here...
136  //start with event cutting...
137  //cout<<"In event #. "<<i-1<<" Maker status "<<istat<<endl;
138 
139  StMuDst* mMuDst = muDstMaker->muDst();
140  if(!mMuDst) {
141  LOG_WARN << " No MuDst " << endm; continue;
142  }
143 
144  StMuEvent* mMuEvent = mMuDst->event();
145  if(!mMuEvent) {
146  LOG_WARN << " No MuEvent " << endm; continue;
147  }
148 
149  //vzVpd
150  StBTofHeader const* mBTofHeader = mMuDst->btofHeader();
151  Float_t vzVpd=-999;
152  if (mBTofHeader) vzVpd = mBTofHeader->vpdVz();
153 
154  //-----------------------------------------------------------------------------
155  //vertex selection in StPicoDstMaker
156  enum PicoVtxMode {NotSet=0, Default=1, Vpd=2, VpdOrDefault=3};
157  PicoVtxMode mVtxMode;
158 
159  //MUST assign this line!!!
160  mVtxMode = VpdOrDefault;
161  const double mTpcVpdVzDiffCut = 3;
162 
163  int const originalVertexId = mMuDst->currentVertexIndex();
164 
165  StMuPrimaryVertex* selectedVertex = nullptr;
166 
167  if (mVtxMode == Default) {
168  // choose the default vertex, i.e. the first vertex
169  mMuDst->setVertexIndex(0);
170  selectedVertex = mMuDst->primaryVertex();
171  }
172  else if (mVtxMode == Vpd || mVtxMode == VpdOrDefault) {
173 
174  if(mVtxMode == VpdOrDefault) {
175  mMuDst->setVertexIndex(0);
176  selectedVertex = mMuDst->primaryVertex();
177  }
178 
179  //StBTofHeader const* mBTofHeader = mMuDst->btofHeader();
180 
181  if (mBTofHeader && fabs(mBTofHeader->vpdVz()) < 200) {
182  float vzVPD = mBTofHeader->vpdVz();
183 
184  for (unsigned int iVtx = 0; iVtx < mMuDst->numberOfPrimaryVertices(); ++iVtx) {
185  StMuPrimaryVertex* vtx = mMuDst->primaryVertex(iVtx);
186  if (!vtx) continue;
187 
188  if (fabs(vzVPD - vtx->position().z()) < mTpcVpdVzDiffCut) {
189  mMuDst->setVertexIndex(iVtx);
190  selectedVertex = mMuDst->primaryVertex();
191  break;
192  } //if (fabs(vzVPD - vtx->position().z()) < mTpcVpdVzDiffCut)
193  } //for (unsigned int iVtx = 0; iVtx < mMuDst->numberOfPrimaryVertices(); ++iVtx)
194  } //if (mBTofHeader && fabs(mBTofHeader->vpdVz()) < 200)
195  } //else if (mVtxMode == Vpd || mVtxMode == VpdOrDefault)
196  else { // default case
197  LOG_ERROR << "Pico Vtx Mode not set!" << endm;
198  }
199 
200  // fall back to default vertex if no vertex is selected in the algorithm above.
201  // should skip this event in the event cuts below.
202  if ( ! selectedVertex ){
203  LOG_INFO << "Vertex is not valid" << endm;
204  //cout<<originalVertexId<<endl;
205  mMuDst->setVertexIndex(originalVertexId);
206  }
207  //end of vertex selection
208  //------------------------------------------------------------------------------
209 
210  mRunId = mMuEvent->runNumber();
211  mEvtId = mMuEvent->eventNumber();
212 
213  //add eventtime and prodtime to moretags.root, for HFT embedding
214  //cout<<mRunId<<" "<<mEvtId<<" "<<mMuEvent->eventInfo().time()<<" "<<mMuEvent->runInfo().productionTime()<<endl;
215  TDatime EventTime, ProdTime;
216  TUnixTime unixTime(mMuEvent->eventInfo().time());
217  Int_t dat=0,tim=0;
218  unixTime.GetGTime(dat,tim);
219  EventTime.Set(dat,tim);
220  unixTime.SetUTime(mMuEvent->runInfo().productionTime());
221  unixTime.GetGTime(dat,tim);
222  ProdTime.Set(dat,tim);
223  mEvtTime = EventTime.GetDate() + EventTime.GetTime()/1000000.;
224  mProdTime= ProdTime.GetDate() + ProdTime.GetTime()/1000000. ;
225  //cout<<EventTime.GetDate() + EventTime.GetTime()/1000000.<<" "<<ProdTime.GetDate() + ProdTime.GetTime()/1000000. <<endl;
226 
227  mmagField = mMuEvent->runInfo().magneticField();
228 
229  mnRefMult = mMuEvent->refMult();
230 
231  Int_t nTofMatPrTrack = 0;
232  Int_t nprTrack2 = 0;
233  Int_t nprTrack3 = 0;
234  Int_t nprTrack4 = 0;
235  TObjArray* prtracks = muDstMaker->muDst()->primaryTracks() ; // Create a TObject array containing the global tracks
236  TObjArrayIter GetPrTracks(prtracks) ; // Create an iterator to step through the tracks
237  StMuTrack* prtrack ; // Pointer to a track
238  while ( ( prtrack = (StMuTrack*)GetPrTracks.Next() ) ) // Main loop for Iterating over tracks
239  {
240  if(prtrack->btofPidTraits().matchFlag()) nTofMatPrTrack ++;
241 
242  if (prtrack->flag() < 0 || fabs(prtrack->momentum().mag()) < 1.e-10
243  || prtrack->dca().mag() > 3 || fabs(prtrack->momentum().pseudoRapidity()) > 1) continue;
244  double const eta = prtrack->momentum().pseudoRapidity() ;
245  double const beta = prtrack->btofPidTraits().beta();
246  double const mass2 = beta <= 1.e-5 ? -999. : prtrack->momentum().mag2() * (std::pow(1. / beta, 2) - 1);
247  if (prtrack->nHitsFit(kTpcId) >= 10) {
248  // refMult2 definition in PicoEvent: pt> 0.1 && abs(dca) < 3 && nHitsTpc >= 10 && abs(eta) > 0.5 && abs(eta) < 1
249  if (fabs(eta) > 0.5) nprTrack2 += 1;
250  // refMult3 definition in PicoEvent: pt> 0.1 && abs(dca) < 3 && nHitsTpc >= 10 && abs(eta) < 1 && Exclude protons
251  if (prtrack->nSigmaProton() < -3. && mass2 < 0.4) nprTrack3 += 1;
252  }
253  if (prtrack->nHitsFit(kTpcId) >= 15) {
254  // refMult4 definition in PicoEvent: pt> 0.1 && abs(dca) < 3 && nHitsTpc >= 15 && abs(eta) < 1 && Exclude kaons
255  if ((mass2 <= -990. && fabs(prtrack->nSigmaKaon()) > 3) || // tof is not available
256  (mass2 > -990. && (mass2 > 0.6 || mass2 < 0.1))) // tof is available
257  nprTrack4 += 1;
258  }
259  }
260  mnTofMatch = nTofMatPrTrack;
261  mnRefMult2 = nprTrack2;
262  mnRefMult3 = nprTrack3;
263  mnRefMult4 = nprTrack4;
264 
265  mVX = mMuEvent->primaryVertexPosition().x();
266  mVY = mMuEvent->primaryVertexPosition().y();
267  mVZ = mMuEvent->primaryVertexPosition().z();
268  mVXsigma = mMuEvent->primaryVertexErrors().x();
269  mVYsigma = mMuEvent->primaryVertexErrors().y();
270  mVZsigma = mMuEvent->primaryVertexErrors().z();
271 
272  mVpdVz = vzVpd;
273  if(mMuDst->primaryVertex())mPVRank = mMuDst->primaryVertex()->ranking();
274  else mPVRank = -1e9;
275 
276  Int_t nGlTrack = 0;
277  TObjArray* gltracks = muDstMaker->muDst()->globalTracks() ; // Create a TObject array containing the global tracks
278  TObjArrayIter GetGlTracks(gltracks) ; // Create an iterator to step through the tracks
279  StMuTrack* gltrack ; // Pointer to a track
280  while ( ( gltrack = (StMuTrack*)GetGlTracks.Next() ) ) // Main loop for Iterating over tracks
281  {
282  if(fabs(gltrack->eta())>=0.5)continue;
283  if(gltrack->nHitsFit()<10)continue;
284  if(gltrack->dca().mag()>=3.0)continue;
285  nGlTrack++ ;
286  }
287  mngRefMult = nGlTrack;
288 
289  //Comment out this line for HFT embedding!
290  mMoreTagsTree->Fill();
291 
292  //Event info (for debug)
293  //cout<<"Run#: "<<mMuEvent->runNumber()<<endl;
294  //cout<<"Evt#: "<<mMuEvent->eventNumber()<<endl;
295  //cout<<muDstMaker->muDst()->currentVertexIndex()<<endl;
296  //cout<<"refmult: "<<mMuEvent->refMult()<<endl;
297 
298  //Event cuts (NO EVENT CUTS TILL HERE!)
299  //vertex is not selected (only for PicoVtxMode::Vpd)
300  if ( ! selectedVertex ) continue;
301  //trigger
302  if ( ! mMuEvent->triggerIdCollection().nominal().isTrigger(520001) && ! mMuEvent->triggerIdCollection().nominal().isTrigger(520011) && ! mMuEvent->triggerIdCollection().nominal().isTrigger(520021) && ! mMuEvent->triggerIdCollection().nominal().isTrigger(520031) && ! mMuEvent->triggerIdCollection().nominal().isTrigger(520041) && ! mMuEvent->triggerIdCollection().nominal().isTrigger(520051) ) continue ;
303  //Vz
304  if ( fabs(mMuEvent->primaryVertexPosition().z()) > 6.0 ) continue ;
305  if ( fabs(mMuEvent->primaryVertexPosition().z() - vzVpd) > 3.0 ) continue ;
306  //Vr
307  //if ( mMuEvent->primaryVertexPosition().perp() > 2.0 ) continue ;
308  //pileup cut
309  //if ( mnTofMatch <= 0.46*mnRefMult - 10 ) continue ;
310  //VF failed (for some old dataset)
311  //if ( fabs(mMuEvent->primaryVertexPosition().x()) < 1e-5 && fabs(mMuEvent->primaryVertexPosition().y()) < 1e-5 && fabs(mMuEvent->primaryVertexPosition().z()) < 1e-5 ) continue;
312 
313  chop_output<<mRunId<<'\t'<<mEvtId<<endl;
314  //for HFT ONLY! PrepEmbd will not be used, event cuts are done here!
315  //mMoreTagsTree->Fill();
316 
317  /*
318  //fill Event QA histograms
319  TObjArray* tracks = muDstMaker->muDst()->primaryTracks() ;
320  TObjArrayIter GetTracks(tracks) ;
321  StMuTrack* gtrack ;
322  while ( ( gtrack = (StMuTrack*)GetTracks.Next() ) )
323  {
324  //const StMuTrack * gtrack = track->globalTrack();
325  if(gtrack->nHits()<=15)continue;
326  if(gtrack->flag()<=0)continue;
327  if(abs(gtrack->charge())!=1) continue;
328  if(gtrack->pt()>0.5) continue;
329  if(fabs(gtrack->nSigmaProton())>2)continue;
330  hPhi->Fill(gtrack->phi());
331  hPhiFirstZ->Fill(gtrack->firstPoint().z(),gtrack->phi());
332  }
333  //end of the filling
334  */
335  }
336 
337  if (nEvents > 1) chain -> Finish() ;
338 
339  if(tags_output!=NULL) tags_output -> Write() ;
340  if(tags_output!=NULL) tags_output -> Close() ;
341  //flush(tags_output);
342  delete tags_output;
343 
344  chop_output.close();
345  // Cleanup
346  delete chain ;
347 }
static StMuPrimaryVertex * primaryVertex()
return pointer to current primary vertex
Definition: StMuDst.h:322
static TObjArray * globalTracks()
returns pointer to the global tracks list
Definition: StMuDst.h:303
StThreeVectorF primaryVertexPosition(int vtx_id=-1) const
The StMuDst is supposed to be structured in &#39;physical events&#39;. Therefore there is only 1 primary vert...
Definition: StMuEvent.cxx:221
StMuDst * muDst()
Definition: StMuDstMaker.h:425
virtual void Clear(Option_t *option="")
User defined functions.
Definition: StChain.cxx:77
virtual const char * GetFile() const
Returns name of current input or output file, depending on mode (GetFileName does the same...
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Definition: TDataSet.cxx:893
unsigned char matchFlag() const
Matching information.
virtual Int_t Finish()
Definition: StChain.cxx:85
UShort_t nHitsFit() const
Return total number of hits used in fit.
Definition: StMuTrack.h:239
short flag() const
Returns flag, (see StEvent manual for type information)
Definition: StMuTrack.h:230
Double_t eta() const
Returns pseudo rapidity at point of dca to primary vertex.
Definition: StMuTrack.h:257
static TObjArray * primaryTracks()
returns pointer to a list of tracks belonging to the selected primary vertex
Definition: StMuDst.h:301
unsigned short refMult(int vtx_id=-1)
Reference multiplicity of charged particles as defined in StEventUtilities/StuRefMult.hh for vertex vtx_id (-1 is default index from StMuDst)
Definition: StMuEvent.cxx:195
virtual Int_t Make()
Definition: StChain.cxx:110
static StMuEvent * event()
returns pointer to current StMuEvent (class holding the event wise information, e.g. event number, run number)
Definition: StMuDst.h:320
const StThreeVectorF & momentum() const
Returns 3-momentum at dca to primary vertex.
Definition: StMuTrack.h:260
Definition: Stypes.h:40
StThreeVectorF dca(Int_t vtx_id=-1) const
Returns 3D distance of closest approach to primary vertex.
Definition: StMuTrack.cxx:359
static StBTofHeader * btofHeader()
returns pointer to the btofHeader - dongx
Definition: StMuDst.h:423
Double_t nSigmaProton() const
Returns Craig&#39;s distance to the calculated dE/dx band for protons in units of sigma.
Definition: StMuTrack.h:247
static Int_t currentVertexIndex()
Get the index number of the current primary vertex.
Definition: StMuDst.h:260
Double_t nSigmaKaon() const
Returns Craig&#39;s distance to the calculated dE/dx band for kaons in units of sigma.
Definition: StMuTrack.h:246
static void setVertexIndex(Int_t vtx_id)
Set the index number of the current primary vertex (used by both primaryTracks() functions and for St...
Definition: StMuDst.cxx:273