00001
00002
00003
00004
00006 #include "TSystem.h"
00007 #include "TRegexp.h"
00008
00009 #include "ToyMCModule.h"
00010 #include <cassert>
00011 #include <cmath>
00012 #include "MessageService/MsgService.h"
00013 #include "MinosObjectMap/MomNavigator.h"
00014 #include "JobControl/JobCInputModule.h"
00015 #include "JobControl/JobCModuleRegistry.h"
00016 #include "JobControl/JobCEnv.h"
00017 #include "RawData/RawRecord.h"
00018 #include "RawData/RawDaqSnarlHeader.h"
00019 #include "Record/RecRecord.h"
00020 #include "Record/RecPhysicsHeader.h"
00021 #include "Registry/Registry.h"
00022 #include "Validity/VldContext.h"
00023 #include "Util/UtilString.h"
00024 #include <TRandom.h>
00025 #include "UgliGeometry/UgliGeomHandle.h"
00026 #include "Digitization/DigiList.h"
00027 #include "Digitization/DigiScintHit.h"
00028 #include "Record/SimSnarlRecord.h"
00029 #include "Record/SimSnarlHeader.h"
00030 #include "RawData/RawRecord.h"
00031 #include "RawData/RawDaqSnarlHeader.h"
00032 #include "RawData/RawDigitDataBlock.h"
00033 #include "RawData/RawDigit.h"
00034 #include <TClonesArray.h>
00035 #include <TParticle.h>
00036 #include "Conventions/Munits.h"
00037 #include "Util/LoadMinosPDG.h"
00038
00039 #include "DataUtil/Truthifier.h"
00040
00041 #include <algorithm>
00042
00043 CVSID("$Id: ToyMCModule.cxx,v 1.14 2007/11/11 06:16:02 rhatcher Exp $");
00044 JOBMODULE(ToyMCModule,"ToyMCModule","Toy Muon MC");
00045
00046
00047
00048
00049 ToyMCModule::ToyMCModule() :
00050 fCurrentSnarl(0)
00051 {
00052 LoadMinosPDG();
00053 }
00054
00055
00056
00057 ToyMCModule::~ToyMCModule()
00058 {
00059 }
00060
00061
00062
00063 void ToyMCModule::BeginJob()
00064 {
00065 }
00066
00067
00068
00069
00070 const Registry& ToyMCModule::DefaultConfig() const
00071 {
00072
00073
00074
00075 static Registry r;
00076
00077
00078 std::string name = this->GetName();
00079 name += ".config.default";
00080 r.SetName(name.c_str());
00081
00082
00083 r.UnLockValues();
00084 r.Set("DetectorType", 2);
00085 r.Set("Rate", 1.0);
00086 r.Set("Date", 20030101);
00087 r.Set("Time", 000000);
00088 r.Set("StartPlane", 1);
00089 r.Set("StopPlane", 50);
00090 r.Set("StartX", 0.30*Munits::m);
00091 r.Set("StartY", 0.30*Munits::m);
00092 r.Set("DX", 0.*Munits::m);
00093 r.Set("DY", 0.*Munits::m);
00094
00095 r.Set("EnergyPerPlane",2.0*Munits::MeV);
00096 r.LockValues();
00097
00098 return r;
00099 }
00100
00101
00102
00103 void ToyMCModule::Config(const Registry& r)
00104 {
00105
00106
00107
00108 int tmpi;
00109 double tmpd;
00110
00111 if (r.Get("DetectorType",tmpi)) { fDetector = tmpi; }
00112 if (r.Get("Rate",tmpd)) { fRate = tmpd; }
00113 if (r.Get("Date",tmpi)) { fDate = tmpi; }
00114 if (r.Get("Time",tmpi)) { fTime = tmpi; }
00115
00116 r.Get("StartPlane",fStartPlane);
00117 r.Get("StopPlane",fStopPlane);
00118 r.Get("StartX",fStartX);
00119 r.Get("StartY",fStartY);
00120 r.Get("DX",fDX);
00121 r.Get("DY",fDY);
00122 r.Get("EnergyPerPlane",fEnergyPerPlane);
00123 }
00124
00125
00126
00127 JobCResult ToyMCModule::Get()
00128 {
00129 MomNavigator* mom = this->GetMom();
00130 assert(mom);
00131 mom -> Clear();
00132
00133 MSG("ToyMC",Msg::kInfo) << "Toy MC running." << endl;
00134
00135
00136 Int_t run = 1000;
00137 Int_t trigbits = 0;
00138 Short_t subrun = 0;
00139 Short_t runtype = 0;
00140 Int_t errcode = 0;
00141 Int_t timeframe = -1;
00142 Int_t spilltype = -1;
00143
00144
00145 int secs;
00146 double frac = frexp(fRate*fCurrentSnarl,&secs);
00147 int nsecs = int(frac*1e9);
00148 VldTimeStamp offset(secs,nsecs);
00149 VldTimeStamp fPulseTime = VldTimeStamp(fDate,fTime,0);
00150 fPulseTime.Add(offset);
00151
00152
00153 fContext = VldContext(Detector::CharToEnum((char)fDetector),
00154 SimFlag::kMC,
00155 fPulseTime);
00156
00157 VldTimeStamp now;
00158 std::string codename = SimSnarlHeader::TrimCodename("$Name: $");
00159 std::string hostname(gSystem->HostName());
00160
00161 SimSnarlHeader simheader(fContext,run,subrun,runtype,
00162 errcode,fCurrentSnarl,trigbits,timeframe,spilltype,
00163 now,codename,hostname);
00164
00165 SimSnarlRecord* simsnarl = new SimSnarlRecord(simheader);
00166
00167 simsnarl->GetTempTags().Set("stream","SimSnarl");
00168
00169
00170 mom -> AdoptFragment(simsnarl);
00171
00172
00173 gRandom->SetSeed(fCurrentSnarl);
00174
00175
00176 UgliGeomHandle ugli(fContext);
00177
00178 std::vector<DigiScintHit> hits;
00179
00180 for(int plane=fStartPlane;plane<=fStopPlane;plane++) {
00181 PlexPlaneId planeId(Detector::CharToEnum((char)fDetector),plane);
00182 if(!(planeId.IsValid())) break;
00183
00184 UgliScintPlnHandle uplane = ugli.GetScintPlnHandle(planeId);
00185 if(!(uplane.IsValid())) break;
00186 std::vector<UgliStripHandle> strips = uplane.GetStripHandleVector();
00187
00188
00189
00190 double min = 1e19;
00191 UgliStripHandle ustrip;
00192
00193 TVector3 x(fStartX + (double)(fCurrentSnarl)*fDX,
00194 fStartY + (double)(fCurrentSnarl)*fDY,
00195 uplane.GetCenter().z());
00196
00197
00198 for(UInt_t istrip = 0;istrip<strips.size(); istrip++) {
00199 if(strips[istrip].IsValid()) {
00200 double dist = fabs(strips[istrip].GlobalToLocal(x).y());
00201 if(dist<min) {
00202 min = dist;
00203 ustrip = strips[istrip];
00204 }
00205 }
00206 }
00207 if(!ustrip.IsValid()) break;
00208
00209
00210 TVector3 x0 = ustrip.GlobalToLocal(x);
00211
00212 MSG("ToyMC",Msg::kInfo) << "Plane " << plane
00213 << " x: (" << x.x() <<","<< x.y() <<","<< x.z() << ")\t"
00214 << "x0: (" << x0.x() <<","<< x0.y() <<","<< x0.z() << ")\t"
00215
00216
00217 << endl;
00218
00219
00220 if( x0.y() > ustrip.GetHalfWidth()) {
00221 x0.SetY(ustrip.GetHalfWidth()-1.0*Munits::mm);
00222 }
00223
00224 if( x0.y() < -ustrip.GetHalfWidth()) {
00225 x0.SetY(-ustrip.GetHalfWidth()+1.0*Munits::mm);
00226 }
00227
00228
00229 if( fabs(x0.x()) > ustrip.GetHalfLength() ) {
00230 MSG("ToyMC",Msg::kWarning) << "Outside strip length!" << endl;
00231 break;
00232 }
00233 TVector3 x1 = x0;
00234 x1.SetZ(-(ustrip.GetHalfThickness()));
00235 TVector3 x2 = x0;
00236 x2.SetZ( (ustrip.GetHalfThickness()));
00237
00238 double t1 = (x.z() - (ustrip.GetHalfThickness()))/Munits::c_light;
00239 double t2 = (x.z() + (ustrip.GetHalfThickness()))/Munits::c_light;
00240
00241 DigiScintHit hit(1,
00242 13,
00243 1e9,
00244 ustrip.GetSEId(),
00245 t1,
00246 x1.x(), x1.y(), x1.z(),
00247 t2,
00248 x2.x(), x2.y(), x2.z(),
00249 (ustrip.GetHalfThickness())*2,
00250 fEnergyPerPlane);
00251 MSG("ToyMC",Msg::kVerbose) << hit;
00252 hits.push_back(hit);
00253
00254 }
00255
00256 TClonesArray *hitlist = new TClonesArray("DigiScintHit",hits.size());
00257 hitlist->SetName("DigiScintHits");
00258 for(unsigned int i=0;i<hits.size();i++) {
00259 new ((*hitlist)[i]) DigiScintHit( hits[i] );
00260 }
00261 simsnarl->AdoptComponent(hitlist);
00262
00263
00264
00265 TClonesArray* newstdhep = new TClonesArray("TParticle",2);
00266 newstdhep->SetName("StdHep");
00267 new((*newstdhep)[0])
00268 TParticle(13,
00269 0, 0, 0, 0, 0,
00270 0.,0.,10000.,10000.,
00271 0.,0.,1.0,0);
00272 new((*newstdhep)[1])
00273 TParticle(13,
00274 0, 0, 0, 0, 0,
00275 0.,0.,10000.,10000.,
00276 0.,0.,1.0,0);
00277
00278 simsnarl->AdoptComponent(newstdhep);
00279
00280 return JobCResult::kAOK;
00281 }
00282
00283
00284
00285 JobCResult ToyMCModule::Next(int n)
00286 {
00287
00288
00289
00290
00291 fCurrentSnarl+=n;
00292 return this->Get();
00293 }
00294
00295
00296
00297 JobCResult ToyMCModule::Prev(int n)
00298 {
00299 if(fCurrentSnarl>0) fCurrentSnarl-=n;
00300 return this->Get();
00301 }
00302
00303
00304
00305 JobCResult ToyMCModule::GoTo(int , int snarl, int )
00306 {
00307 fCurrentSnarl = snarl;
00308 return this->Get();
00309 }
00310
00311
00312
00313 JobCResult ToyMCModule::Ana(const MomNavigator* mom)
00314 {
00315
00316
00317
00318 CandRecord *candrec = dynamic_cast<CandRecord *>
00319 (mom->GetFragment("CandRecord", "PrimaryCandidateRecord"));
00320 if (candrec == 0) {
00321 MSG("toymc", Msg::kWarning) << "No PrimaryCandidateRecord in MOM."
00322 << endl;
00323 JobCResult result;
00324 result.SetWarning().SetFailed();
00325 return result;
00326 }
00327
00328
00329 CandDigitListHandle *cdlh_ptr = dynamic_cast<CandDigitListHandle *>
00330 (candrec->FindCandHandle("CandDigitListHandle", "canddigitlist"));
00331
00332 if(cdlh_ptr==0) return JobCResult::kError;
00333 CandDigitListHandle cdlh = *cdlh_ptr;
00334
00335 CandDigitHandleItr cdhItr(cdlh.GetDaughterIterator());
00336
00337
00338
00339
00340
00341 return JobCResult::kPassed;
00342 }