StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StMuDstFilterMaker.cxx
1 /***************************************************************************
2  *
3  * $Id: StMuDstFilterMaker.cxx,v 1.18 2016/05/04 19:24:07 smirnovd Exp $
4  * Author: Frank Laue, BNL, laue@bnl.gov
5  ***************************************************************************/
6 #include "StMuDstFilterMaker.h"
7 
8 #include "StMuDSTMaker/COMMON/StMuTypes.hh"
9 
10 
11 #include "StEvent/StEventTypes.h"
12 
13 #include "THack.h"
14 #include "TFile.h"
15 #include "TTree.h"
16 #include "TBranch.h"
17 #include "TChain.h"
18 
19 #include <vector>
20 #include <algorithm>
21 
22 #define __COMPRESSION__ 9
23 #define __BUFFER__ 65536
24 #define __SPLIT__ 99
25 #define __AUTOSAVE__ 1000000
26 
27 
28 
29 StMuDstFilterMaker::StMuDstFilterMaker(const char* name) : StMaker(name), mMuDstMaker(0), mFile(0), mTTree(0), mFilterGlobals(1), mDoBemc(1), mDoEemc(1) {
30  DEBUGMESSAGE2("");
31  // Create the TClonesArrays
32  createArrays();
33 }
34 
39 void StMuDstFilterMaker::open(const Char_t *fname) {
40  mFile = new TFile(fname,"RECREATE","StMuDst");
41  if (mFile->IsZombie() ) throw StMuExceptionNullPointer("no file openend",__PRETTYF__);
42  mFile->SetCompressionLevel(__COMPRESSION__);
43 
44  // Create a ROOT Tree and one superbranch
45  DEBUGMESSAGE2("now create trees and branches");
46 
47  mTTree = new TTree("MuDst", "StMuDst",__SPLIT__);
48  if (!mTTree) throw StMuExceptionNullPointer("can not create tree",__PRETTYF__);
49 #if ROOT_VERSION_CODE < ROOT_VERSION(5,26,0)
50  Long64_t MAXLONG=100000000000LL; // 100 GB
51  LOG_INFO << "Tree size MAX will be " << (float) MAXLONG/1000/1000/1000 << " GB " << endm;
52 
53  mTTree->SetMaxTreeSize(MAXLONG);
54 #endif
55  // muDst stuff
56  DEBUGMESSAGE2("arrays");
57  for ( int i=0; i<__NARRAYS__; i++) {
58  DEBUGVALUE2(i);
59  mTTree->Branch(StMuArrays::arrayNames[i],&mArrays[i], __BUFFER__, __SPLIT__);
60  }
61 #ifndef __NO_STRANGE_MUDST__
62  // strange stuff
63  DEBUGMESSAGE2("strange arrays");
64  for ( int i=0; i<__NSTRANGEARRAYS__; i++) {
65  DEBUGVALUE2(i);
66  mTTree->Branch(StMuArrays::strangeArrayNames[i],&mStrangeArrays[i], __BUFFER__, __SPLIT__);
67  }
68 #endif
69  // emc stuff
70  DEBUGMESSAGE2("emc arrays");
71  for ( int i=0; i<__NEMCARRAYS__; i++) {
72  DEBUGVALUE2(i);
73  mTTree->Branch(StMuArrays::emcArrayNames[i],&mEmcArrays[i], __BUFFER__, __SPLIT__);
74  }
75 }
76 
81  if (mFile) {
82  mFile->Write();
83  mFile->Close();
84  }
85  mFile = 0;
86  mTTree = 0;
87 }
88 
89 
90 StMuDstFilterMaker::~StMuDstFilterMaker() {
91  /* no=op */
92 }
93 
94 
95 
97  DEBUGMESSAGE1("");
98  if ( !mMuDstMaker ) return 0;
99 
100  if (mFile==0 || mCurFileName != mMuDstMaker->chain()->GetFile()->GetName()) {
101  string outName;
102  if (mOutDirName.size())
103  outName=mOutDirName+'/';
104  if (mOutFileName.size()) {
105  outName+=mOutFileName;
106  }
107  else {
108  close();
109  const Char_t *inName = mMuDstMaker->chain()->GetFile()->GetName();
110  const Char_t *baseName = strrchr(inName,'/');
111  if (!baseName)
112  baseName=inName;
113  else
114  baseName++;
115  outName+=baseName;
116  }
117  if (mFile==0) {
118  cout << "Opening output file " << outName << endl;
119  open(outName.c_str());
120  }
121  mCurFileName = mMuDstMaker->chain()->GetFile()->GetName();
122  }
123  StMuDst* muDst = mMuDstMaker->muDst();
124  if ( !muDst ) return 0;
125 
126  /* In this example we want to write only primary tracks with pT>2,
127  * the corresponding globals tracks and the event-wise information
128  * and the event wise information. We will discarde the event completely if
129  * the vertex is not |z|<50cm and if it has no tracks above 2GeV/c
130  */
131  // this is the function that decides whether the whole event shall be discarded or not
132  /*
133  cout << "event at " << muDst->event();
134  if (muDst->event() != 0)
135  cout << ", vtx z " << muDst->event()->primaryVertexPosition().z() << endl;
136  else
137  cout << endl;
138  */
139  clear();
140  if ( filter(muDst)==false ) return 0;
141 
142  /*
143  * Now apply filters to the individual TClonesArrays.
144  */
145 
146  //the event wise information first
147  if ( filter( muDst->event() ) == 0 ) return 0;
148 
149  DEBUGMESSAGE("Event accepted");
150  addType( mArrays[muEvent], *(muDst->event()) );
151 
152  // Add first primary vertex by default
153  if (muDst->primaryVertex()) {
154  addType( mArrays[muPrimaryVertex], *(muDst->primaryVertex()) );
155  }
156 
157  // The tracks are the most difficult part, because the primary tracks point
158  // to their global counterparts.
159  // For now, we are just keeping the global counterparts of all primary
160  // tracks, so thta's relatively easy to keep track off.
161 
162  // Note that for some productions we cannot trust the uniqueness of
163  // the track keys (StMuTrack::id()) anymore.
164 
165  TArrayI globals_stored(muDst->globalTracks()->GetEntries());
166  int numberOfTracks = 0;
167  numberOfTracks = muDst->primaryTracks()->GetEntries();
168  for ( int i=0; i<numberOfTracks; i++) {
169  StMuTrack* track = muDst->primaryTracks(i); // Note: this only works for a single primary vertex
170  if ( filter( track )==true ) {
171  Int_t global_idx=track->index2Global();
172  if (global_idx >= 0 && globals_stored[global_idx]==0) {
173 
174  Int_t nw_global_idx = addType( mArrays[muGlobal], *muDst->globalTracks(global_idx) );
175  track->setIndex2Global(nw_global_idx);
176  globals_stored[global_idx] = 1;
177  }
178  addType( mArrays[muPrimary], *track );
179  }
180  }
181 
182  numberOfTracks = muDst->globalTracks()->GetEntries();
183  for ( int i=0; i<numberOfTracks; i++) {
184  StMuTrack* track = muDst->globalTracks(i);
185  if (mFilterGlobals && globals_stored[i] == 0) {
186  if (filter(track)) {
187  addType( mArrays[muGlobal], *track);
188  }
189  }
190  }
191 
192  // the emc collection
193  StMuEmcCollection* emc = muDst->muEmcCollection();
194  if ( filter(emc) ) {
195  // This only works if the input MuDst is in the new format
196  // (data spread over multiple branches, not stored in StMuEmcCollection)
197  StMuEmcTowerData *typeOfTowerData=0;
198  StMuEmcHit *typeOfEmcHit=0;
199  if (mDoBemc || mDoEemc) {
200  addType( muDst->emcArray(muEmcTow), mEmcArrays[muEmcTow], typeOfTowerData );
201  if (mEmcArrays[muEmcTow]->GetEntries()) {
202  if (!mDoBemc)
203  ((StMuEmcTowerData*) mEmcArrays[muEmcTow]->UncheckedAt(0))->clearBemc();
204  if (!mDoEemc)
205  ((StMuEmcTowerData*) mEmcArrays[muEmcTow]->UncheckedAt(0))->clearEemc();
206  }
207  }
208  if (mDoBemc) {
209  addType( muDst->emcArray(muEmcPrs), mEmcArrays[muEmcPrs], typeOfEmcHit );
210  addType( muDst->emcArray(muEmcSmde), mEmcArrays[muEmcSmde], typeOfEmcHit );
211  addType( muDst->emcArray(muEmcSmdp), mEmcArrays[muEmcSmdp], typeOfEmcHit );
212  }
213  if (mDoEemc) {
214  addType( muDst->emcArray(muEEmcPrs), mEmcArrays[muEEmcPrs], typeOfEmcHit );
215  addType( muDst->emcArray(muEEmcSmdu), mEmcArrays[muEEmcSmdu], typeOfEmcHit );
216  addType( muDst->emcArray(muEEmcSmdv), mEmcArrays[muEEmcSmdv], typeOfEmcHit );
217  }
218  }
219 
220  // write the event only if it has at least one primary track
221  if ( mArrays[muPrimary]->GetEntries()>0) {
222  mTTree->Fill(); THack::IsTreeWritable(mTTree);
223  }
224  return 0;
225 }
226 
227 template <class T>
228 int StMuDstFilterMaker::addType(TClonesArray* tcaTo , T t) {
229  int counter =-1;
230  if (tcaTo) {
231  counter = tcaTo->GetEntries();
232  new((*tcaTo)[counter]) T( t );
233  }
234  return counter;
235 }
236 
237 template <class T>
238 int StMuDstFilterMaker::addType(TClonesArray* tcaFrom, TClonesArray* tcaTo ,T *t) {
239  if (tcaFrom && tcaTo) {
240  int n = tcaFrom->GetEntries();
241  int counter = tcaTo->GetEntries();
242  for (int i=0; i<n;i++) {
243  new((*tcaTo)[counter++]) T( *(T*)(void*)tcaFrom->UncheckedAt(i) );
244  }
245  }
246  return 0;
247 }
248 
250  close();
251  return 0;
252 }
253 
258  DEBUGMESSAGE2("");
259 
260  clearArrays();
261 }
262 
269  int dummy;
270  for ( int i=0; i<__NARRAYS__; i++) {
271  mArrays[i] = 0;
272  DEBUGVALUE2(mArrays[i]);
273  mArrays[i]= mMuDstMaker->clonesArray(mArrays[i],StMuArrays::arrayTypes[i],StMuArrays::arraySizes[i],dummy);
274  DEBUGVALUE2(mArrays[i]);
275  }
276 #ifndef __NO_STRANGE_MUDST__
277  for ( int i=0; i<__NSTRANGEARRAYS__; i++) {
279  mStrangeArrays[i] = 0;
280  mStrangeArrays[i]= mMuDstMaker->clonesArray(mStrangeArrays[i],StMuArrays::strangeArrayTypes[i],StMuArrays::strangeArraySizes[i],dummy);
281  }
282 #endif
283  for ( int i=0; i<__NEMCARRAYS__; i++) {
285  mEmcArrays[i] = 0;
286  mEmcArrays[i]= mMuDstMaker->clonesArray(mEmcArrays[i],StMuArrays::emcArrayTypes[i],StMuArrays::emcArraySizes[i],dummy);
287  }
288 }
289 
291 {
293  for ( int i=0; i<__NARRAYS__; i++) {
294  mArrays[i]->Clear();
295  }
296 #ifndef __NO_STRANGE_MUDST__
297  for ( int i=0; i<__NSTRANGEARRAYS__; i++) {
299  mStrangeArrays[i]->Clear();
300  }
301 #endif
302  for ( int i=0; i<__NEMCARRAYS__; i++) {
304  mEmcArrays[i]->Clear();
305  }
306 }
307 
308 
309 /*
310  * Here I define my cuts by specializing the
311  * template<class T> bool filter(T*)
312  * function
313  */
316  return ( track->p().mag()>1. ) && ( fabs(track->eta())<1.0 );
317 }
318 
319 
320 ClassImp(StMuDstFilterMaker)
321 
322 /***************************************************************************
323  *
324  * $Log: StMuDstFilterMaker.cxx,v $
325  * Revision 1.18 2016/05/04 19:24:07 smirnovd
326  * Addressed compiler warning by removing set but never used variables
327  *
328  * Revision 1.17 2011/08/18 18:41:36 fisyak
329  * set max. tree size = 100 GB
330  *
331  * Revision 1.16 2011/04/19 22:50:08 fisyak
332  * Use default size of TTree (100 GB) for ROOT >= 5.26.0
333  *
334  * Revision 1.15 2011/04/08 01:25:50 fisyak
335  * Add branches for MC track and vertex information, add IdTruth to tracks and vertices, reserve a possiblity to remove Strange MuDst
336  *
337  * Revision 1.14 2009/05/22 23:48:18 fine
338  * Test I/O errors after filling the TTree
339  *
340  * Revision 1.13 2009/05/22 22:25:31 fine
341  * Add the Zombue test for TFile ctors
342  *
343  * Revision 1.12 2009/03/10 23:43:53 jeromel
344  * Set tree size to max size
345  *
346  * Revision 1.11 2005/12/19 01:55:55 mvl
347  * Introduced check before copying primary vertex for backwards compatibility
348  *
349  * Revision 1.10 2005/12/13 03:12:13 mvl
350  * Changes to StMuDst2StEventMaker (code in StMuDst) and StMuDstFilterMaker
351  * to no longer rely on track keys for matching global and primary tracks.
352  * This was needed because track keys are not guaranteed to be unique anymore.
353  *
354  * Revision 1.9 2005/05/23 19:46:20 mvl
355  * Two incremental changes by Alex Suiade: A message is printed when a new output file is opened
356  * and if StMuEvent does not pass the cut, the whole event is discarded
357  * (previously, tracks could be kept, which does not make sense)
358  *
359  * Revision 1.8 2005/05/18 22:47:29 mvl
360  * Fixed StMuDstFilterMaker to work again with changes in MuDstMaker
361  * (the change in v1.6 was faulty. Thanks Alex for finding this)
362  * Added some new features suggested by Alex Suiade:
363  * - Emc data now supported (for SL04k and later MuDst).
364  * Flags added to switch Eemc and Bemc copying seperately (setDoBemc and setDoEemc)
365  * - Global tracks are checked seperately. They were only copied
366  * if the corresponding primary fullfills the filter() criteria.
367  * Now they are also copied if only the global track fullfills the criteria
368  * Can be switched with setFilterGlobals()
369  *
370  * Revision 1.7 2004/10/21 02:57:25 mvl
371  * Changed call to getter for StMuEmcCollection
372  *
373  * Revision 1.6 2004/10/19 01:47:57 mvl
374  * Changed calls to clear TClonesArray in StMuDstMaker (not tested ;-()
375  *
376  * Revision 1.5 2004/02/17 04:56:36 jeromel
377  * Extended help, added crs support, restored __GNUC__ for PRETTY_FUNCTION(checked once
378  * more and yes, it is ONLY defined in GCC and so is __FUCTION__), use of a consistent
379  * internal __PRETTYF__, return NULL if no case selected (+message) and protected against
380  * NULL mChain.
381  *
382  * Revision 1.4 2003/11/24 23:36:36 laue
383  * commented the StMuEmcCollection out
384  *
385  * Revision 1.3 2003/09/07 03:49:03 perev
386  * gcc 3.2 + WarnOff
387  *
388  * Revision 1.2 2003/08/04 14:38:10 laue
389  * Alex Suaide's updated for the EMC. Now EEMC is included.
390  *
391  * Revision 1.1 2003/04/15 18:48:35 laue
392  * Minor changes to be able to filter MuDst.root files and an example
393  * how to do this. The StMuDstFilterMaker is just an example, it has to be
394  * customized (spoilers, chrome weels, etc.) by the user.
395  *
396  *
397  **************************************************************************/
static StMuPrimaryVertex * primaryVertex()
return pointer to current primary vertex
Definition: StMuDst.h:322
Int_t mFilterGlobals
If set, keep also globals that fulfill cuts, while primary does not.
static TObjArray * globalTracks()
returns pointer to the global tracks list
Definition: StMuDst.h:303
Int_t mDoBemc
Copy barrel data (if it passes cuts)
StMuDst * muDst()
Definition: StMuDstMaker.h:425
Int_t index2Global() const
Returns index of associated global track. If not in order can be set with StMuDst::fixTrackIndeces() ...
Definition: StMuTrack.h:231
static const char * arrayTypes[__NALLARRAYS__]
&lt; names of the classes, the TClonesArrays are arrays of this type
Definition: StMuArrays.h:120
int Make()
Filters the muDst and writes the filtered version.
StMuDstFilterMaker(const char *name="muDstFilter")
Default constructor; get pointer to StMuDstMaker.
void setIndex2Global(Int_t i)
Set index of associated global track.
Definition: StMuTrack.h:211
TClonesArray * mArrays[__NARRAYS__]
the list of TClonesArrays to copy
int Finish()
Writes and closes the output file.
void open(const Char_t *)
const StThreeVectorF & p() const
Returns 3-momentum at dca to primary vertex.
Definition: StMuTrack.h:259
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
static StMuEmcCollection * muEmcCollection()
returns pointer to current StMuEmcCollection
Definition: StMuDst.h:389
static StMuEvent * event()
returns pointer to current StMuEvent (class holding the event wise information, e.g. event number, run number)
Definition: StMuDst.h:320
TChain * chain()
In read mode, returns pointer to the chain of .MuDst.root files that where selected.
Definition: StMuDstMaker.h:426
Int_t mDoEemc
Copy endcap data (if it passes cuts)
static int arraySizes[__NALLARRAYS__]
&lt; maximum sizes of the TClonesArrays
Definition: StMuArrays.h:142
bool filter(T *t)
specialize this function to apply filters to the individual branches
static TClonesArray * emcArray(int type)
returns pointer to the n-th TClonesArray from the emc arrays
Definition: StMuDst.h:269