ROOT logo
//*-- Created : 04/12/2009 by I.Koenig
//_HADES_CLASS_DESCRIPTION
///////////////////////////////////////////////////////////////////////////////
//
//  HStart2HitF
//
//  This class implements the hit finder for the Start2 detector.
//
///////////////////////////////////////////////////////////////////////////////

#include "hades.h"
#include "hcategory.h"
#include "hevent.h"
#include "hruntimedb.h"
#include "hspectrometer.h"
#include "hstart2cal.h"
#include "hstart2detector.h"
#include "hstart2hit.h"
#include "hstart2hitf.h"
#include "hstart2hitfpar.h"
#include "hstartdef.h"

#include <iomanip>
#include <iostream>
#include <algorithm>

using namespace std;

ClassImp(HStart2HitF)

HStart2HitF::HStart2HitF(void)
   : HReconstructor()
{
   // default constructor

   fCatCal    = NULL;
   fCatHit    = NULL;
   fPar       = NULL;
   fSkipEvent = kFALSE;
}

HStart2HitF::HStart2HitF(const Text_t *name, const Text_t *title, Bool_t skip)
   : HReconstructor(name, title)
{
   // constructor

   fCatCal    = NULL;
   fCatHit    = NULL;
   fPar       = NULL;
   fSkipEvent = skip;
}

Bool_t HStart2HitF::init(void)
{
   // gets the Start2Cal category and creates the Start2Hit category
   // creates an iterator which loops over all cells in Start2Cal
   // initialize parameter container

   HStart2Detector* det = static_cast<HStart2Detector*>(gHades->getSetup()->getDetector("Start"));
   if (NULL == det) {
       Error("init", "No Start Detector found.");
       return kFALSE;
   }
   fCatCal = gHades->getCurrentEvent()->getCategory(catStart2Cal);
   if (NULL == fCatCal) {
       Error("init", "HStart2Cal category not available!");
       return kFALSE;
   }
   fCatHit = det->buildCategory(catStart2Hit);
   if (NULL == fCatHit) {
       Error("init", "HStart2Hit category not available!");
       return kFALSE;
   }

   HRuntimeDb* rtdb = gHades->getRuntimeDb();
   fPar = static_cast<HStart2HitFPar*>(rtdb->getContainer("Start2HitFPar"));
   if (NULL == fPar) {
       Error("init", "Pointer to Start2HitFPar parameters is NULL");
       return kFALSE;
   }

   loc.set(1, 0);
   fActive = kTRUE;

   return kTRUE;
}

Int_t HStart2HitF::execute(void)
{
   // makes the hits and fills the HStartHit category

   HStart2Cal* pCal = NULL;
   HStart2Hit* pHit = NULL;

   Bool_t foundTime = kFALSE;
   Int_t  module    = -1;
   Int_t  strip     = -1;

   fSecondTime       = -10000;
   Int_t newstrip    = -1;
   Float_t firstTime = -10000;
   firstCluster .clear();
   secondCluster.clear();

   for (Int_t entry = 0; entry < fCatCal->getEntries(); ++entry) {
      if (NULL == (pCal = static_cast<HStart2Cal*>(fCatCal->getObject(entry)))) {
	  Error("execute", "Pointer to HStart2Cal object == NULL, returning");
	  return -1;
      }

      // reject veto
      if (pCal->getModule() > 1) continue;

      // check the times and apply a time cut to remove BKG
      // in strip 7 at least the reference time after calibration
      // should be in the data

      for (Int_t i = 0; i < pCal->getMultiplicity() && i < pCal->getMaxMultiplicity(); ++i) {
         if (pCal->getTime(i + 1) > (fPar->getMeanTime(pCal->getModule(), pCal->getStrip()) - fPar->getWidth(pCal->getModule(), pCal->getStrip())) &&
	    pCal->getTime(i + 1) < (fPar->getMeanTime(pCal->getModule(), pCal->getStrip()) + fPar->getWidth(pCal->getModule(), pCal->getStrip())))
	 {
	     pCal->getAddress(module, strip);
	     // write data to the ouput category
	     if (!foundTime) {
		 pHit = static_cast<HStart2Hit*>(fCatHit->getSlot(loc));
		 if (NULL != pHit) {
		     pHit = new(pHit) HStart2Hit;
		 } else {
		     Error("execute", "Can't get slot mod=%i, chan=%i", loc[0], loc[1]);
		     return -1;
		 }
	     }
	     // First hit or earlier than the previous hit
	     if (!foundTime || firstTime > pCal->getTime(i + 1)) {
		 pHit->setAddress(module, strip);
		 pHit->setTimeAndWidth(pCal->getTime(i + 1), pCal->getWidth(i + 1));
		 pHit->setFlag(kTRUE);
		 pHit->setMultiplicity(pCal->getMultiplicity());
		 firstTime = pHit->getTime();
		 newstrip = entry * 1000 + i+1;
	     }
	     foundTime = kTRUE;
	 }
      }
   }

   //----------------------------------------------------------------------
   // cluster info's
   vector<Int_t> vtemp;
   Float_t maxdiff   = 2; // ns with arround starttime used to cluster
   Float_t newStart2 = -100000;  // clostest scond time
   Float_t timediff  =  100000;  // local var
   Int_t   nextHit   = -1;

   if (foundTime) firstCluster.push_back(newstrip);

   for (Int_t entry = 0; entry < fCatCal->getEntries(); ++entry) {
       if(NULL == (pCal = static_cast<HStart2Cal*>(fCatCal->getObject(entry)))) {
	   Error("execute", "Pointer to HStart2Cal object == NULL, returning");
	   return -1;
       }

       // reject veto
       if (pCal->getModule() > 1) continue;

       // check the times and apply a time cut to remove BKG
       // in strip 7 at least the reference time after calibration
       // should be in the data

       for (Int_t i = 0; i < pCal->getMultiplicity() && i < pCal->getMaxMultiplicity(); ++i) {
	   if (pCal->getTime(i + 1) > (fPar->getMeanTime(pCal->getModule(), pCal->getStrip()) - fPar->getWidth(pCal->getModule(), pCal->getStrip())) &&
	       pCal->getTime(i + 1) < (fPar->getMeanTime(pCal->getModule(), pCal->getStrip()) + fPar->getWidth(pCal->getModule(), pCal->getStrip()))) {
	       Float_t time = pCal->getTime(i + 1);
	       newstrip     = entry * 1000 + i+1;
	       Float_t diff = TMath::Abs(firstTime-time) ;

	       if(diff < maxdiff) { // first cluster
		   if(find(firstCluster.begin(), firstCluster.end(), newstrip) == firstCluster.end()) {
		       firstCluster.push_back(newstrip);
		   }
	       } else {  // second cluster
		   vtemp.push_back(newstrip);
		   if(diff < timediff) {
		       timediff  = diff;
		       nextHit   = newstrip;
		       newStart2 = time;
		   }
	       } // end second clust
	   } // end window
       } // hitloop
   } // end strip loop

   if (vtemp.size() > 0) { // there was something else
       fSecondTime = newStart2;
       secondCluster.push_back(nextHit);
       for (UInt_t i = 0; i < vtemp.size(); i++) {
	   if (find(secondCluster.begin(), secondCluster.end(), vtemp[i]) == secondCluster.end()) {
	       secondCluster.push_back(vtemp[i]);
	   }
       }
   }

   //--------------------------------------------------------------------
   // fill cluster infos
   if (firstCluster.size() > 0) {
       pHit = static_cast<HStart2Hit*>(fCatHit->getObject(0));
       if (NULL == pHit) {
	   Error("execute()", "Pointer to HStart2Hit object == NULL");
       } else {
	   pHit->setIteration(1);
	   pHit->resetClusterStrip(2);
	   for(UInt_t i = 0; i < firstCluster.size(); i++) {
	       pHit->setClusterStrip(0, firstCluster[i]);
	   }
	   for (UInt_t i = 0; i < secondCluster.size(); i++) {
	       pHit->setClusterStrip(1, secondCluster[i]);
	   }
	   pHit->setSecondTime(fSecondTime);

	   if (secondCluster.size() > 0)  pHit->setMultiplicity(2);
	   else                           pHit->setMultiplicity(1);
       }
   }

   //----------------------------------------------------------------------
   if (kFALSE == foundTime && kTRUE == fSkipEvent) {
       return kSkipEvent;
   }

   return 0;
}
 hstart2hitf.cc:1
 hstart2hitf.cc:2
 hstart2hitf.cc:3
 hstart2hitf.cc:4
 hstart2hitf.cc:5
 hstart2hitf.cc:6
 hstart2hitf.cc:7
 hstart2hitf.cc:8
 hstart2hitf.cc:9
 hstart2hitf.cc:10
 hstart2hitf.cc:11
 hstart2hitf.cc:12
 hstart2hitf.cc:13
 hstart2hitf.cc:14
 hstart2hitf.cc:15
 hstart2hitf.cc:16
 hstart2hitf.cc:17
 hstart2hitf.cc:18
 hstart2hitf.cc:19
 hstart2hitf.cc:20
 hstart2hitf.cc:21
 hstart2hitf.cc:22
 hstart2hitf.cc:23
 hstart2hitf.cc:24
 hstart2hitf.cc:25
 hstart2hitf.cc:26
 hstart2hitf.cc:27
 hstart2hitf.cc:28
 hstart2hitf.cc:29
 hstart2hitf.cc:30
 hstart2hitf.cc:31
 hstart2hitf.cc:32
 hstart2hitf.cc:33
 hstart2hitf.cc:34
 hstart2hitf.cc:35
 hstart2hitf.cc:36
 hstart2hitf.cc:37
 hstart2hitf.cc:38
 hstart2hitf.cc:39
 hstart2hitf.cc:40
 hstart2hitf.cc:41
 hstart2hitf.cc:42
 hstart2hitf.cc:43
 hstart2hitf.cc:44
 hstart2hitf.cc:45
 hstart2hitf.cc:46
 hstart2hitf.cc:47
 hstart2hitf.cc:48
 hstart2hitf.cc:49
 hstart2hitf.cc:50
 hstart2hitf.cc:51
 hstart2hitf.cc:52
 hstart2hitf.cc:53
 hstart2hitf.cc:54
 hstart2hitf.cc:55
 hstart2hitf.cc:56
 hstart2hitf.cc:57
 hstart2hitf.cc:58
 hstart2hitf.cc:59
 hstart2hitf.cc:60
 hstart2hitf.cc:61
 hstart2hitf.cc:62
 hstart2hitf.cc:63
 hstart2hitf.cc:64
 hstart2hitf.cc:65
 hstart2hitf.cc:66
 hstart2hitf.cc:67
 hstart2hitf.cc:68
 hstart2hitf.cc:69
 hstart2hitf.cc:70
 hstart2hitf.cc:71
 hstart2hitf.cc:72
 hstart2hitf.cc:73
 hstart2hitf.cc:74
 hstart2hitf.cc:75
 hstart2hitf.cc:76
 hstart2hitf.cc:77
 hstart2hitf.cc:78
 hstart2hitf.cc:79
 hstart2hitf.cc:80
 hstart2hitf.cc:81
 hstart2hitf.cc:82
 hstart2hitf.cc:83
 hstart2hitf.cc:84
 hstart2hitf.cc:85
 hstart2hitf.cc:86
 hstart2hitf.cc:87
 hstart2hitf.cc:88
 hstart2hitf.cc:89
 hstart2hitf.cc:90
 hstart2hitf.cc:91
 hstart2hitf.cc:92
 hstart2hitf.cc:93
 hstart2hitf.cc:94
 hstart2hitf.cc:95
 hstart2hitf.cc:96
 hstart2hitf.cc:97
 hstart2hitf.cc:98
 hstart2hitf.cc:99
 hstart2hitf.cc:100
 hstart2hitf.cc:101
 hstart2hitf.cc:102
 hstart2hitf.cc:103
 hstart2hitf.cc:104
 hstart2hitf.cc:105
 hstart2hitf.cc:106
 hstart2hitf.cc:107
 hstart2hitf.cc:108
 hstart2hitf.cc:109
 hstart2hitf.cc:110
 hstart2hitf.cc:111
 hstart2hitf.cc:112
 hstart2hitf.cc:113
 hstart2hitf.cc:114
 hstart2hitf.cc:115
 hstart2hitf.cc:116
 hstart2hitf.cc:117
 hstart2hitf.cc:118
 hstart2hitf.cc:119
 hstart2hitf.cc:120
 hstart2hitf.cc:121
 hstart2hitf.cc:122
 hstart2hitf.cc:123
 hstart2hitf.cc:124
 hstart2hitf.cc:125
 hstart2hitf.cc:126
 hstart2hitf.cc:127
 hstart2hitf.cc:128
 hstart2hitf.cc:129
 hstart2hitf.cc:130
 hstart2hitf.cc:131
 hstart2hitf.cc:132
 hstart2hitf.cc:133
 hstart2hitf.cc:134
 hstart2hitf.cc:135
 hstart2hitf.cc:136
 hstart2hitf.cc:137
 hstart2hitf.cc:138
 hstart2hitf.cc:139
 hstart2hitf.cc:140
 hstart2hitf.cc:141
 hstart2hitf.cc:142
 hstart2hitf.cc:143
 hstart2hitf.cc:144
 hstart2hitf.cc:145
 hstart2hitf.cc:146
 hstart2hitf.cc:147
 hstart2hitf.cc:148
 hstart2hitf.cc:149
 hstart2hitf.cc:150
 hstart2hitf.cc:151
 hstart2hitf.cc:152
 hstart2hitf.cc:153
 hstart2hitf.cc:154
 hstart2hitf.cc:155
 hstart2hitf.cc:156
 hstart2hitf.cc:157
 hstart2hitf.cc:158
 hstart2hitf.cc:159
 hstart2hitf.cc:160
 hstart2hitf.cc:161
 hstart2hitf.cc:162
 hstart2hitf.cc:163
 hstart2hitf.cc:164
 hstart2hitf.cc:165
 hstart2hitf.cc:166
 hstart2hitf.cc:167
 hstart2hitf.cc:168
 hstart2hitf.cc:169
 hstart2hitf.cc:170
 hstart2hitf.cc:171
 hstart2hitf.cc:172
 hstart2hitf.cc:173
 hstart2hitf.cc:174
 hstart2hitf.cc:175
 hstart2hitf.cc:176
 hstart2hitf.cc:177
 hstart2hitf.cc:178
 hstart2hitf.cc:179
 hstart2hitf.cc:180
 hstart2hitf.cc:181
 hstart2hitf.cc:182
 hstart2hitf.cc:183
 hstart2hitf.cc:184
 hstart2hitf.cc:185
 hstart2hitf.cc:186
 hstart2hitf.cc:187
 hstart2hitf.cc:188
 hstart2hitf.cc:189
 hstart2hitf.cc:190
 hstart2hitf.cc:191
 hstart2hitf.cc:192
 hstart2hitf.cc:193
 hstart2hitf.cc:194
 hstart2hitf.cc:195
 hstart2hitf.cc:196
 hstart2hitf.cc:197
 hstart2hitf.cc:198
 hstart2hitf.cc:199
 hstart2hitf.cc:200
 hstart2hitf.cc:201
 hstart2hitf.cc:202
 hstart2hitf.cc:203
 hstart2hitf.cc:204
 hstart2hitf.cc:205
 hstart2hitf.cc:206
 hstart2hitf.cc:207
 hstart2hitf.cc:208
 hstart2hitf.cc:209
 hstart2hitf.cc:210
 hstart2hitf.cc:211
 hstart2hitf.cc:212
 hstart2hitf.cc:213
 hstart2hitf.cc:214
 hstart2hitf.cc:215
 hstart2hitf.cc:216
 hstart2hitf.cc:217
 hstart2hitf.cc:218
 hstart2hitf.cc:219
 hstart2hitf.cc:220
 hstart2hitf.cc:221
 hstart2hitf.cc:222
 hstart2hitf.cc:223
 hstart2hitf.cc:224
 hstart2hitf.cc:225
 hstart2hitf.cc:226
 hstart2hitf.cc:227
 hstart2hitf.cc:228
 hstart2hitf.cc:229
 hstart2hitf.cc:230
 hstart2hitf.cc:231