22 #include "TDatabasePDG.h" 23 #include "THashList.h" 24 #include "TCollection.h" 39 #include "Geant4/G4UImanager.hh" 40 #include "Geant4/G4RunManager.hh" 41 #include "Geant4/G4VisExecutive.hh" 42 #include "Geant4/QGSP_BERT.hh" 100 which_(p.get<
std::string>(
"which",
"truth")),
107 throw cet::exception(
"Landed") <<
"no output filename specified and unable to connect to LANDED app using local socket\n" <<
108 "please either specify a filename, or check LANDED is running and that you have started the server\n";
118 G4RunManager* runManager=
new G4RunManager;
120 runManager->SetUserInitialization(
new QGSP_BERT);
122 G4VisManager* visManager =
new G4VisExecutive;
123 visManager->Initialize();
126 strcpy(tmpdir,
"/tmp/vcXXXXXX");
129 char* vrmlviewer=getenv(
"G4VRMLFILE_VIEWER");
131 unsetenv(
"G4VRMLFILE_VIEWER");
132 char* oldvrmldir=getenv(
"G4VRMLFILE_DEST_DIR");
133 setenv(
"G4VRMLFILE_DEST_DIR", tmpdir, 1);
134 G4UImanager* UImanager = G4UImanager::GetUIpointer();
135 UImanager->ApplyCommand(
"/run/initialize");
136 UImanager->ApplyCommand(
"/vis/open VRML2FILE");
137 UImanager->ApplyCommand(
"/vis/drawVolume");
138 UImanager->ApplyCommand(
"/vis/viewer/flush");
140 setenv(
"G4VRMLFILE_DEST_DIR", oldvrmldir, 1);
142 unsetenv(
"G4VRMLFILE_DEST_DIR");
144 setenv(
"G4VRMLFILE_VIEWER", vrmlviewer, 1);
147 strcpy(tmpname, tmpdir);
148 strcat(tmpname,
"g4_00.wrl");
149 std::ifstream geofile(tmpname);
150 std::stringstream geodata;
151 geodata << geofile.rdbuf();
168 uLongf complen=compressBound(
vrml_.length());
169 Bytef* comp=
new Bytef[complen];
170 if (compress(comp, &complen, (
const Bytef*)
vrml_.c_str(),
vrml_.length())!=Z_OK)
171 throw cet::exception(
"Landed") <<
"failed to compress geometry vrml\n";
176 if (sqlite3_open_v2(
outputFilename_.c_str(), &
ppDb_, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
"unix")!=SQLITE_OK)
177 throw cet::exception(
"Landed") <<
"failed to create sqlite file\n";
180 sqlite3_exec(
ppDb_,
"PRAGMA synchronous = OFF", NULL, NULL, &sErrMsg);
184 if (sqlite3_prepare(
ppDb_,
"CREATE TABLE Geometry ( vrml BLOB, size INT );", 100, &stmt,
nullptr)!=SQLITE_OK)
185 throw cet::exception(
"Landed") <<
"failed to create geometry table on prepare\n";
186 if (sqlite3_step(stmt)!=SQLITE_DONE)
187 throw cet::exception(
"Landed") <<
"failed to create geometry table on step\n";
188 if (sqlite3_finalize(stmt)!=SQLITE_OK)
189 throw cet::exception(
"Landed") <<
"failed to create geometry table on finalize\n";
191 if (sqlite3_prepare(
ppDb_,
"INSERT INTO Geometry ( vrml, size ) VALUES ( ?, ? );", -1, &stmt,
nullptr)!=SQLITE_OK)
192 throw cet::exception(
"Landed") <<
"failed to fill geometry table on prepare\n";
193 if (sqlite3_bind_blob(stmt, 1, comp, complen, SQLITE_STATIC)!=SQLITE_OK ||
194 sqlite3_bind_int(stmt, 2,
vrml_.length())!=SQLITE_OK)
195 throw cet::exception(
"Landed") <<
"failed to fill geometry table on bind\n";
196 if (sqlite3_step(stmt)!=SQLITE_DONE)
197 throw cet::exception(
"Landed") <<
"failed to fill geometry table on step\n";
198 if (sqlite3_finalize(stmt)!=SQLITE_OK)
199 throw cet::exception(
"Landed") <<
"failed to fill geometry table on finalize\n";
203 if (sqlite3_prepare(
ppDb_,
"CREATE TABLE Events ( RunNumber INT, SubRunNumber INT, EventNumber INT);", 200, &stmt,
nullptr)!=SQLITE_OK)
204 throw cet::exception(
"Landed") <<
"failed to create events table on prepare\n";
205 if (sqlite3_step(stmt)!=SQLITE_DONE)
206 throw cet::exception(
"Landed") <<
"failed to create events table on step\n";
207 if (sqlite3_finalize(stmt)!=SQLITE_OK)
208 throw cet::exception(
"Landed") <<
"failed to create events table on finalize\n";
211 if (sqlite3_prepare(
ppDb_,
"CREATE TABLE Hits ( RunNumber INT, SubRunNumber INT, EventNumber INT, NumElectrons REAL, Energy REAL, X REAL, Y REAL, Z REAL, Track INT);", 200, &stmt,
nullptr)!=SQLITE_OK)
212 throw cet::exception(
"Landed") <<
"failed to create hits table on prepare\n";
213 if (sqlite3_step(stmt)!=SQLITE_DONE)
214 throw cet::exception(
"Landed") <<
"failed to create hits table on step\n";
215 if (sqlite3_finalize(stmt)!=SQLITE_OK)
216 throw cet::exception(
"Landed") <<
"failed to create hits table on finalize\n";
219 if (sqlite3_prepare(
ppDb_,
"CREATE TABLE Tracks (RunNumber INT, SubRunNumber INT, EventNumber INT, ID INT, PDGCode INT, Segment INT, Energy REAL, X REAL, Y REAL, Z REAL);", 200, &stmt,
nullptr)!=SQLITE_OK)
220 throw cet::exception(
"Landed") <<
"failed to create tracks table on prepare\n";
221 if (sqlite3_step(stmt)!=SQLITE_DONE)
222 throw cet::exception(
"Landed") <<
"failed to create tracks table on step\n";
223 if (sqlite3_finalize(stmt)!=SQLITE_OK)
224 throw cet::exception(
"Landed") <<
"failed to create tracks table on finalize\n";
235 sqlite3_close(
ppDb_);
253 std::map<size_t, int> pdgcodes;
260 std::string whichcalo;
262 whichcalo=
which_.substr(0,
which_.length()-2)+std::string(
"calodc");
264 whichcalo=
which_+std::string(
"calo");
268 event.getByLabel(
"largeant", mcparticles);
269 event.getByLabel(
"largeant", simchannels);
273 event.getByLabel(whichcalo, calorimetry);
276 event.getByLabel(whichcalo, track2calo);
277 event.getByLabel(
which_, particle2track);
279 for (
size_t i=0; i<particle2track->size(); i++)
282 for (
size_t j=0; j<track2calo->size(); j++)
284 if (track2calo->at(j).first==particle2track->at(i).second)
286 for (
size_t k=0; k<calorimetry->size(); k++)
290 pdgcodes[k]=particle2track->at(i).first->PdgCode();
328 for (
auto const& simchannel:*simchannels)
331 for (
auto const& tdcide:simchannel.TDCIDEMap())
332 hitcount+=tdcide.second.size();
333 if (mcparticles->size())
336 for (
auto const&
part:*mcparticles)
338 for (
size_t i=0; i<
part.NumberTrajectoryPoints(); i++)
347 for (
size_t tracknum=0; tracknum<calorimetry->size(); tracknum++)
349 hitcount+=calorimetry->at(tracknum).XYZ().size();
355 if (sqlite3_prepare(
ppDb_,
"INSERT INTO Events (RunNumber, SubRunNumber, EventNumber) VALUES ( ?, ?, ? );", 200, &stmt,
nullptr)!=SQLITE_OK)
356 throw cet::exception(
"Landed") <<
"failed to fill events table on prepare\n";
357 if (sqlite3_bind_int(stmt, 1, runnum)!=SQLITE_OK)
358 throw cet::exception(
"Landed") <<
"failed to fill events table on bind\n";
359 if (sqlite3_bind_int(stmt, 2, subrunnum)!=SQLITE_OK)
360 throw cet::exception(
"Landed") <<
"failed to fill events table on bind\n";
361 if (sqlite3_bind_int(stmt, 3, eventnum)!=SQLITE_OK)
362 throw cet::exception(
"Landed") <<
"failed to fill events table on bind\n";
363 if (sqlite3_step(stmt)!=SQLITE_DONE)
364 throw cet::exception(
"Landed") <<
"failed to fill events table on step\n";
365 if (sqlite3_finalize(stmt)!=SQLITE_OK)
366 throw cet::exception(
"Landed") <<
"failed to fill events table on finalize\n";
371 for (
auto const& simchannel:*simchannels)
375 sqlite3_exec(
ppDb_,
"BEGIN TRANSACTION", NULL, NULL, &sErrMsg);
376 for (
auto const& tdcide:simchannel.TDCIDEMap())
379 auto const& idevec=tdcide.second;
380 for (
auto const& ide:idevec)
383 if (sqlite3_prepare(
ppDb_,
"INSERT INTO Hits (RunNumber, SubRunNumber, EventNumber, NumElectrons, Energy, X, Y, Z, Track ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? );", 200, &stmt,
nullptr)!=SQLITE_OK)
384 throw cet::exception(
"Landed") <<
"failed to fill hits table on prepare\n";
385 if (sqlite3_bind_int(stmt, 1, runnum)!=SQLITE_OK)
386 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
387 if (sqlite3_bind_int(stmt, 2, subrunnum)!=SQLITE_OK)
388 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
389 if (sqlite3_bind_int(stmt, 3, eventnum)!=SQLITE_OK)
390 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
391 if (sqlite3_bind_double(stmt, 4, ide.numElectrons)!=SQLITE_OK)
392 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
393 if (sqlite3_bind_double(stmt, 5, ide.energy)!=SQLITE_OK)
394 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
395 if (sqlite3_bind_double(stmt, 6, ide.x)!=SQLITE_OK)
396 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
397 if (sqlite3_bind_double(stmt, 7, ide.y)!=SQLITE_OK)
398 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
399 if (sqlite3_bind_double(stmt, 8, ide.z)!=SQLITE_OK)
400 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
401 if (sqlite3_bind_int(stmt, 9, ide.trackID)!=SQLITE_OK)
402 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
403 if (sqlite3_step(stmt)!=SQLITE_DONE)
404 throw cet::exception(
"Landed") <<
"failed to fill hits table on step\n";
405 if (sqlite3_finalize(stmt)!=SQLITE_OK)
406 throw cet::exception(
"Landed") <<
"failed to fill hits table on finalize\n";
410 sqlite3_exec(
ppDb_,
"END TRANSACTION", NULL, NULL, &sErrMsg);
414 sqlite3_exec(
ppDb_,
"BEGIN TRANSACTION", NULL, NULL, &sErrMsg);
415 for (
auto const&
part:*mcparticles)
417 for (
size_t i=0; i<
part.NumberTrajectoryPoints(); i++)
419 if (sqlite3_prepare(
ppDb_,
"INSERT INTO Tracks ( RunNumber, SubRunNumber, EventNumber, ID, PDGCode, Segment, Energy, X, Y, Z) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );", 200, &stmt,
nullptr)!=SQLITE_OK)
420 throw cet::exception(
"Landed") <<
"failed to fill tracks table on prepare\n";
421 if (sqlite3_bind_int(stmt, 1, runnum)!=SQLITE_OK)
422 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
423 if (sqlite3_bind_int(stmt, 2, subrunnum)!=SQLITE_OK)
424 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
425 if (sqlite3_bind_int(stmt, 3, eventnum)!=SQLITE_OK)
426 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
427 if (sqlite3_bind_int(stmt, 4,
part.TrackId())!=SQLITE_OK)
428 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
429 if (sqlite3_bind_int(stmt, 5,
part.PdgCode())!=SQLITE_OK)
430 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
431 if (sqlite3_bind_int(stmt, 6, i)!=SQLITE_OK)
432 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
433 if (sqlite3_bind_double(stmt, 7,
part.E(i))!=SQLITE_OK)
434 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
435 if (sqlite3_bind_double(stmt, 8,
part.Vx(i))!=SQLITE_OK)
436 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
437 if (sqlite3_bind_double(stmt, 9,
part.Vy(i))!=SQLITE_OK)
438 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
439 if (sqlite3_bind_double(stmt, 10,
part.Vz(i))!=SQLITE_OK)
440 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
441 if (sqlite3_step(stmt)!=SQLITE_DONE)
442 throw cet::exception(
"Landed") <<
"failed to fill tracks table on step\n";
443 if (sqlite3_finalize(stmt)!=SQLITE_OK)
444 throw cet::exception(
"Landed") <<
"failed to fill tracks table on finalize\n";
447 sqlite3_exec(
ppDb_,
"END TRANSACTION", NULL, NULL, &sErrMsg);
452 sqlite3_exec(
ppDb_,
"BEGIN TRANSACTION", NULL, NULL, &sErrMsg);
454 for (
size_t tracknum=0; tracknum<calorimetry->size(); tracknum++)
456 auto const&
calo=calorimetry->at(tracknum);
457 for (
size_t hitnum=0; hitnum<
calo.dEdx().size() && hitnum<
calo.dQdx().size() && hitnum<
calo.XYZ().size(); hitnum++)
459 if (sqlite3_prepare(
ppDb_,
"INSERT INTO Hits (RunNumber, SubRunNumber, EventNumber, NumElectrons, Energy, X, Y, Z, Track ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? );", 200, &stmt,
nullptr)!=SQLITE_OK)
460 throw cet::exception(
"Landed") <<
"failed to fill hits table on prepare\n";
461 if (sqlite3_bind_int(stmt, 1, runnum)!=SQLITE_OK)
462 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
463 if (sqlite3_bind_int(stmt, 2, subrunnum)!=SQLITE_OK)
464 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
465 if (sqlite3_bind_int(stmt, 3, eventnum)!=SQLITE_OK)
466 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
467 if (sqlite3_bind_double(stmt, 4,
calo.TrkPitchVec().at(hitnum)*
calo.dQdx().at(hitnum))!=SQLITE_OK)
468 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
469 if (sqlite3_bind_double(stmt, 5,
calo.TrkPitchVec().at(hitnum)*
calo.dEdx().at(hitnum))!=SQLITE_OK)
470 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
471 if (sqlite3_bind_double(stmt, 6,
calo.XYZ().at(hitnum).X())!=SQLITE_OK)
472 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
473 if (sqlite3_bind_double(stmt, 7,
calo.XYZ().at(hitnum).Y())!=SQLITE_OK)
474 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
475 if (sqlite3_bind_double(stmt, 8,
calo.XYZ().at(hitnum).Z())!=SQLITE_OK)
476 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
477 if (sqlite3_bind_int(stmt, 9, tracknum+1)!=SQLITE_OK)
478 throw cet::exception(
"Landed") <<
"failed to fill hits table on bind\n";
479 if (sqlite3_step(stmt)!=SQLITE_DONE)
480 throw cet::exception(
"Landed") <<
"failed to fill hits table on step\n";
481 if (sqlite3_finalize(stmt)!=SQLITE_OK)
482 throw cet::exception(
"Landed") <<
"failed to fill hits table on finalize\n";
485 sqlite3_exec(
ppDb_,
"END TRANSACTION", NULL, NULL, &sErrMsg);
487 sqlite3_exec(
ppDb_,
"BEGIN TRANSACTION", NULL, NULL, &sErrMsg);
488 for (
size_t tracknum=0; tracknum<calorimetry->size(); tracknum++)
490 auto const&
calo=calorimetry->at(tracknum);
491 if (
calo.dEdx().size()>1)
494 if (pdgcodes.find(tracknum)!=pdgcodes.end())
495 pdgcode=pdgcodes[tracknum];
497 for (
size_t hitnum=0; hitnum<
calo.dEdx().size() && hitnum<
calo.dQdx().size() && hitnum<
calo.XYZ().size(); hitnum++)
499 if (sqlite3_prepare(
ppDb_,
"INSERT INTO Tracks ( RunNumber, SubRunNumber, EventNumber, ID, PDGCode, Segment, Energy, X, Y, Z) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );", 200, &stmt,
nullptr)!=SQLITE_OK)
500 throw cet::exception(
"Landed") <<
"failed to fill tracks table on prepare\n";
501 if (sqlite3_bind_int(stmt, 1, runnum)!=SQLITE_OK)
502 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
503 if (sqlite3_bind_int(stmt, 2, subrunnum)!=SQLITE_OK)
504 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
505 if (sqlite3_bind_int(stmt, 3, eventnum)!=SQLITE_OK)
506 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
507 if (sqlite3_bind_int(stmt, 4, tracknum+1)!=SQLITE_OK)
508 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
509 if (sqlite3_bind_int(stmt, 5, pdgcode)!=SQLITE_OK)
510 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
511 if (sqlite3_bind_int(stmt, 6, hitnum+1)!=SQLITE_OK)
512 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
513 if (sqlite3_bind_double(stmt, 7, energy)!=SQLITE_OK)
514 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
515 if (sqlite3_bind_double(stmt, 8,
calo.XYZ().at(hitnum).X())!=SQLITE_OK)
516 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
517 if (sqlite3_bind_double(stmt, 9,
calo.XYZ().at(hitnum).Y())!=SQLITE_OK)
518 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
519 if (sqlite3_bind_double(stmt, 10,
calo.XYZ().at(hitnum).Z())!=SQLITE_OK)
520 throw cet::exception(
"Landed") <<
"failed to fill tracks table on bind\n";
521 if (sqlite3_step(stmt)!=SQLITE_DONE)
522 throw cet::exception(
"Landed") <<
"failed to fill tracks table on step\n";
523 if (sqlite3_finalize(stmt)!=SQLITE_OK)
524 throw cet::exception(
"Landed") <<
"failed to fill tracks table on finalize\n";
525 energy-=
calo.TrkPitchVec().at(hitnum)*
calo.dEdx().at(hitnum);
533 sock_->sendEvent(hitcount, trajtotal, runnum, subrunnum, eventnum);
538 for (
auto const& simchannel:*simchannels)
541 for (
auto const& tdcide:simchannel.TDCIDEMap())
543 auto const& idevec=tdcide.second;
544 for (
auto const& ide:idevec)
546 sock_->sendHit(ide.x, ide.y, ide.z, ide.energy, ide.numElectrons, ide.trackID);
550 for (
auto const&
part:*mcparticles)
552 for (
size_t i=0; i<
part.NumberTrajectoryPoints(); i++)
561 for (
size_t tracknum=0; tracknum<calorimetry->size(); tracknum++)
563 auto const&
calo=calorimetry->at(tracknum);
564 for (
size_t hitnum=0; hitnum<
calo.dEdx().size() && hitnum<
calo.dQdx().size() && hitnum<
calo.XYZ().size(); hitnum++)
566 sock_->sendHit(
calo.XYZ().at(hitnum).X(),
calo.XYZ().at(hitnum).Y(),
calo.XYZ().at(hitnum).Z(),
calo.TrkPitchVec().at(hitnum)*
calo.dEdx().at(hitnum),
calo.TrkPitchVec().at(hitnum)*
calo.dQdx().at(hitnum), tracknum+1);
569 for (
size_t tracknum=0; tracknum<calorimetry->size(); tracknum++)
572 if (pdgcodes.find(tracknum)!=pdgcodes.end())
573 pdgcode=pdgcodes[tracknum];
574 auto const&
calo=calorimetry->at(tracknum);
575 if (
calo.dEdx().size()>1)
578 for (
size_t hitnum=0; hitnum<
calo.dEdx().size() && hitnum<
calo.dQdx().size() && hitnum<
calo.XYZ().size(); hitnum++)
580 sock_->sendVertex(
calo.XYZ().at(hitnum).X(),
calo.XYZ().at(hitnum).Y(),
calo.XYZ().at(hitnum).Z(),
energy, tracknum+1, pdgcode);
581 energy-=
calo.TrkPitchVec().at(hitnum)*
calo.dEdx().at(hitnum);
Build Geant4 geometry from GDML.
std::vector< int > events_
art::ServiceHandle< geo::Geometry > geom_
Landed & operator=(Landed const &)=delete
Definition of basic raw digits.
SigType_t SignalType(geo::PlaneID const &pid) const
Returns the type of signal on the channels of specified TPC plane.
std::string GDMLFile() const
Returns the full directory path to the GDML file source.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
contains objects relating to OpDet hits
object containing MC truth information necessary for making RawDigits and doing back tracking ...
#define DEFINE_ART_MODULE(klass)
Landed(fhicl::ParameterSet const &p)
IDNumber_t< Level::SubRun > SubRunNumber_t
EDAnalyzer(Table< Config > const &config)
Provides recob::Track data product.
std::string outputFilename_
object containing MC truth information necessary for making RawDigits and doing back tracking ...
IDNumber_t< Level::Event > EventNumber_t
void analyze(art::Event const &e) override
Namespace collecting geometry-related classes utilities.
art framework interface to geometry description
art::ServiceHandle< LandedSocket > sock_
cet::coded_exception< error, detail::translate > exception
Event finding and building.
Signal from collection planes.
IDNumber_t< Level::Run > RunNumber_t