Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

MCInitModule.cxx

Go to the documentation of this file.
00001 
00002 //
00003 // MCInitModule.cxx
00004 //
00005 // Purpose: A Demo JobControl Module for creating the MC initial state.
00006 //
00007 // Author: S. Kasahara 11/05 
00008 //
00010 
00011 #include <iostream>
00012 #include <cassert>
00013 using namespace std;
00014 
00015 #include <TClonesArray.h>
00016 #include <TParticle.h>
00017 #include <TSystem.h>
00018 #include <TDatabasePDG.h>
00019 
00020 #include "ParticleTransportSim/test/MCInitModule.h"
00021 #include "MessageService/MsgService.h"
00022 #include "JobControl/JobCModuleRegistry.h"
00023 #include "JobControl/JobCommand.h"
00024 #include "Record/SimSnarlRecord.h"
00025 #include "MinosObjectMap/MomNavigator.h"
00026 #include "REROOT_Classes/NeuKin.h"
00027 #include "REROOT_Classes/REROOT_NeuKin.h"
00028 
00029 ClassImp(MCInitModule)
00030 
00031 //   Definition of static data members
00032 //   *********************************
00033 
00034 CVSID("$Id: MCInitModule.cxx,v 1.5 2009/06/22 03:51:01 kasahara Exp $");
00035 JOBMODULE(MCInitModule, "MCInitModule",
00036                              "A module for initializing the MC snarl.");
00037 
00038 
00039 // Definition of methods 
00040 // *********************
00041 
00042 //......................................................................
00043 
00044 const char* MCInitModule::AsString(EInitMethod initmethod) {
00045   //
00046   // Purpose: Static method to convert enumerated init method to a string
00047   //
00048 
00049   switch ( initmethod ) {
00050 
00051   case kSetKine:
00052     return "SetKine";
00053 
00054   default:
00055     MSG("MCInit",Msg::kWarning) 
00056     << "MCInitModule::AsString called with unknown EInitMethod "
00057     << (int)initmethod << endl;
00058     return "Unknown";
00059 
00060   } // end of switch
00061   
00062 }
00063 
00064 //......................................................................
00065 
00066 MCInitModule::MCInitModule() :
00067   fRun(0),fSubRun(0),fSnarl(-1),fDetector(Detector::kFar),
00068   fSimFlag(SimFlag::kMC),fSnarlTimeInterval((time_t)1,900000000),
00069   fInitMethod(kSetKine),fKineParticle(0) {
00070   //
00071   // Purpose: Default constructor
00072   //
00073   
00074 }
00075 
00076 //......................................................................
00077 
00078 MCInitModule::~MCInitModule() {
00079   // 
00080   // Purpose:: Destructor
00081   //
00082 
00083   if ( fKineParticle ) delete fKineParticle; fKineParticle = 0;
00084   
00085 }
00086 
00087   
00088 //......................................................................
00089 
00090 const Registry& MCInitModule::DefaultConfig() const {
00091   //
00092   // Purpose: Method to return default configuration.
00093   // 
00094 
00095   MSG("MCInit",Msg::kDebug) << "MCInitModule::DefaultConfig" << endl;
00096 
00097   static Registry r; 
00098   std::string name = this->JobCModule::GetName();
00099   name += ".config.default";
00100   r.SetName(name.c_str());
00101   r.UnLockValues();
00102 
00103   r.Set("Run", 0);
00104   r.Set("SubRun", 0);
00105   r.Set("Snarl", 0);
00106   r.Set("Detector", (int)Detector::kFar);
00107   r.Set("SimFlag", (int)SimFlag::kMC);
00108   r.Set("StartDate",0); 
00109   r.Set("SnarlTimeInterval",1.9);
00110   r.Set("InitMethod",(int)kSetKine);
00111  
00112   r.LockValues();
00113 
00114   return r;
00115 }
00116 
00117 //......................................................................
00118 void MCInitModule::Config(const Registry& r) {
00119   //
00120   // Purpose: Configure the module.
00121   //
00122 
00123   MSG("MCInit",Msg::kDebug) << "MCInitModule::Config" << endl;
00124   
00125   Int_t tmpi;
00126   Double_t tmpd;
00127   //  const Char_t* tmps;
00128 
00129   if ( r.Get("Run",tmpi) ) fRun = tmpi;
00130   if ( r.Get("SubRun",tmpi) ) fSubRun = tmpi;
00131   if ( r.Get("Snarl",tmpi) ) fSnarl = tmpi - 1; // will increment on first Get
00132   if ( r.Get("SimFlag",tmpi) ) fSimFlag = (SimFlag::SimFlag_t)tmpi;
00133   if ( r.Get("Detector",tmpi) ) fDetector = (Detector::Detector_t)tmpi;
00134   
00135   if ( r.Get("StartDate",tmpi) ) {
00136     if ( tmpi > 0 ) fStartTimeStamp = VldTimeStamp(tmpi,0,0);
00137     else fStartTimeStamp = VldTimeStamp(); // set to current time
00138   }
00139   if ( r.Get("SnarlTimeInterval",tmpd) ) {
00140     Int_t sec = (Int_t)tmpd; // truncate
00141     Int_t nsec = (Int_t)((tmpd - (Double_t)sec)*1.e9);
00142     fSnarlTimeInterval = VldTimeStamp(sec,nsec);
00143   }
00144        
00145   if ( r.Get("InitMethod",tmpi) ) {
00146     fInitMethod = (EInitMethod)tmpi;
00147   }
00148        
00149   return;
00150   
00151 }
00152     
00153 //......................................................................
00154 void MCInitModule::BeginJob() {
00155   //
00156   // Purpose: Called at the beginning of the job, after module configuration
00157  
00158   MSG("MCInit",Msg::kDebug) << "MCInitModule::BeginJob " << endl;
00159   
00160   // Rewind, because first Get() will move forward
00161   fStartTimeStamp = fStartTimeStamp - fSnarlTimeInterval; 
00162   fCurrentVld = VldContext(fDetector,fSimFlag,fStartTimeStamp);
00163 
00164   if ( fInitMethod == kSetKine ) {
00165     if ( fKineParticle ) {
00166       MSG("MCInit",Msg::kDebug) << "Initialization method "
00167           << AsString(kSetKine) << "\npdg " << fKineParticle->GetPdgCode() 
00168           << "/" << fKineParticle->GetName()  
00169           << " vtx(x,y,z)(m) (" << fKineParticle->Vx()  << "," 
00170           <<  fKineParticle->Vy() << "," << fKineParticle->Vz() << ")" 
00171           << " p(x,y,z)(GeV/c) (" << fKineParticle->Px() << "," 
00172           <<  fKineParticle->Py() << "," << fKineParticle->Pz() << ")"
00173           << endl;
00174     }
00175     else {
00176       MSG("MCInit",Msg::kFatal) << "SetKine Initialization method chosen,\n"
00177           << "but no initial state defined. Use\n"
00178           << "jc.Mod(''MCInit'').Cmd(''SetKine pdgId vx vy vz px py pz'')\n" 
00179           << "Abort!" << endl;
00180       abort();
00181     }
00182   }
00183            
00184 }
00185   
00186 //......................................................................
00187 JobCResult MCInitModule::Get(MomNavigator *mom) {
00188   //
00189   // Purpose:  Create and fill SimSnarlRecord.
00190   //
00191   // Notes: A SimSnarlRecord is generated with contents:
00192   //          i) TClonesArray of name NeuKinList containing objects of type
00193   //             REROOT_NeuKin.
00194   //         ii) TClonesArray of name StdHep containing objects of type
00195   //             TParticle.
00196   // See description at top of MCInitModule.h for more information.
00197   //
00198 
00199   JobCResult result(JobCResult::kPassed);  
00200   MSG("MCInit",Msg::kDebug) << "MCInitModule::Get" << endl;
00201 
00202   // Check that mom exists.
00203   assert(mom);
00204 
00205   SimSnarlRecord* simrec = 0;
00206   // Check for existence of SimSnarlRecord in Mom 
00207   simrec = dynamic_cast<SimSnarlRecord*>(mom -> GetFragment("SimSnarlRecord"));
00208   if ( simrec ) {
00209     MSG("MCInit",Msg::kFatal) << "SimSnarlRecord present in input Mom!" 
00210                               << endl;
00211     abort(); // don't want to think about it yet
00212   }
00213 
00214   fSnarl++;
00215   fCurrentVld = VldContext(fDetector,fSimFlag,
00216                            GetCurrentTimeStamp() + fSnarlTimeInterval);
00217   // Not sure what to do with these flags yet. Copy RerootExodus
00218   Short_t runtype = 0;
00219   UInt_t  trigsrc = 0;
00220   UInt_t errcode = 0;
00221   // time frames in 1 second buckets, starting with timeframe 0
00222   Int_t timeframe = (GetCurrentTimeStamp() - fStartTimeStamp 
00223                    - fSnarlTimeInterval).GetSec(); 
00224   Int_t spilltype = -1;
00225 
00226   VldTimeStamp now;
00227   std::string  codename = SimSnarlHeader::TrimCodename("$Name:  $");
00228   std::string  hostname(gSystem->HostName());
00229 
00230   SimSnarlHeader simheader(fCurrentVld,fRun,fSubRun,runtype,errcode,fSnarl,
00231                            trigsrc,timeframe,spilltype,
00232                            now,codename,hostname);
00233   simrec = new SimSnarlRecord(simheader);
00234 
00235   MSG("MCInit",Msg::kDebug) 
00236     << "MCInitModule::Get created new SimSnarlRecord.\n" 
00237     <<  simheader << endl;
00238 
00239   mom -> AdoptFragment(simrec);
00240 
00241   BuildInitialState(simrec);
00242   
00243   return result;
00244   
00245 }
00246 
00247 //....................................................................
00248 void MCInitModule::HandleCommand(JobCommand* cmd) {
00249   // 
00250   // Handle script commands that don't work through Config:
00251   //   jc.Mod("MCInit").Cmd("SetKine pdgId vx vy vz px py pz");
00252 
00253   
00254   if (!cmd->HaveCmd()) return;
00255   
00256   string s = cmd->PopCmd();
00257   if ( s == "SetKine" ) {
00258     bool isOkay = false;
00259     if (cmd -> HaveOpt() ) {
00260       int pdg = cmd->PopIntOpt();
00261       if ( cmd -> HaveOpt() ) {
00262         double vx = cmd->PopFloatOpt();
00263         if ( cmd -> HaveOpt() ) {
00264           double vy = cmd->PopFloatOpt();
00265           if ( cmd -> HaveOpt() ) {
00266             double vz = cmd->PopFloatOpt();
00267             if ( cmd -> HaveOpt() ) {
00268               double px = cmd->PopFloatOpt();
00269               if ( cmd -> HaveOpt() ) {
00270                 double py = cmd -> PopFloatOpt();
00271                 if ( cmd -> HaveOpt() ) {
00272                   double pz = cmd -> PopFloatOpt();
00273                   isOkay = true;
00274                   SetKine(pdg,vx,vy,vz,px,py,pz);
00275                 }
00276               }
00277             }
00278           }
00279         }
00280       }
00281     }
00282     if (!isOkay ) {
00283       MSG("MCInit",Msg::kFatal) 
00284         << "SetKine called w/incorrect parameter list. Abort!" << endl;
00285       abort();
00286     }
00287     return;
00288   }
00289     
00290   // Errors fall through to here
00291   MSG("MCInit",Msg::kError) << "Illegal command: " << s.c_str() << endl;
00292   
00293 }
00294 
00295   
00296 //.....................................................................
00297 void MCInitModule::SetKine(Int_t pdg, 
00298                                 Double_t vx, Double_t vy, Double_t vz,
00299                                 Double_t px, Double_t py, Double_t pz) { 
00300   //
00301   // Purpose: Method to set initial state kinematics.  
00302   // 
00303   // Arguments: particle id (pdg)
00304   //            vtx & momentum in MINOS standard units
00305   //            (m and GeV/c respectively)
00306   // 
00307   // Notes: fKineParticle (TParticle) will be constructed from the arguments.
00308   //
00309 
00310   const TDatabasePDG& dbpdg = *(TDatabasePDG::Instance());
00311   TParticlePDG* partpdg = dbpdg.GetParticle(pdg);
00312 
00313   std::string partname = "???";
00314   if ( partpdg ) partname=partpdg->GetName();
00315   
00316   if ( fKineParticle ) {
00317     delete fKineParticle; fKineParticle = 0;
00318   }
00319   
00320   // 1 is neugen status code for final state particle 
00321   // 0 is for neutrino/nucleus primaries
00322   Int_t status = 1; 
00323   Int_t mother1 = -1;
00324   Int_t mother2 = -1;
00325   Int_t daughter1 = -1;
00326   Int_t daughter2 = -1;
00327   Double_t mass = partpdg -> Mass();
00328   Double_t etot = TMath::Sqrt(px*px + py*py + pz*pz + mass*mass);
00329   Double_t time = 0;
00330   fKineParticle = new TParticle(pdg,status,mother1,mother2,daughter1,
00331                                 daughter2,px,py,pz,etot,vx,vy,vz,time);
00332    
00333 }
00334   
00335 //......................................................................
00336 void MCInitModule::BuildInitialState(SimSnarlRecord* simrec) {
00337   //
00338   //  Purpose: Private method to build initial state particles according
00339   //           to specified method EInitMethod.  NeuKin and StdHep arrays
00340   //           are created and filled in SimSnarlRecord.
00341   //
00342   //  Arguments: SimSnarlRecord.
00343   //  
00344   //  Return: none.
00345   // 
00346 
00347 
00348   switch (fInitMethod) {
00349 
00350   case kSetKine:
00351     BuildSetKine(simrec);
00352     break;
00353 
00354   default:
00355     MSG("MCInit",Msg::kFatal) << "Unknown EInitMethod " 
00356            << MCInitModule::AsString(fInitMethod) << ". Abort!" << endl;
00357     abort();
00358   }
00359 
00360 }
00361 
00362 //......................................................................
00363 void MCInitModule::BuildSetKine(SimSnarlRecord* simrec) {
00364   //
00365   //  Purpose: Private method to build initial state particles according
00366   //           to specified method EInitMethod::kSetKine.  
00367   //           NeuKin and StdHep arrays are created and filled in 
00368   //           SimSnarlRecord.
00369   //
00370   //  Arguments: SimSnarlRecord.
00371   //  
00372   //  Return: none.
00373   // 
00374 
00375 
00376   // Create StdHep entry
00377   Int_t nstdhep = 1; // 1 primary track
00378   TClonesArray* stdheparray = new TClonesArray("TParticle",nstdhep);
00379   stdheparray -> SetName("StdHep");
00380   simrec -> AdoptComponent(stdheparray);
00381 
00382   if ( !fKineParticle ) {
00383     MSG("MCInit",Msg::kFatal) << "SetKine Initialization method chosen,\n"
00384           << "but no initial state defined. Use\n"
00385           << "jc.Mod(''MCInit'').Cmd(''SetKine pdgId vx vy vz px py pz'')\n" 
00386           << "Abort!" << endl;
00387     abort();
00388   }
00389   new ((*stdheparray)[0])TParticle(*fKineParticle);
00390 
00391 
00392   // Create single dummy REROOT_NeuKin object, REROOT_NeuKin to be replaced
00393   Int_t nneukin = 1; // 1 primary
00394   TClonesArray* neukinarray = new TClonesArray("REROOT_NeuKin",nneukin);
00395   neukinarray -> SetName("NeuKinList");
00396   simrec -> AdoptComponent(neukinarray);
00397 
00398   // This bit of nonsense is because REROOT_NeuKin constructor doesn't
00399   // initialize data members
00400   NEUKIN_DEF neukin;
00401   neukin.ID= 1; //index of this neukin, set to 1 cuz REROOT_NeuKin subtracts 1
00402   neukin.INu = 0; // particle id
00403   neukin.INuNoOsc = 0;
00404   neukin.ITg = 0;
00405   neukin.IBoson = -99999; // to be the same as reroot
00406   neukin.IResonance = 0;
00407   neukin.IAction = 0;
00408   neukin.A = 0;
00409   neukin.Z = 0;
00410   neukin.Sigma = 0;
00411   for ( int ic = 0; ic < 4; ic++ ) {
00412     neukin.P4Neu[ic] = 0;
00413     neukin.P4NeuNoOsc[ic] = 0;
00414     neukin.P4Tgt[ic] = 0;
00415   }
00416   neukin.X = 0;
00417   neukin.Y = 0;
00418   neukin.Q2 = 0;
00419   neukin.W2 = 0;
00420   for ( int ic = 0; ic < 4; ic++ ) {
00421     neukin.P4Shw[ic] = 0;
00422     neukin.P4Mu1[ic] = 0;
00423     neukin.P4Mu2[ic] = 0;
00424   }
00425   if (TMath::Abs(fKineParticle->GetPdgCode()) == 13 ) {
00426     // Muon primary, gminos/reroot labels these muon final state
00427     neukin.P4Mu1[0] = fKineParticle->Px();
00428     neukin.P4Mu1[1] = fKineParticle->Py();
00429     neukin.P4Mu1[2] = fKineParticle->Pz();
00430     neukin.P4Mu1[3] = fKineParticle->Energy();
00431   }
00432   neukin.EMFrac = 0;
00433   for ( int ic = 0; ic < 4; ic++ ) {
00434     neukin.P4El1[ic] = 0;
00435     neukin.P4El2[ic] = 0;
00436     neukin.P4Tau[ic] = 0;
00437   }
00438   
00439   // new with placement, create dummy REROOT_NeuKin
00440   new ((*neukinarray)[0])REROOT_NeuKin(&neukin);
00441 
00442   
00443   return;
00444   
00445 }
00446 

Generated on Mon Feb 15 11:06:58 2010 for loon by  doxygen 1.3.9.1