+//_____________________________________________________________________________
+void AliSimulation::SetGAliceFile(const char* fileName)
+{
+// set the name of the galice file
+// the path is converted to an absolute one if it is relative
+
+ fGAliceFileName = fileName;
+ if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) {
+ char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(),
+ fGAliceFileName);
+ fGAliceFileName = absFileName;
+ delete[] absFileName;
+ }
+
+ AliDebug(2, Form("galice file name set to %s", fileName));
+}
+
+//_____________________________________________________________________________
+void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
+ Int_t nEvents)
+{
+// set the number of events per file for the given detector and data type
+// ("Hits", "Summable Digits", "Digits", "Reconstructed Points" or "Tracks")
+
+ TNamed* obj = new TNamed(detector, type);
+ obj->SetUniqueID(nEvents);
+ fEventsPerFile.Add(obj);
+}
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::ApplyAlignObjsToGeom(TObjArray* alObjArray)
+{
+ // Read collection of alignment objects (AliAlignObj derived) saved
+ // in the TClonesArray ClArrayName and apply them to the geometry
+ // manager singleton.
+ //
+ alObjArray->Sort();
+ Int_t nvols = alObjArray->GetEntriesFast();
+
+ Bool_t flag = kTRUE;
+
+ for(Int_t j=0; j<nvols; j++)
+ {
+ AliAlignObj* alobj = (AliAlignObj*) alObjArray->UncheckedAt(j);
+ if (alobj->ApplyToGeometry() == kFALSE) flag = kFALSE;
+ }
+
+ if (AliDebugLevelClass() >= 1) {
+ gGeoManager->CheckOverlaps(20);
+ TObjArray* ovexlist = gGeoManager->GetListOfOverlaps();
+ if(ovexlist->GetEntriesFast()){
+ AliError("The application of alignment objects to the geometry caused huge overlaps/extrusions!");
+ }
+ }
+
+ return flag;
+
+}
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* fileName, const char* clArrayName)
+{
+ // read collection of alignment objects (AliAlignObj derived) saved
+ // in the TClonesArray ClArrayName in the file fileName and apply
+ // them to the TGeo geometry passed as argument
+ //
+
+ TFile* inFile = TFile::Open(fileName,"READ");
+ if (!inFile || !inFile->IsOpen()) {
+ AliErrorClass(Form("Could not open file %s !",fileName));
+ return kFALSE;
+ }
+
+ TClonesArray* alObjArray = ((TClonesArray*) inFile->Get(clArrayName));
+ inFile->Close();
+ if (!alObjArray) {
+ AliErrorClass(Form("Could not get array (%s) from file (%s) !",clArrayName,fileName));
+ return kFALSE;
+ }
+
+ return ApplyAlignObjsToGeom(alObjArray);
+
+}
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::ApplyAlignObjsToGeom(AliCDBParam* param, AliCDBId& Id)
+{
+ // read collection of alignment objects (AliAlignObj derived) saved
+ // in the TClonesArray ClArrayName in the AliCDBEntry identified by
+ // param (to get the AliCDBStorage) and Id; apply the alignment objects
+ // to the TGeo geometry passed as argument
+ //
+
+ AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage(param);
+ AliCDBEntry* entry = storage->Get(Id);
+ TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
+
+ return ApplyAlignObjsToGeom(AlObjArray);
+
+}
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* uri, const char* path, Int_t runnum, Int_t version, Int_t sversion)
+{
+ // read collection of alignment objects (AliAlignObj derived) saved
+ // in the TClonesArray ClArrayName in the AliCDBEntry identified by
+ // param (to get the AliCDBStorage) and Id; apply the alignment objects
+ // to the TGeo geometry passed as argument
+ //
+
+ AliCDBParam* param = AliCDBManager::Instance()->CreateParameter(uri);
+ AliCDBId id(path, runnum, runnum, version, sversion);
+
+ return ApplyAlignObjsToGeom(param, id);
+
+}
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* detName, Int_t runnum, Int_t version, Int_t sversion)
+{
+ // read collection of alignment objects (AliAlignObj derived) saved
+ // in the TClonesArray ClArrayName in the AliCDBEntry identified by
+ // param (to get the AliCDBStorage) and Id; apply the alignment objects
+ // to the TGeo geometry passed as argument
+ //
+
+ InitCDBStorage("local://$ALICE_ROOT");
+ AliCDBPath path(detName,"Align","Data");
+ AliCDBEntry* entry = AliCDBManager::Instance()->Get(path.GetPath(),runnum,version,sversion);
+
+ if(!entry) return kFALSE;
+ TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
+
+ return ApplyAlignObjsToGeom(AlObjArray);
+}
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::SetAlignObjArraySingleDet(const char* detName)
+{
+ // Fills array of single detector's alignable objects from CDB
+
+ AliDebug(2, Form("Loading alignment data for detector: %s",detName));
+
+ AliCDBEntry *entry;
+
+ AliCDBPath path(detName,"Align","Data");
+
+ entry=AliCDBManager::Instance()->Get(path.GetPath());
+ if(!entry){
+ AliDebug(2,Form("Couldn't load alignment data for detector %s",detName));
+ return kFALSE;
+ }
+ entry->SetOwner(1);
+ TClonesArray *alignArray = (TClonesArray*) entry->GetObject();
+ alignArray->SetOwner(0);
+ AliDebug(2,Form("Found %d alignment objects for %s",
+ alignArray->GetEntries(),detName));
+
+ AliAlignObj *alignObj=0;
+ TIter iter(alignArray);
+
+ // loop over align objects in detector
+ while( ( alignObj=(AliAlignObj *) iter.Next() ) ){
+ fAlignObjArray->Add(alignObj);
+ }
+ // delete entry --- Don't delete, it is cached!
+
+ AliDebug(2, Form("fAlignObjArray entries: %d",fAlignObjArray->GetEntries() ));
+ return kTRUE;
+
+}
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
+{
+ // Read the alignment objects from CDB.
+ // Each detector is supposed to have the
+ // alignment objects in DET/Align/Data CDB path.
+ // All the detector objects are then collected,
+ // sorted by geometry level (starting from ALIC) and
+ // then applied to the TGeo geometry.
+ // Finally an overlaps check is performed.
+
+ Bool_t delRunLoader = kFALSE;
+ if (!runLoader) {
+ runLoader = LoadRun("READ");
+ if (!runLoader) return kFALSE;
+ delRunLoader = kTRUE;
+ }
+
+ // Load alignment data from CDB and fill fAlignObjArray
+ if(fLoadAlignFromCDB){
+ if(!fAlignObjArray) fAlignObjArray = new TObjArray();
+
+ //fAlignObjArray->RemoveAll();
+ fAlignObjArray->Clear();
+ fAlignObjArray->SetOwner(0);
+
+ TString detStr = fLoadAlignData;
+ TString dataNotLoaded="";
+ TString dataLoaded="";
+
+ TObjArray* detArray = runLoader->GetAliRun()->Detectors();
+ for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
+ AliModule* det = (AliModule*) detArray->At(iDet);
+ if (!det || !det->IsActive()) continue;
+ if (IsSelected(det->GetName(), detStr)) {
+ if(!SetAlignObjArraySingleDet(det->GetName())){
+ dataNotLoaded += det->GetName();
+ dataNotLoaded += " ";
+ } else {
+ dataLoaded += det->GetName();
+ dataLoaded += " ";
+ }
+ }
+ } // end loop over detectors
+
+ if ((detStr.CompareTo("ALL") == 0)) detStr = "";
+ dataNotLoaded += detStr;
+ AliInfo(Form("Alignment data loaded for: %s",
+ dataLoaded.Data()));
+ AliInfo(Form("Didn't/couldn't load alignment data for: %s",
+ dataNotLoaded.Data()));
+ } // fLoadAlignFromCDB flag
+
+ // Check if the array with alignment objects was
+ // provided by the user. If yes, apply the objects
+ // to the present TGeo geometry
+ if (fAlignObjArray) {
+ if (gGeoManager && gGeoManager->IsClosed()) {
+ if (ApplyAlignObjsToGeom(fAlignObjArray) == kFALSE) {
+ AliError("The misalignment of one or more volumes failed!"
+ "Compare the list of simulated detectors and the list of detector alignment data!");
+ if (delRunLoader) delete runLoader;
+ return kFALSE;
+ }
+ }
+ else {
+ AliError("Can't apply the misalignment! gGeoManager doesn't exist or it is still opened!");
+ if (delRunLoader) delete runLoader;
+ return kFALSE;
+ }
+ }
+
+ if (delRunLoader) delete runLoader;
+
+ return kTRUE;
+}
+
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::SetRunNumber()
+{
+ // Set the CDB manager run number
+ // The run number is retrieved from gAlice
+
+ if(AliCDBManager::Instance()->GetRun() < 0) {
+ AliRunLoader* runLoader = LoadRun("READ");
+ if (!runLoader) return kFALSE;
+ else {
+ AliCDBManager::Instance()->SetRun(runLoader->GetAliRun()->GetRunNumber());
+ AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
+ delete runLoader;
+ }
+ }
+ return kTRUE;
+}
+